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

せろはんいんべーだーうじゃうじゃ

ゲーム製作中
十字キーでUFO(旗艦)を操作、インベーダー編隊がうじゃうじゃ追従します
使ってない変数とか保留したスクリプトが残っていたりしますがご了承ください
ようわからん。
Press key "LEFT","RIGHT","UP" and "DOWN"
auther maxcaffy
========
実物を見たことはないのですが、昔のゲーセンに置かれていた白黒スペース
インベーダーにはセロハンが貼られて、疑似カラーになっていたそうですね。
そんなイメージです。
forker matacat
========
Get Adobe Flash player
by matacat 06 Apr 2010
// forked from maxcaffy's インベーダーうじゃうじゃ掃射(製作中)
// forked from maxcaffy's インベーダーうじゃうじゃ

//ゲーム製作中
//十字キーでUFO(旗艦)を操作、インベーダー編隊がうじゃうじゃ追従します
//使ってない変数とか保留したスクリプトが残っていたりしますがご了承ください
//ようわからん。
//Press key "LEFT","RIGHT","UP" and "DOWN"
//auther maxcaffy

//========
// 実物を見たことはないのですが、昔のゲーセンに置かれていた白黒スペース
// インベーダーにはセロハンが貼られて、疑似カラーになっていたそうですね。
// そんなイメージです。
// forker matacat
//========

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.text.TextField;
    import flash.ui.Keyboard;
    
    [SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "0x000000")]
    
    public class InvaderUJA extends Sprite
    {
        // インベーダーたちの列数、総数、間隔
        private const ROWS:int   = 24;
        private const TOTAL:int  = ROWS * 16;
        private const MARGIN:int = 4;
        
        // 矢印キーのフラグ
        public static var fkUp:Boolean    = false;
        public static var fkDown:Boolean  = false;
        public static var fkLeft:Boolean  = false;
        public static var fkRight:Boolean = false;
        
        // ステージの幅、高さ、キャラクターたちを配置するためのスプライト
        private var wz:int;
        private var hi:int;
        private var display:Sprite;
        
        // キャラクターたちと Z キーフラグ
        private var cannon:Cannon;
        private var ufo:Ufo;
        private var invader:Invader;
        private var fkZ:Boolean;
        
        public function InvaderUJA()
        {
            wz = stage.stageWidth;
            hi = stage.stageHeight;
            
            // セロハン
            var g:Graphics  = graphics;
            g.beginFill(0x400000);
            g.drawRect(0, 0, wz, hi / 6);
            g.beginFill(0x404000);
            g.drawRect(0, hi / 6, wz, hi / 6);
            g.beginFill(0x004000);
            g.drawRect(0, hi / 3, wz, hi / 6);
            g.beginFill(0x004040);
            g.drawRect(0, hi / 2, wz, hi / 6);
            g.beginFill(0x000040);
            g.drawRect(0, hi / 3 * 2, wz, hi / 6);
            g.beginFill(0x400040);
            g.drawRect(0, hi / 6 * 5, wz, hi / 6);
            cacheAsBitmap = true;
            
            display           = new Sprite();
            display.blendMode = "add";
            addChild(display);
            
            initFunc();
            initAdd();
        }
        
        private function initFunc():void
        {
            // 凸
            cannon   = new Cannon();
            cannon.x = wz - Cannon.WIDTH >> 1;
            cannon.y = wz - 20;
            display.addChild(cannon);
            
            // 冊
            ufo   = new Ufo();
            ufo.x = wz - Ufo.WIDTH >> 1;
            ufo.y = 7;
            display.addChild(ufo);
            
            // 再
            var ofsX:int  = wz - ROWS * (Invader.WIDTH + MARGIN) >> 1;
            var ofsY:int  = 20;
            var tempX:int = 0;
            var tempY:int = 0;
            
            // インベーダーインスタンスはそれ自体が連結リストになっている
            var i:Invader = invader = new Invader(ufo, cannon, wz, hi);
            i.x = tempX + ofsX;
            i.y = tempY + ofsY;
            display.addChild(i);
            
            for (var j:int = 1; j < TOTAL; j++) {
                if (j % ROWS == 0) {
                    tempX  = 0;
                    tempY += Invader.HEIGHT + MARGIN;
                } else {
                    tempX += Invader.WIDTH + MARGIN;
                }
                
                i   = i.next = new Invader();
                i.x = tempX + ofsX;
                i.y = tempY + ofsY;
                display.addChild(i);
            }
        }
        
        private function initAdd():void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunc);
            stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunc);
        }
        
        private function keyDownFunc(event:KeyboardEvent):void
        {
            switch (event.keyCode) {
                case Keyboard.UP:    fkUp    = true; break;
                case Keyboard.DOWN:  fkDown  = true; break;
                case Keyboard.LEFT:  fkLeft  = true; break;
                case Keyboard.RIGHT: fkRight = true; break;
                case 90:  // Z
                    if (!fkZ) {
                        fkZ = true;
                        for (var i:Invader = invader; i; i = i.next) if(i.standBy) i.fire();
                    }
                    break;
            }
        }
        
        private function keyUpFunc(event:KeyboardEvent):void
        {
            switch (event.keyCode) {
                case Keyboard.UP:    fkUp    = false; break;
                case Keyboard.DOWN:  fkDown  = false; break;
                case Keyboard.LEFT:  fkLeft  = false; break;
                case Keyboard.RIGHT: fkRight = false; break;
                case 90:  // Z
                    if (fkZ) fkZ = false;
                    break;
            }
        }
    }
}


import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;

const WHITE:uint = 0xFFC0C0C0;

/*---------------------------------------------------------------------
文字列でできたデータを読んで BitmapData を返すユーティリティ
---------------------------------------------------------------------*/
function drawBmp(data:String, width:int, height:int, color:uint):BitmapData
{
    if (data.length != width * height) throw new Error("ビットマップデータの定義が不正です。");
    
    var bmd:BitmapData = new BitmapData(width, height, true, 0);
    
    for (var j:int = 0; j < height; j++) {
        for (var i:int = 0; i < width; i++) {
            if (data.charAt(i + j * width) != " ") bmd.setPixel32(i, j, color);
        }
    }
    
    return bmd;
}

/*---------------------------------------------------------------------
キャラクターの基本クラス
サブクラスは init():void と update(e:Event):void をオーバーライドする
init() は addChild() するときのみ実行される初期化メソッド。コンストラクタとは区別すること
update() は ENTER_FRAME のたびに実行される更新メソッド。移動・アニメーションを定義する
---------------------------------------------------------------------*/
class AllYourBase extends Bitmap
{
    public function AllYourBase()
    {
        addEventListener(Event.ADDED_TO_STAGE, added);
    }
    
    protected function added(e:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, added);
        init();
        addEventListener(Event.REMOVED_FROM_STAGE, removed);
        addEventListener(Event.ENTER_FRAME, update);
    }
    
    protected function removed(e:Event):void
    {
        removeEventListener(Event.REMOVED_FROM_STAGE, removed);
        removeEventListener(Event.ENTER_FRAME, update);
        addEventListener(Event.ADDED_TO_STAGE, added);
    }
    
    protected function init():void
    {
    }
    
    protected function update(e:Event):void
    {
    }
}

/*---------------------------------------------------------------------
凸クラス(いわゆる自機)
---------------------------------------------------------------------*/
class Cannon extends AllYourBase
{
    public static const WIDTH:int  = 13;
    public static const HEIGHT:int = 8;
    
    private static var bmd:BitmapData = drawBmp(
          "      X      "
        + "     XXX     "
        + "     XXX     "
        + " XXXXXXXXXXX "
        + "XXXXXXXXXXXXX"
        + "XXXXXXXXXXXXX"
        + "XXXXXXXXXXXXX"
        + "XXXXXXXXXXXXX"
    , WIDTH, HEIGHT, WHITE);
    
    private var goto:int;
    
    public function Cannon():void
    {
        super();
    }
    
    override protected function init():void
    {
        goto       = stage.stageWidth * Math.random();
        bitmapData = bmd;
    }
    
    override protected function update(e:Event):void
    {
        var distance:int = goto - this.x;
        
        this.x += distance / 16;
        
        if ((distance > 0 ? distance : -distance) < 10) {
            goto = stage.stageWidth * Math.random();
        }
    }
}

/*---------------------------------------------------------------------
インベーダークラス(ぴこぴこ動く)
---------------------------------------------------------------------*/
class Invader extends AllYourBase
{
    public static const WIDTH:int  = 11;
    public static const HEIGHT:int = 8;
    
    private static const INTERVAL:int = 10;
    
    private static var bmd:Vector.<BitmapData> = Vector.<BitmapData>([
        drawBmp(
              "  X     X  "
            + "X  X   X  X"
            + "X XXXXXXX X"
            + "XXX XXX XXX"
            + "XXXXXXXXXXX"
            + " XXXXXXXXX "
            + "  X     X  "
            + " X       X "
        , WIDTH, HEIGHT, WHITE),
        
        drawBmp(
              "  X     X  "
            + "   X   X   "
            + "  XXXXXXX  "
            + "XXX XXX XXX"
            + "XXXXXXXXXXX"
            + "X XXXXXXX X"
            + "X X     X X"
            + "   XX XX   "
        , WIDTH, HEIGHT, WHITE)
    ]);
    
    private static var owner:Ufo;
    private static var wz:int;
    private static var hi:int;
    
    public var standBy:Boolean;
    public var next:Invader;
    
    private var beam:Beam;
    private var vX:Number;
    private var vY:Number;
    private var initX:int;
    private var initY:int;
    private var followSpeed:Number;
    private var cnt:int;
    private var phase:int;
    
    public function Invader(
            owner:Ufo       = null,
            target:Cannon   = null,
            stageWidth:int  = -1,
            stageHeight:int = -1   ):void
    {
        super();
        
        if (owner) {
            Invader.owner = owner;
            Invader.wz    = stageWidth - WIDTH;
            Invader.hi    = stageHeight - HEIGHT;
        }
        beam = new Beam(this, target, stageHeight);
    }
    
    override protected function init():void
    {
        standBy     = true;
        vX          = 0;
        vY          = 0;
        initX       = this.x;
        initY       = this.y;
        followSpeed = 0.1 + 0.9 * Math.random();
        cnt         = 0;
        phase       = 0;
        bitmapData  = bmd[0];
    }
    
    override protected function update(e:Event):void
    {
        // 移動
        vX += initX + owner.dX - this.x - vX * 0.0625;
        vY += initY + owner.dY - this.y - vY * 0.0625;
        
        this.x += vX * followSpeed;
        this.y += vY * followSpeed;
        
        if      (this.x < 0)  this.x = 0;
        else if (this.x > wz) this.x = wz;
        
        if      (this.y < 0)  this.y = 0;
        else if (this.y > hi) this.y = hi;
        
        // アニメーション
        if (++cnt >= INTERVAL) {
            cnt        = 0;
            phase      = phase == 0 ? 1 : 0;
            bitmapData = bmd[phase];
        }
    }
    
    public function fire():void
    {
        standBy = false;
        beam.x  = this.x + 1;
        beam.y  = this.y;
        parent.addChild(beam);
    }
}

/*---------------------------------------------------------------------
UFOクラス(旗艦)
---------------------------------------------------------------------*/
class Ufo extends AllYourBase
{
    public static const WIDTH:int  = 16;
    public static const HEIGHT:int = 7;
    
    private static const MAX_SPEED:Number = 10;
    private static const R:Number         = 0.95;
    
    private static var bmd:BitmapData = drawBmp(
          "     XXXXXX     "
        + "   XXXXXXXXXX   "
        + "  XXXXXXXXXXXX  "
        + " XX XX XX XX XX "
        + "XXXXXXXXXXXXXXXX"
        + "  XXX  XX  XXX  "
        + "   X        X   "
    , WIDTH, HEIGHT, WHITE);
    
    public var dX:int;
    public var dY:int;
    
    private var vX:Number;
    private var vY:Number;
    private var initX:int;
    private var initY:int;
    private var wz:int;
    private var hi:int;
    
    public function Ufo():void
    {
        super();
    }
    
    override protected function init():void
    {
        vX         = 0;
        vY         = 0;
        initX      = this.x;
        initY      = this.y;
        wz         = stage.stageWidth - WIDTH;
        hi         = stage.stageHeight - HEIGHT;
        bitmapData = bmd;
    }
    
    override protected function update(e:Event):void
    {
        if (Math.sqrt(vX * vX + vY * vY) < MAX_SPEED) {
            if (InvaderUJA.fkUp) {
                if (InvaderUJA.fkLeft) {
                    vX -= 0.7071;
                    vY -= 0.7071;
                } else if (InvaderUJA.fkRight) {
                    vX += 0.7071;
                    vY -= 0.7071;
                } else {
                    vY -= 1;
                }
            } else if (InvaderUJA.fkDown) {
                if (InvaderUJA.fkLeft) {
                    vX -= 0.7071;
                    vY += 0.7071;
                } else if (InvaderUJA.fkRight) {
                    vX += 0.7071;
                    vY += 0.7071;
                } else {
                    vY += 1;
                }
            } else if (InvaderUJA.fkLeft) {
                vX -= 1;
            } else if (InvaderUJA.fkRight) {
                vX += 1;
            }
        }
        
        this.x += vX *= R;
        this.y += vY *= R;
        
        if (this.x < 0 || this.x > wz) this.x += vX *= -1;
        if (this.y < 0 || this.y > hi) this.y += vY *= -1;
        
        dX = this.x - initX;
        dY = this.y - initY;
    }
}

/*---------------------------------------------------------------------
ビームクラス(弾丸) += 衝突エフェクトクラス(爆発)
---------------------------------------------------------------------*/
class Beam extends AllYourBase
{
    public static const WIDTH:int  = 9;
    public static const HEIGHT:int = 7;
    
    private static const BEAM_SPEED:int    = 2;
    private static const BEAM_INTERVAL:int = 1;
    private static const EXP_INTERVAL:int  = 2;
    
    private static var beamData:Vector.<BitmapData> = Vector.<BitmapData>([
        drawBmp(
              "    X    "
            + "     X   "
            + "    X    "
            + "   X     "
            + "    X    "
            + "     X   "
            + "    X    "
        , WIDTH, HEIGHT, WHITE),
        
        drawBmp(
              "     X   "
            + "    X    "
            + "   X     "
            + "    X    "
            + "     X   "
            + "    X    "
            + "   X     "
        , WIDTH, HEIGHT, WHITE),
        
        drawBmp(
              "    X    "
            + "   X     "
            + "    X    "
            + "     X   "
            + "    X    "
            + "   X     "
            + "    X    "
        , WIDTH, HEIGHT, WHITE),
        
        drawBmp(
              "   X     "
            + "    X    "
            + "     X   "
            + "    X    "
            + "   X     "
            + "    X    "
            + "     X   "
        , WIDTH, HEIGHT, WHITE)
    ]);
    
    private static var expData:Vector.<BitmapData> = Vector.<BitmapData>([
        drawBmp(
              "         "
            + "         "
            + "         "
            + "         "
            + "         "
            + "    X    "
            + "   X X   "
        , WIDTH, HEIGHT, WHITE),
        
        drawBmp(
              "         "
            + "         "
            + "         "
            + "         "
            + "   XXX   "
            + "  X   X  "
            + "  X X X  "
        , WIDTH, HEIGHT, WHITE),
        
        drawBmp(
              "         "
            + "         "
            + "         "
            + "   XXX   "
            + "  X   X  "
            + " X  X  X "
            + " X X X X "
        , WIDTH, HEIGHT, WHITE),
        
        drawBmp(
              "         "
            + "   X X   "
            + " X     X "
            + "         "
            + "X  X X  X"
            + "         "
            + "X X   X X"
        , WIDTH, HEIGHT, WHITE)
    ]);
    
    private static var target:Cannon;
    private static var stageBottom:int;
    private static var testTop:int;
    private static var testBottom:int;
    
    private var owner:Invader;
    private var fHit:Boolean;
    private var cnt:int;
    private var phase:int;
    
    public function Beam(owner:Invader, target:Cannon = null, stageHeight:int = -1):void
    {
        super();
        
        if (target) {
            Beam.target      = target;
            Beam.stageBottom = stageHeight - HEIGHT;
            Beam.testTop     = stageBottom - 20;
            Beam.testBottom  = testTop + HEIGHT + Cannon.HEIGHT;
        }
        
        this.owner = owner;
    }
    
    override protected function init():void
    {
        fHit            = false;
        cnt             = 0;
        phase           = 0;
        bitmapData = beamData[0];
    }
    
    override protected function update(e:Event):void
    {
        if (!fHit) {
            // 移動
            this.y += BEAM_SPEED;
            
            // あたり判定・アニメーション
            if (this.y >= stageBottom
             || this.y >= testTop
             && this.y <= testBottom
             && this.x >= target.x - 4
             && this.x <= target.x - 4 + Cannon.WIDTH) {
                fHit       = true;
                cnt        = 0;
                phase      = 0;
                bitmapData = expData[0];
            } else {
                if (++cnt >= BEAM_INTERVAL) {
                    cnt        = 0;
                    phase      = phase < 3 ? phase + 1 : 0;
                    bitmapData = beamData[phase];
                }
            }
        } else {
            if (++cnt >= EXP_INTERVAL) {
                if (phase < 3) {
                    cnt = 0;
                    phase++;
                    bitmapData = expData[phase];
                } else {
                    parent.removeChild(this);
                    owner.standBy = true;
                }
            }
        }
    }
}