Guest User

Untitled

a guest
Jun 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package com.asgamer.directionalmovement
  2. {
  3.  
  4. import flash.display.MovieClip;
  5. import flash.display.Stage;
  6. import flash.events.Event;
  7. import com.senocular.utils.KeyObject;
  8. import flash.ui.Keyboard;
  9.  
  10. public class Ship extends MovieClip
  11. {
  12.  
  13. private var key:KeyObject;
  14. private var speed:Number = 0.3;
  15. private var rotateSpeed:Number = 5;
  16. private var vx:Number = 0;
  17. private var vy:Number = 0;
  18. private var friction:Number = 0.95;
  19.  
  20. public function Ship () : void
  21. {
  22. key = new KeyObject(stage);
  23. addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
  24. }
  25.  
  26. public function loop(e:Event) : void
  27. {
  28.  
  29. if (key.isDown(Keyboard.UP))
  30. {
  31. vy += Math.sin(degreesToRadians(rotation)) * speed;
  32. vx += Math.cos(degreesToRadians(rotation)) * speed;
  33. flame.visible = true;
  34. } else {
  35. vy *= friction;
  36. vx *= friction;
  37. flame.visible = false;
  38. }
  39.  
  40. if (key.isDown(Keyboard.RIGHT))
  41. rotation += rotateSpeed;
  42. else if (key.isDown(Keyboard.LEFT))
  43. rotation -= rotateSpeed;
  44.  
  45. y += vy;
  46. x += vx;
  47.  
  48.  
  49. }
  50.  
  51. public function degreesToRadians(degrees:Number) : Number
  52. {
  53. return degrees * Math.PI / 180;
  54. }
  55.  
  56. }
  57.  
  58.  
  59. }
Add Comment
Please, Sign In to add comment