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

code on 2009-1-3

// write as3 code here..
package {
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    public class A extends Sprite {
        public var txt:TextField = new TextField();
        private var key:Key = new Key(stage, txt);
        public var ship:Ship = new Ship(230,450);
        private var enemy:Enemy = new Enemy();
        public var bullets:Array = new Array(500);
        function A() {
            this.z= 300;
            this.rotationX = -30;
            a = this;
            addChild(enemy);
            enemy.init(230,50,0);
            for(var i:int = 0; i < 500;i++){
                bullets[i] = new Bullet();
                addChild(bullets[i]);
            }
            txt.width = 100;
            txt.height = 100;
            addChild(ship);
            addChild(txt);
            addEventListener("enterFrame",onEnter);
        }
        private function onEnter(e:Event):void {
            enemy.move();
            ship.move();
            for each (var b:Bullet in bullets){
                if(b.visible) {
                    b.move();
                }
            }
        }
        public function addBulletN(x:Number, y:Number, rad:Number,n:int, r:Number):void {
            for (var i:int = 0; i < n; i++) {
                addBullet(x,y,rad+(i-n/2.0)*r);
            }
        }
        public function addBullet(x:Number, y:Number, rad:Number):void {
            for each (var b:Bullet in bullets) {
                if(b.visible) continue;
                b.init(x,y,rad);
                return;
            }
        }
    }
}

import flash.events.*;
import flash.display.*;
import flash.text.*;

var a:A;

class Ship extends Sprite{
    private var speed:Number = 10;
    public function Ship(x:Number, y:Number) {
        this.x = x;
        this.y = y;
        graphics.beginFill(0xffff0000);
        graphics.drawRect(-5,-5,10,10);
        graphics.endFill();
    }
    public function move():void {
        if(Key["left" ])x -= speed;
        if(Key["right"])x += speed;
        if(Key["up"   ])y -= speed;
        if(Key["down" ])y += speed;
        if(x < 0) x = 5; else if(x > 465) x = 465 - 5;
        if(y < 0) y = 5; else if(y > 465) y = 465 - 5;

    }
}

class Enemy extends Sprite{
    private var speed:Number = 5;
    private var rad:Number = 0;
    private var r:Number = 0.05;
    public function Enemy() {
        graphics.beginFill(0xffff0000);
        graphics.drawRect(-5,-5,10,10);
        graphics.endFill();
        visible = false;
    }
    public function init(x:Number, y:Number, rad:Number):void {
        this.x = x;
        this.y = y;
        this.rad = rad = Math.atan((y-a.ship.y)/(x-a.ship.x));
        visible = true;
        move = move1;
    }
    public var move:Function;
    private function moveBody():Number {
        var rad2:Number = Math.atan2(a.ship.y-y, a.ship.x-x);
        if(normalRad(rad-rad2)<0)
            rad += r else rad -= r;
        rad = normalRad(rad);
        x += Math.cos(rad)*speed;
        y += Math.sin(rad)*speed;
        if(x < 0) x = 5; else if(x > 465) x = 465 - 5;
        if(y < 0) y = 5; else if(y > 465) y = 465 - 5;
        return rad2;
    }
    private var nextMove:Function;
    private var time:int;
    private function wait(w:int,next:Function):void {
        time = w;
        move = moveWait;
        nextMove = next;
    }
    public function moveWait():void {
        moveBody();
        time -= 1;
        if(time <= 0) {
            move = nextMove;
        }
    }
    public function move1():void {
        var r:Number = moveBody();
        if(Math.random() > 0.8) {
            a.addBullet(x,y,r);
            if(Math.random() > 0.8) {
                r2=0;
                wait(30,move2);
            }
        }
    }
    private var r2:Number = 0;
    public function move2():void {
        var r:Number = moveBody();
//        if(Math.random() > 0.8) {
            var rr:int = Math.random()*5+1;
            a.addBulletN(x,y,r+r2,rr,0.1);
            r2 = r2+rr*0.1;
            if(Math.random() < 0.03)wait(60,move1);
//        }
    }
}

function normalRad(v:Number):Number {
    return v + 2 * Math.PI * (
        (v >  Math.PI) ? -1 :
        (v < -Math.PI) ? 1  : 0
    );
}

class Bullet extends Sprite{
    private var speed:Number = 10;
    private var rad:Number;
    public function Bullet() {
        graphics.beginFill(0xffff8800);
        graphics.drawRect(-5,-5,10,10);
        graphics.endFill();
        visible = false;
    }
    public function init(x:Number, y:Number, rad:Number):void {
        this.x = x;
        this.y = y;
        this.rad = rad;
        visible = true;
    }
    public function move():Boolean {
        x += Math.cos(rad)*speed;
        y += Math.sin(rad)*speed;
        if(x < 0 || 465 < x || y < 0 || 465 < y) visible = false;
        return visible ;
    }
}

class Key {
    function Key(stage:Stage,txt:TextField) {
        var kmap:Object = {37:"left", 38:"up",39:"right",40:"down",90:"shot"};
        for each(var i:String in kmap) Key[i]=false;
        stage.addEventListener("keyDown",function(e:KeyboardEvent):void {
//            txt.text = ""+e.keyCode + ":"+kmap[e.keyCode];
            if(e.keyCode in kmap) Key[kmap[e.keyCode]] = true;
        });
        stage.addEventListener("keyUp",function(e:KeyboardEvent):void {
            if(e.keyCode in kmap) Key[kmap[e.keyCode]] = false;
        });
    }
}