In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

SpaceShooter

=====================================================
SpaceShooter Experiment v1.0
by Johannes Jensen, 2009.
========================================================
Get Adobe Flash player
by Johannes 19 Dec 2009
/* =====================================================
 SpaceShooter Experiment v1.0
 by Johannes Jensen, 2009.
======================================================== */

package {
	// Import needed libraries.
	import flash.display.Sprite;
    import flash.display.Stage;
    import flash.geom.Point;
    
    // Main class, that will create the main Sprite.
    public class Main extends Sprite {
			// Global variables that are gonna be passed to a lot of classes.
			// I'm using an Object for the bullets to make it sort of easier
			// to work with.
    		public var keyboardRecorder:KeyboardRecorder;
			public var bullets:Object = { id: 0, bullets: new Object() };

			// Classes
			public var stars:Stars;
    		public var ship:Ship;
    		
        	public function Main() {
        		// draw a black box which will act as the bg.
        		this.graphics.lineStyle(1, 0x000000);
        		this.graphics.beginFill(0x000000);
        		this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
				
				// Create star heaven (the background of stars scrolling down.)
				stars = new Stars(this, stage);
            	
            	// Check for the keyboard presses
            	keyboardRecorder = new KeyboardRecorder(stage);
            	
            	// Add the player to the stage.
            	ship = new Ship(this, stage, keyboardRecorder.keys, bullets);
				
			// Crate test.
			var aCrate:Crate = new Crate(this, stage, 0, 12, bullets, new Point(stage.stageWidth/2, 0), ship);
        }
    }
}

// Import needed libraries.
import flash.display.*;
import flash.filters.*;
import flash.events.*;
import flash.geom.*;
import flash.net.*;

// Stars class.
// The animated stars in the background.
class Stars {
	public var ref:Sprite;
	public var stageRef:Stage;
	public var display:BitmapData;
	public var bitmap:Bitmap;
	public var starsInterval:int = 20;
	public var starsC:int = 0;
	
	public function Stars(_ref:Sprite, _stage:Stage):void {
		ref = _ref;
		stageRef = _stage;
		
		display = new BitmapData(stageRef.stageWidth, stageRef.stageHeight, true, 0x00000000);
		bitmap = new Bitmap(display);
		
		ref.addChild(bitmap);
		stageRef.addEventListener(Event.ENTER_FRAME, main);
		
		init();
	}
	
	private function get R():int {
		return 0x40 + Math.floor(Math.random() * 0xBF);
	}
	
	private function newStars(i:int):void {
		display.setPixel32(Math.floor(Math.random()*stageRef.stageWidth), i + 1, 0x55 << 24 | R << 16 | R << 8 | R);
		display.setPixel32(Math.floor(Math.random()*stageRef.stageWidth), i + 2, 0x7F << 24 | R << 16 | R << 8 | R);
		display.setPixel32(Math.floor(Math.random()*stageRef.stageWidth), i + 2, 0xBF << 24 | R << 16 | R << 8 | R);
		display.setPixel32(Math.floor(Math.random()*stageRef.stageWidth), i + 3, 0xFF << 24 | R << 16 | R << 8 | R);
	}
	
	private function init():void {
		for(var i:int = 0; i < stageRef.stageHeight; i += starsInterval) {
			newStars(i);
		}
	}
	
	private function main(e:Event):void {
		if(starsC == starsInterval) {
			newStars(0);
			starsC = 0;
		}
		
		display.scroll(0, 1);
		starsC++;
	}
}

// KeyboardRecorder class.
// This class is used for checking keyinput.
class KeyboardRecorder {
	// Array of the keyboard keys.
	public var keys:Array = new Array(255);
	
	// Constructor
	public function KeyboardRecorder(_stage:Stage):void {
		_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
		_stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
	}
	
	// Once a key is down place it into the keys Array.
	private function keyDown(e:KeyboardEvent):void {
		keys[e.keyCode] = true;
	}
	
	// Once a key has been released remove it from the keys Array.
	private function keyUp(e:KeyboardEvent):void {
		keys[e.keyCode] = false;
	}
}

// Ship class.
// This is the class that will handle the ship.
class Ship extends Sprite {
	public var ref:Sprite;
	public var keys:Array;
	public var stageRef:Stage;
	public var bulletsRef:Object;
	
	private var xv:int = 0;
	private var yv:int = 0;
	private var mv:int = 8;
	private var shot:Boolean = false;
	
	// Constructor
	public function Ship(_ref:Sprite, _stage:Stage, _keys:Array, _bullets:Object):void {
		draw();
		ref = _ref;
		ref.addChild(this);
		stageRef = _stage;
		keys = _keys;
		bulletsRef = _bullets;
		this.x = _stage.stageWidth / 2;
		this.y = _stage.stageHeight - 64;
		stageRef.addEventListener(Event.ENTER_FRAME, main);
		this.filters = [new GlowFilter(0xFFFFFF)];
	}
	
	// Draw the ship
	private function draw():void {
		// left wing
		this.graphics.lineStyle(1, 0x9F2209, 1, true);
		this.graphics.beginFill(0x9F2209);
		this.graphics.moveTo(-9, 9);
		this.graphics.lineTo(-12, 17);
		this.graphics.lineTo(-3, 9);
		this.graphics.lineTo(-9, 9);
		this.graphics.endFill();
		
		// right wing
		this.graphics.lineStyle(1, 0x9F2209, 1, true);
		this.graphics.beginFill(0x9F2209);
		this.graphics.moveTo(9, 9);
		this.graphics.lineTo(12, 17);
		this.graphics.lineTo(3, 9);
		this.graphics.lineTo(9, 9);
		this.graphics.endFill();
		
		// body
		this.graphics.lineStyle(1, 0x2FAB56, 1, true);
		this.graphics.beginFill(0x2FAB56);
		this.graphics.moveTo(0, -9);
		this.graphics.lineTo(9, 9);
		this.graphics.lineTo(-9, 9);
		this.graphics.lineTo(0, -9);
		this.graphics.endFill();
	}
	
	private function get w():Number { return this.width / 2; }
	private function get h():Number { return this.height / 2; }
	
	private function main(e:Event):void {
		if(keys[32] && !shot) {
			bulletsRef.bullets[bulletsRef.id+1] = {data:new Bullet(ref, stageRef, bulletsRef, new Point(this.x, this.y - h), bulletsRef.id+1)};
			bulletsRef.id++;
			shot = true;
		}
		
		if(!keys[32]) {
			shot = false;
		}
		
		if(keys[37]) {
			xv--;
		} else if(keys[39]) {
			xv++;
		} else {
			xv -= xv > 0 ? 1 : (xv < 0 ? -1 : 0);
		}
		
		if(keys[38]) {
			yv--;
		} else if(keys[40]) {
			yv++;
		} else {
			yv -= yv > 0 ? 1 : (yv < 0 ? -1 : 0);
		}
		
		if(xv > mv) xv = mv;
		if(xv < -mv) xv = -mv;
		if(yv > mv) yv = mv;
		if(yv < -mv) yv = -mv;
		
		if(this.x - w < 0) xv = xv < 0 ? -xv : xv;
		if(this.x + w > stageRef.stageWidth) xv = xv > 0 ? -xv : xv;
		if(this.y - h < 0) yv = yv < 0 ? -yv : yv;
		if(this.y + h > stageRef.stageHeight) yv = yv > 0 ? -yv : yv;
		
		this.x += xv;
		this.y += yv;
	}
	
	public function cleanUp():void {
		ref.removeChild(this);
		stageRef.removeEventListener(Event.ENTER_FRAME, main);
	}
}

// Bullet class.
// The bullets are the ones coming out from your ship, or enemies.
class Bullet extends Sprite {
	public var ref:Sprite;
	public var stageRef:Stage;
	public var bulletsRef:Object;
	public var id:String;
	
	public function Bullet(_ref:Sprite, _stage:Stage, _bullets:Object, place:Point, _id:String):void {
		draw();
		ref = _ref;
		ref.addChild(this);
		stageRef = _stage;
		stageRef.addEventListener(Event.ENTER_FRAME, main);
		bulletsRef = _bullets;
		id = _id;
		this.x = place.x;
		this.y = place.y;
	}
	
	private function draw():void {
		this.graphics.lineStyle(1, 0x1F4BB5, 1, true);
		this.graphics.beginFill(0x1F4BB5);
		this.graphics.moveTo(0, -2);
		this.graphics.lineTo(-2, 2);
		this.graphics.lineTo(2, 2);
		this.graphics.lineTo(0, -2);
		this.graphics.endFill();
	}
	
	public function cleanUp():void {
		ref.removeChild(this);
		stageRef.removeEventListener(Event.ENTER_FRAME, main);
		delete bulletsRef.bullets[id];
	}
	
	public function main(e:Event):void {
		this.y -= 10;
		
		if(this.y < 0) {
			cleanUp();
		}
	}
}

// Crate class.
// This class makes a brown crate which you can shoot and it explodes.
class Crate extends Sprite {
	public var ref:Sprite;
	public var stageRef:Stage;
	public var bulletsRef:Object;
	public var playerRef:Ship;
	
	private var xv:int = 0;
	private var yv:int = 0;
	private var radian:Number;
	private var size:Number;
	
	public function Crate(_ref:Sprite, _stage:Stage, _angle:int, _size:int, _bullets:Object, _pos:Point, _player:Ship) {
		draw(_size);
		this.x = _pos.x;
		this.y = _pos.y;
		size = _size;
		ref = _ref;
		ref.addChild(this);
		stageRef = _stage;
		stageRef.addEventListener(Event.ENTER_FRAME, main);
		radian = ((_angle + 90) * Math.PI) / 180;
		bulletsRef = _bullets;
		playerRef = _player;
		xv = 2.5 * Math.cos(radian);
		yv = 2.5 * Math.sin(radian);
		this.filters = [new GlowFilter(0xFFFFFF)];
	}
	
	private function draw(size:int):void {
		this.graphics.lineStyle(1, 0x622E07, 1, true);
		this.graphics.beginFill(0x622E07);
		this.graphics.drawRect(-size, -size, size * 2, size * 2);
		this.graphics.endFill();
		this.graphics.lineStyle(1, 0x824405);
		this.graphics.moveTo(-size, -size);
		this.graphics.lineTo(size, size);
		this.graphics.moveTo(size, -size);
		this.graphics.lineTo(-size, size);
	}
	
	public function get w():Number { return this.width / 2; }
	public function get h():Number { return this.height /2; }
	public function A():Number { return Math.floor(Math.random()*360); }
	
	public function main(e:Event):void {
		this.x += xv;
		this.y += yv;
		
		if(this.x - w < 0) xv = Math.abs(xv);
		if(this.x + w > stageRef.stageWidth) xv = -Math.abs(xv);
		if(this.y - h < 0) yv = Math.abs(yv);
		if(this.y + h > stageRef.stageHeight) {
			cleanUp();
			return;
		}
		
		for(var i:String in bulletsRef.bullets) {
			if(this.hitTestObject(bulletsRef.bullets[i].data)) {
				bulletsRef.bullets[i].data.cleanUp();
				delete bulletsRef.bullets[i];
				if(size >= 3) {
					new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
					new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
					new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
					new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
				}
				cleanUp();
				return;
			}
		}
		
		if(this.hitTestObject(playerRef)) {
			if(size >= 3) {
				new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
				new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
				new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
				new Crate(ref, stageRef, A(), size / 1.5, bulletsRef, new Point(this.x, this.y), playerRef);
			}
			cleanUp();
		}
	}
	
	public function cleanUp():void {
		ref.removeChild(this);
		stageRef.removeEventListener(Event.ENTER_FRAME, main);
	}
}

// BasicEnemy class.
// This class controls a basic enemy.
class BasicEnemy extends Sprite {
	public var ref:Sprite;
	public var stageRef:Stage;
	
	private var xv:int = 0;
	private var yv:int = 0;
	
	public function BasicEnemy(_ref:Sprite, _stage:Stage):void {
		ref = _ref;
		stageRef = _stage;
	}
}