O'Reilly logo

Developing Mobile Games with Moai SDK by Francisco Tufro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Movement

Now let's get right into the important stuff. First we'll define a couple of methods on Character. Then we'll rebuild InputManager to handle key events instead of mouse events and integrate that with our game.

  1. The first method we're going to use will make our character run. It will be called when we press the A or D keys, moving left or right respectively. We'll pass it two parameters, direction (-1 for left, 1 for right) and keyDown, which is a Boolean value that tells us if the player is pressing the key.
  2. The first thing we do is use the direction parameter in combination with setScl (used to scale objects). The idea is that if you scale a sprite by a negative factor, it flips in the direction of the scaled component, so what we're doing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required