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

forked from: Game

Get Adobe Flash player
by TylerS 10 Jan 2010
    Embed
/**
 * Copyright TylerS ( http://wonderfl.net/user/TylerS )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dsoG
 */

// forked from TylerS's Game
 package {
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.net.*;
    import flash.events.Event;
    import flash.display.*;
    import flash.system.Security;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import gs.TweenLite; 
    import gs.easing.*; 
    
    
    [SWF(backgroundColor="#000000", frameRate="40")]
    public class SpaceShooter extends Sprite {
        public function SpaceShooter() {
        	Security.loadPolicyFile("http://www.justsuppose.com/crossdomain.xml");
            loadImageData();
        }
        
        private var ship:Sprite;
        public var aliens:Array;
        public var missiles:Array = new Array();
        
        private function playGame(e:Event):void {
        	
        	    // See if any missiles hit our aliens.
        	    for (var m:int = 0; m < missiles.length; m++) {
        	       for (var a:int = 0; a < aliens.length; a++) {
        	           var theMissile:Sprite = missiles[m];
        	           var theAlien:Sprite = aliens[a];
        	           
        	           if (theMissile.y > theAlien.y &&
        	               theMissile.y < (theAlien.y + theAlien.height))
        	           {
        	           	
        	           	if (theMissile.x > theAlien.x &&
        	               theMissile.x < (theAlien.x + theAlien.width))
        	             {
        	             	missiles.splice(m, 1);
        	             	aliens.splice(a, 1);
        	             	removeChild(theMissile);
        	                 theAlien.visible = false;
        	             }
        	           }
        	       }	
        	    }
        	
            if (leftDown)
                ship.x = ship.x - 9; 
                
            if (rightDown)
                ship.x = ship.x + 9;
                
            if (ship.x < 0)
                ship.x = 0;
                
            if (ship.x > 400)
                ship.x = 400;
        }
        
        public function launchMissile():void {
        		var launchMissile:Sprite = new Sprite();
        		addChild(launchMissile);
        		launchMissile.graphics.beginFill(0x00FF00);
        		launchMissile.graphics.drawRect(0,0,2,10);
        		launchMissile.graphics.endFill();
        		launchMissile.x = ship.x;
        		launchMissile.y = ship.y;
        		missiles.push(launchMissile);
        		TweenLite.to(launchMissile, 1, {y:-30,onComplete:missileDone, onCompleteParams:[launchMissile]});        
        }
        
        public function missileDone(missile:Sprite):void {
           removeChild(missile);
           for (var i:int = 0; i < missiles.length; i++) {
               if (missiles[i] == missile) { 
                   missiles.splice(i, 1);
                   break;
               }
           }
        }
        
        private var leftDown:Boolean = false;
        private var rightDown:Boolean = false;
        
        private function keyUp(e:KeyboardEvent):void {
            if (e.keyCode == Keyboard.RIGHT)
                rightDown = false;            
            if (e.keyCode == Keyboard.LEFT)
                leftDown = false; 
     
        }
        
        private function keyDown(e:KeyboardEvent):void {
            if (e.keyCode == Keyboard.RIGHT)
                rightDown = true;
            if (e.keyCode == Keyboard.LEFT)
                leftDown = true;
            if (e.keyCode == Keyboard.SPACE)
                launchMissile();
        }
        
        private function createGame():void {
        	   aliens = new Array();
        	   
        	   for (var i:Number = 0; i < 10; i++) {
        	      var s:Sprite = createAlien();
        	      aliens.push(s);
        	      
        	      if (i < 5) {
        	          s.x = 15 + i * 90;
        	          s.y = 40;
        	      } else {
        	          s.x = 15 + (i-5) * 90;
        	          s.y = 120;
        	      }
        	          s.scaleX = .75
        	      addChild(s);
        	   }
        	   
        	   s = createShip();
        	   addChild(s);
        	   s.x = 200;
        	   s.y = 400;
        	   ship = s;
        	   
        	   // Extra Lives
        	   s = createShip();
        	   addChild(s);
        	   s.x = 55;
        	   s.y = 5;
        	   s.scaleX = .60
        	   s.scaleY = .60
        	   
        	   s = createShip();
        	   addChild(s);
        	   s.x = 15;
        	   s.y = 5;
        	   s.scaleX = .60
        	   s.scaleY = .60
        	   
        	  
        	   // Scan lines
        	   s = new Sprite();
        	   addChild(s);
        	   s.graphics.lineStyle(1, 0x000000, .2);
        	   
        	   for (i=0; i < 465; i+=2) {
        	   	 s.graphics.moveTo(0,i);
        	     s.graphics.lineTo(465,i);
        	   }
        	   
        	   addEventListener("enterFrame", playGame);
        	   stage.addEventListener("keyDown", keyDown);
        	   stage.addEventListener("keyUp", keyUp);
            /*addChild(createAlien());
            var s:Sprite = createShip();
            s.y = 400;
            addChild(s);*/
        }
        
        //--------------------------
        
        private var alienLoader:Loader;
        private var shipLoader:Loader;
        private var alienLoaded:Boolean = false;
        private var shipLoaded:Boolean = false;
        
        private function checkReady():void {
            if (alienLoaded && shipLoaded)
                createGame();
        }
        
        private function loadImageData():void {
            alienLoader = new Loader();
            alienLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onAlienComplete);
            alienLoader.load(new URLRequest("http://www.justsuppose.com/Alien.png"));
        
            shipLoader = new Loader();
            shipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onShipComplete);
            shipLoader.load(new URLRequest("http://www.justsuppose.com/Ship.png"));
        }
    
        private function onAlienComplete(e:Event):void {
            alienLoaded = true;
            checkReady();
        }
        
        private function onShipComplete(e:Event):void {
            shipLoaded = true;
            checkReady();
        }
        
        private function createAlien():Sprite {
        
            var bmp:Bitmap = new Bitmap(Bitmap(alienLoader.content).bitmapData.clone());
            addChild(bmp);
            var s:Sprite = new Sprite();
            s.addChild(bmp);
            return s;
        }
        
        private function createShip():Sprite {
        
            var bmp:Bitmap = new Bitmap(Bitmap(shipLoader.content).bitmapData.clone());
            addChild(bmp);
            var s:Sprite = new Sprite();
            s.addChild(bmp);
            return s;
        }
    }
    
    
}