Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Games with Flutter

You're reading from  Building Games with Flutter

Product type Book
Published in Jun 2022
Publisher Packt
ISBN-13 9781801816984
Pages 224 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Paul Teale Paul Teale
Profile icon Paul Teale

Table of Contents (17) Chapters

Preface 1. Part 1: Game Basics
2. Chapter 1: Getting Started with Flutter Games 3. Chapter 2: Working with the Flame Engine 4. Chapter 3: Building a Game Design 5. Part 2: Graphics and Sound
6. Chapter 4: Drawing and Animating Graphics 7. Chapter 5: Moving the Graphics with Input 8. Chapter 6: Playing Sound Effects and Music 9. Chapter 7: Designing Your Own Levels 10. Chapter 8: Scaling the Game for Web and Desktop 11. Part 3: Advanced Games Programming
12. Chapter 9: Implementing Advanced Graphics Effects 13. Chapter 10: Making Intelligent Enemies with AI 14. Chapter 11: Finishing the Game 15. Other Books You May Enjoy Appendix: Answers

Colliding with other sprites

Let's fix the issue with George wandering off the edge of the screen that we saw at the end of the last section and flip the direction if he hits the edge of the screen. To do this, follow these steps:

  1. In the onLoad function of main.dart, let's add the ScreenCollidable component to the bottom of the function so we can detect collisions between George and the screen edges:
    add(ScreenCollidable());
  2. In the george.dart file, change the class definition to add the HasHitBoxes and Collidable mixins:
    class George extends SpriteAnimationComponent with HasHitBoxes, Collidable {
  3. At the bottom of the onLoad function, add the HitboxRectangle shape for the collision detection:
    addHitbox(HitboxRectangle());
  4. Add the following import at the top of the file to resolve the reference to HitboxRectangle:
    import 'package:flame/geometry.dart';
  5. At the bottom of this class file, after the update function definition, add the following onCollision...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}