Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FILE: 'shooter.as'
- package {
- import flash.display.MovieClip;
- import flash.text.TextField;
- import flash.ui.Keyboard;
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- import Ship;
- import Bullet;
- import Util;
- public class shooter extends MovieClip {
- private var s: Ship;
- private var bullet_timeout: int;
- private var keyPressed:Object = { };
- private var lbl: TextField;
- public function shooter() {
- // constructor code
- lbl = new TextField();
- lbl.text = 'Click anywhere on the canvas to give focus.\nArrow keys move around, space fires bullets.'
- lbl.selectable = false;
- lbl.width = 400;
- lbl.x = 12;
- lbl.y = 12;
- this.stage.addChild(lbl);
- s = new Ship();
- s.x = 275;
- s.y = 200;
- s.rotation = -90;
- this.stage.addChild(s);
- this.stage.addEventListener(KeyboardEvent.KEY_DOWN, on_keydown);
- this.stage.addEventListener(KeyboardEvent.KEY_UP, on_keyup);
- this.stage.addEventListener(Event.ENTER_FRAME, on_enterframe);
- bullet_timeout = 0;
- }
- private function on_keydown(e: KeyboardEvent) {
- keyPressed[e.keyCode] = true;
- }
- private function on_keyup(e: KeyboardEvent) {
- keyPressed[e.keyCode] = false;
- }
- private function on_enterframe(e: Event) {
- if (keyPressed[Keyboard.LEFT]) {
- s.rotation -= 3;
- }
- if (keyPressed[Keyboard.RIGHT]) {
- s.rotation += 3;
- }
- if (keyPressed[Keyboard.UP]) {
- s.x += Math.cos(Util.deg2rad(s.rotation))*3;
- s.y += Math.sin(Util.deg2rad(s.rotation))*3;
- }
- if (keyPressed[Keyboard.DOWN]) {
- s.x -= Math.cos(Util.deg2rad(s.rotation))*5;
- s.y -= Math.sin(Util.deg2rad(s.rotation))*5;
- }
- if (keyPressed[Keyboard.SPACE]) {
- if (bullet_timeout >= 5) {
- var x1, x2, y1, y2;
- x1 = s.x + Math.cos(Util.deg2rad(s.rotation+15))*20;
- y1 = s.y + Math.sin(Util.deg2rad(s.rotation+15))*20;
- x2 = s.x + Math.cos(Util.deg2rad(s.rotation-15))*20;
- y2 = s.y + Math.sin(Util.deg2rad(s.rotation-15))*20;
- var b1 = new Bullet(x1, y1, s.rotation);
- var b2 = new Bullet(x2, y2, s.rotation);
- this.stage.addChild(b1);
- this.stage.addChild(b2);
- bullet_timeout = 0;
- } else {
- bullet_timeout++;
- }
- }
- }
- }
- }
- // FILE 'Ship.as'
- package {
- import flash.display.MovieClip;
- public class Ship extends MovieClip {
- public function Ship() {
- // draw ship
- with (this.graphics) {
- lineStyle (1, 0x000000, 1);
- moveTo(15, 0);
- lineTo(-15, 7.5);
- lineTo(-15, -7.5);
- lineTo(15, 0);
- }
- }
- }
- }
- // FILE 'Bullet.as'
- package {
- import flash.events.Event;
- import flash.display.MovieClip;
- import Util;
- public class Bullet extends MovieClip {
- public function Bullet(nx, ny, nrotation: Number) {
- // constructor code
- with (this.graphics) {
- lineStyle (1, 0x000000, 1);
- drawRect(-5, -2, 10, 4);
- }
- this.x = nx;
- this.y = ny;
- this.rotation = nrotation;
- this.addEventListener(Event.ENTER_FRAME, on_enterframe);
- }
- private function on_enterframe(e: Event) {
- if (this.x<=0 || this.x>=550 || this.y<=0 || this.y>=400) {
- this.removeEventListener(Event.ENTER_FRAME, on_enterframe);
- this.stage.removeChild(this);
- } else {
- this.x += Math.cos(Util.deg2rad(this.rotation))*7;
- this.y += Math.sin(Util.deg2rad(this.rotation))*7;
- }
- }
- }
- }
- // FILE 'Util.as'
- package {
- public class Util {
- public static function deg2rad(deg:Number):Number {
- return Math.PI*deg/180.0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement