Advertisement
nordlaender

mover

Oct 10th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Mover
  2.     constructor: (@shape,@speed)->
  3.         @up=false
  4.         @down=false
  5.         @right=false
  6.         @left=false
  7.         document.onkeyup=@onKeyUp
  8.         document.onkeydown=@onKeyDown
  9.        
  10.     onKeyDown: (e)=>
  11.         if e?
  12.             switch e.keyCode
  13.                 when 37 #left
  14.                     @left=true
  15.                     @right=false
  16.                 when 38 #up
  17.                     @up=true
  18.                     @down=false
  19.                 when 39 #right
  20.                     @right=true
  21.                     @left=false
  22.                 when 40 #down
  23.                     @down=true
  24.                     @up=false
  25.     onKeyUp: (e)=>
  26.         alert "up"
  27.         if e?
  28.             switch e.keyCode
  29.                 when 37
  30.                     @left=false
  31.                 when 38
  32.                     @up=false
  33.                 when 39
  34.                     @right=false
  35.                 when 40
  36.                     @down=false
  37.     tick: (elapsedTime)=>
  38.         if @up then @shape.y=@shape.y - @speed
  39.         if @down then @shape.y=@shape.y + @speed
  40.         if @right then @shape.x=@shape.x + @speed
  41.         if @left then @shape.x=@shape.x - @speed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement