Book Image

Building Games with Flutter

By : Paul Teale
Book Image

Building Games with Flutter

By: Paul Teale

Overview of this book

With its powerful tools and quick implementation capabilities, Flutter provides a new way to build scalable cross-platform apps. In this book, you'll learn how to build on your knowledge and use Flutter as the foundation for creating games. This game development book takes a hands-on approach to building a complete game from scratch. You'll see how to get started with the Flame library and build a simple animated example to test Flame. You'll then discover how to organize and load images and audio in your Flutter game. As you advance, you'll gain insights into the game loop and set it up for fast and efficient processing. The book also guides you in using Tiled to create maps, add sprites to the maps that the player can interact with, and see how to use tilemap collision to create paths for a player to walk on. Finally, you'll learn how to make enemies more intelligent with artificial intelligence (AI). By the end of the book, you'll have gained the confidence to build fun multiplatform games with Flutter.
Table of Contents (17 chapters)
1
Part 1: Game Basics
5
Part 2: Graphics and Sound
11
Part 3: Advanced Games Programming

Moving our character with touch

Now that we have George moving with the joystick, let's look at an alternative method for moving our character via screen touch events, which is very popular in games. With mobile devices having very sensitive high-resolution screens nowadays, we can use touch events or gestures to move our character from its current location to the location on the screen that was tapped. Using trigonometry, we can calculate the angle between the origin and target locations and use the angle to move George in the correct direction, with the correct animation that matches the direction.

We are already detecting a tap event for the run button via the HasTappables mixin. So, to detect touches on the screen, we need to add the Tappable mixin to the Background class and override onTapUp to get an x and y location that we can use to calculate the movement.

Because we need to know this coordinate inside of the George class, we will need to pass in a reference to...