1. Home
  2. Computing & Technology
  3. Ruby

Make Your Sprite Move

By Amanda & Michael Morin, About.com

4 of 5

Processing Events in the Player Class

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.

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
Explore Ruby
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Tasks & Scripts
  5. Game Programming
  6. Processing Events in the Player Class

©2009 About.com, a part of The New York Times Company.

All rights reserved.