1. Computing

Make Your Sprite Move

By

Once there's an event method set up in the player class, you just need to set up some case statement to see which type the event is and which key was pressed. Depending on the key pressed, you'll change a @dir instance variable, which determines which way the player will move.

<pre><code> def event(e)
case(e)
when KeyDownEvent
case(e.key)
when K_LEFT
@dir -= 1
when K_RIGHT
@dir += 1
end

when KeyUpEvent
case(e.key)
when K_LEFT
@dir += 1
when K_RIGHT
@dir -= 1
end
end
end
</code></pre>
  1. About.com
  2. Computing
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. Processing Events in the Player Class

©2014 About.com. All rights reserved.