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.
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) andkeyDown
, which is a Boolean value that tells us if the player is pressing the key.The first thing we do is use the
direction
parameter in combination withsetScl
(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 here is telling our sprite to face left or right according to what key the player pressed.function Character:run ( direction, keyDown ) if keyDown then self.prop:setScl ( direction, 1 )
Then, we force the linear velocity by 100 in the specified...