Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2012
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FILE: 'shooter.as'
  2.  
  3. package  {
  4.     import flash.display.MovieClip;
  5.     import flash.text.TextField;
  6.     import flash.ui.Keyboard;
  7.     import flash.events.KeyboardEvent;
  8.     import flash.events.Event;
  9.     import Ship;
  10.     import Bullet;
  11.     import Util;
  12.    
  13.     public class shooter extends MovieClip {
  14.        
  15.         private var s: Ship;
  16.         private var bullet_timeout: int;
  17.         private var keyPressed:Object = { };
  18.         private var lbl: TextField;
  19.        
  20.         public function shooter() {
  21.             // constructor code
  22.            
  23.             lbl = new TextField();
  24.             lbl.text = 'Click anywhere on the canvas to give focus.\nArrow keys move around, space fires bullets.'
  25.             lbl.selectable = false;
  26.             lbl.width = 400;
  27.             lbl.x = 12;
  28.             lbl.y = 12;
  29.             this.stage.addChild(lbl);
  30.            
  31.             s = new Ship();
  32.             s.x = 275;
  33.             s.y = 200;
  34.             s.rotation = -90;
  35.             this.stage.addChild(s);
  36.            
  37.             this.stage.addEventListener(KeyboardEvent.KEY_DOWN, on_keydown);
  38.             this.stage.addEventListener(KeyboardEvent.KEY_UP, on_keyup);
  39.             this.stage.addEventListener(Event.ENTER_FRAME, on_enterframe);
  40.            
  41.             bullet_timeout = 0;
  42.         }
  43.        
  44.         private function on_keydown(e: KeyboardEvent) {
  45.             keyPressed[e.keyCode] = true;
  46.         }
  47.        
  48.         private function on_keyup(e: KeyboardEvent) {
  49.             keyPressed[e.keyCode] = false;
  50.         }
  51.        
  52.         private function on_enterframe(e: Event) {
  53.             if (keyPressed[Keyboard.LEFT]) {
  54.                 s.rotation -= 3;
  55.             }
  56.             if (keyPressed[Keyboard.RIGHT]) {
  57.                 s.rotation += 3;
  58.             }
  59.             if (keyPressed[Keyboard.UP]) {
  60.                 s.x += Math.cos(Util.deg2rad(s.rotation))*3;
  61.                 s.y += Math.sin(Util.deg2rad(s.rotation))*3;
  62.             }
  63.             if (keyPressed[Keyboard.DOWN]) {
  64.                 s.x -= Math.cos(Util.deg2rad(s.rotation))*5;
  65.                 s.y -= Math.sin(Util.deg2rad(s.rotation))*5;
  66.             }
  67.             if (keyPressed[Keyboard.SPACE]) {
  68.                 if (bullet_timeout >= 5) {
  69.                     var x1, x2, y1, y2;
  70.                     x1 = s.x + Math.cos(Util.deg2rad(s.rotation+15))*20;
  71.                     y1 = s.y + Math.sin(Util.deg2rad(s.rotation+15))*20;
  72.                     x2 = s.x + Math.cos(Util.deg2rad(s.rotation-15))*20;
  73.                     y2 = s.y + Math.sin(Util.deg2rad(s.rotation-15))*20;
  74.                     var b1 = new Bullet(x1, y1, s.rotation);
  75.                     var b2 = new Bullet(x2, y2, s.rotation);
  76.                     this.stage.addChild(b1);
  77.                     this.stage.addChild(b2);
  78.                     bullet_timeout = 0;
  79.                 } else {
  80.                     bullet_timeout++;
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. // FILE 'Ship.as'
  88.  
  89. package  {
  90.     import flash.display.MovieClip;
  91.    
  92.     public class Ship extends MovieClip {
  93.  
  94.         public function Ship() {
  95.             // draw ship
  96.             with (this.graphics) {
  97.                 lineStyle (1, 0x000000, 1);
  98.                 moveTo(15, 0);
  99.                 lineTo(-15, 7.5);
  100.                 lineTo(-15, -7.5);
  101.                 lineTo(15, 0);
  102.             }
  103.         }
  104.     }
  105. }
  106.  
  107. // FILE 'Bullet.as'
  108.  
  109. package  {
  110.     import flash.events.Event;
  111.     import flash.display.MovieClip;
  112.     import Util;
  113.    
  114.     public class Bullet extends MovieClip {
  115.  
  116.         public function Bullet(nx, ny, nrotation: Number) {
  117.             // constructor code
  118.             with (this.graphics) {
  119.                 lineStyle (1, 0x000000, 1);
  120.                 drawRect(-5, -2, 10, 4);
  121.             }
  122.            
  123.             this.x = nx;
  124.             this.y = ny;
  125.             this.rotation = nrotation;
  126.            
  127.             this.addEventListener(Event.ENTER_FRAME, on_enterframe);
  128.         }
  129.  
  130.         private function on_enterframe(e: Event) {
  131.             if (this.x<=0 || this.x>=550 || this.y<=0 || this.y>=400) {
  132.                 this.removeEventListener(Event.ENTER_FRAME, on_enterframe);
  133.                 this.stage.removeChild(this);
  134.             } else {
  135.                 this.x += Math.cos(Util.deg2rad(this.rotation))*7;
  136.                 this.y += Math.sin(Util.deg2rad(this.rotation))*7;
  137.             }
  138.         }
  139.     }
  140. }
  141.  
  142. // FILE 'Util.as'
  143.  
  144. package  {
  145.     public class Util {
  146.         public static function deg2rad(deg:Number):Number {
  147.             return Math.PI*deg/180.0;
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement