Advertisement
Guest User

Character.as

a guest
Nov 15th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. // Code to move character
  2. package {
  3. import flash.display.*;
  4. import flash.events.*;
  5. import flash.ui.*;
  6. import Shark;
  7.  
  8. public class Character extends MovieClip {
  9. public var maxSpeed=10;
  10. public var dx=0;
  11. public var dy=0;
  12. public var rot=0;
  13. private var keysDown: Array = new Array();
  14. // Declare variables:
  15. var bullet:Bullet;
  16. private var dir=0;
  17. private var speed=0;
  18. var reload:int = 10;
  19. private var bullets:Array;
  20.  
  21.  
  22. public function Character(bulletList) {
  23. bullets=bulletList;
  24. // Initialize variables:
  25.  
  26. addEventListener(Event.ADDED_TO_STAGE, init);
  27. }
  28.  
  29. function init(e:Event):void {
  30. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
  31. stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
  32. this.addEventListener(Event.ENTER_FRAME, processInput);
  33. this.addEventListener(Event.ENTER_FRAME, moveMe);
  34. removeEventListener(Event.ADDED_TO_STAGE, init);
  35. }
  36.  
  37. private function keyPressed(e:KeyboardEvent):void {
  38. keysDown[e.keyCode]=true;
  39. // Handle keys pressed:
  40. }
  41.  
  42. private function keyReleased(e:KeyboardEvent):void {
  43. keysDown[e.keyCode]=false;
  44. }
  45.  
  46. private function processInput(e:Event) {
  47. // Handle keys held:
  48. if (keysDown[Keyboard.LEFT]) {
  49. dir-=5;
  50. if (dir<0) {
  51. dir+=360;
  52. }
  53. }
  54. if (keysDown[Keyboard.RIGHT]) {
  55. dir+=5;
  56. if (dir>360) {
  57. dir-=360;
  58. }
  59. }
  60. if (keysDown[Keyboard.DOWN]) {
  61. speed-=1;
  62. }
  63. if (keysDown[Keyboard.UP]) {
  64. speed+=1;
  65. }
  66. if (--reload==0) {
  67. reload=1;
  68.  
  69. if (keysDown[Keyboard.SPACE]) {
  70. reload=10;
  71. bullet=new Bullet(x,y,20*Math.cos(rot*Math.PI/180),20*Math.sin(rot*Math.PI/180),rot);
  72. stage.addChild(bullet);
  73. bullets.push(bullet);
  74. }
  75. }
  76. // Optional: friction and maximum speed:
  77. if (speed>maxSpeed) {
  78. speed=maxSpeed;
  79. } else if (speed>0) {
  80. speed-=0.1;
  81. } else if (-speed>maxSpeed) {
  82. speed=- maxSpeed;
  83. } else if (speed<0) {
  84. speed+=0.1;
  85. }
  86. dx=speed*Math.cos(dir*Math.PI/180);
  87. dy=speed*Math.sin(dir*Math.PI/180);
  88. rot=dir;
  89.  
  90.  
  91. }
  92. public function get Xpos():Number {
  93. return this.x;
  94. }
  95. public function get Ypos():Number {
  96. return this.y;
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. private function moveMe(e:Event) {
  105. this.x+=dx;
  106. this.y+=dy;
  107.  
  108. this.rotation=rot;
  109.  
  110. // stop if it tries to go off the screen
  111. if (this.x<0) {
  112. this.x=0;
  113. }
  114. if (this.x>stage.stageWidth) {
  115. this.x=stage.stageWidth;
  116. }
  117. if (this.y<0) {
  118. this.y=0;
  119. }
  120. if (this.y>stage.stageHeight) {
  121. this.y=stage.stageHeight;
  122. }
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement