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...