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: ゲームっぽくしてみた

ゲームっぽくしてみた
十字キーで移動
 スペースキーで弾発射
だれか当たり判定つけて><
// forked from ton's ゲームっぽくしてみたforked from: なんとかディウスっぽい背景
// forked from gyuque's なんとかディウスっぽい背景
// ゲームっぽくしてみた
// 十字キーで移動
// スペースキーで弾発射
// だれか当たり判定つけて><

package
{
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	import flash.ui.Keyboard;

	[SWF(width="320", height="240", backgroundColor="0x000000", frameRate="30")]  
	public class Nemesis extends Sprite
	{
		public static const W:int = 320;
		public static const H:int = 240;

                public static var KeyMapping:Object = {};

		private var mScroll:int = 0;
		private var mMyPos:Point;
		private var mBGen:MountainGen = new MountainGen(160);
		private var mTGen:MountainGen = new MountainGen(160, 1);
		private var mBBGen:MountainGen = new MountainGen(180, 1, true);

		private var mBBGScreen:BitmapData = new BitmapData(W, H, true, 0);
		private var mBBGScreenBmp:Bitmap;
		private var mBBGScreenBmp2:Bitmap;

		private var mBGScreen:BitmapData = new BitmapData(W, H, true, 0);
		private var mBGScreenBmp:Bitmap;
		private var mBGScreenBmp2:Bitmap;
		private var mStarbg:StarBG = new StarBG(W, H, 30);
		
		private var ship:Ship = new Ship(0x00ffff, true);
                private var mKeyState:KeyState = new KeyState();

		function Nemesis()
		{
			mapKey(Keyboard.UP, KeyState.K_UP);
			mapKey(Keyboard.DOWN, KeyState.K_DOWN);
			mapKey(Keyboard.LEFT,KeyState.K_LEFT);
			mapKey(Keyboard.RIGHT, KeyState.K_RIGHT);
			mapKey(Keyboard.SPACE, KeyState.K_TRG1);

                        setupBG();
                        setupShip();

			addEventListener(Event.ENTER_FRAME, tick);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
		}

                private function setupShip():void {
			ship.x = 40;
			ship.y = H / 2;

                        var m1:Ship = new Multiple();
                        ship.appendMultiple(m1);

                        var m2:Ship = new Multiple();
                        ship.appendMultiple(m2);

                        ship.fillMoveBuffer();           

                        addChild(m1);
                        addChild(m2);
			addChild(ship);
                }

                private function setupBG():void {
			addChild(mStarbg);

			mBBGScreenBmp = new Bitmap(mBBGScreen);
			addChild(mBBGScreenBmp);
			mBBGScreenBmp2 = new Bitmap(mBBGScreen);
			addChild(mBBGScreenBmp2);

			mBGScreenBmp = new Bitmap(mBGScreen);
			addChild(mBGScreenBmp);
			mBGScreenBmp2 = new Bitmap(mBGScreen);
			addChild(mBGScreenBmp2);

			var i:int;
			mBGen.step();
			mTGen.step();
			mBBGen.step();
			for (i = 0;i < W;i++) {
				mBGen.step();
				mTGen.step();
				mBGen.draw(mBGScreen, i, 80);
				mTGen.draw(mBGScreen, i, 0, false);

				mBBGen.step();
				mBBGen.draw(mBBGScreen, i, 60);
			}
                }

                private static function mapKey(raw:uint, _to:uint):void {
                    KeyMapping[raw] = _to;   
                }
		
		private var _clrrc:Rectangle = new Rectangle(0, 0, 1, H);
		private function tick(e:Event):void
		{
			
			var ox:int = mScroll % W;
			var ox2:int = int(mScroll/2) % W;
			_clrrc.x = ox;
			mBGScreen.fillRect(_clrrc, 0);

			mStarbg.tick();
			mBGen.step();
			mTGen.step();

			mStarbg.draw();
			mBGen.draw(mBGScreen, ox, 80);
			mTGen.draw(mBGScreen, ox, 0, false);

                        if ((mScroll%2)==1) {
    			    _clrrc.x = ox2;
			    mBBGScreen.fillRect(_clrrc, 0);
    			    mBBGen.step();
			    mBBGen.draw(mBBGScreen, ox2, 60);
                        }

			mBGScreenBmp.x = -ox;
			mBGScreenBmp2.x = W-ox;

			mBBGScreenBmp.x = -ox2;
			mBBGScreenBmp2.x = W-ox2;

			ship.moveByKeyState(mKeyState.state);
			ship.tick();
                        ship.draw();

			mScroll++;
		}
		
		private function onKeyDownHandler(e:KeyboardEvent):void {
                        if (KeyMapping.hasOwnProperty(e.keyCode))
                            mKeyState.setState(KeyMapping[e.keyCode]);
		}
		private function onKeyUpHandler(e:KeyboardEvent):void 
		{
                        if (KeyMapping.hasOwnProperty(e.keyCode))
                            mKeyState.delState(KeyMapping[e.keyCode]);
		}		
	}
}

import flash.display.*;
import flash.events.Event;
import flash.geom.Point;

class KeyState
{
    public static const K_UP:uint    = 0x01;
    public static const K_DOWN:uint  = 0x02;
    public static const K_LEFT:uint  = 0x04;
    public static const K_RIGHT:uint = 0x08;
    public static const K_TRG1:uint  = 0x10;

    private var mState:uint = 0;

    public function setState(s:uint):void {
        mState |= s;
    }

    public function delState(s:uint):void {
        mState &= ~s;
    }

    public function get state():uint {
        return mState;
    }
}

class Ship extends Sprite
{
	private var speed:int = 4;
        protected var mCount:int = 0;
        private var mMovBuf:PositionRingBuffer;
        private var mMultiples:Array;

	public var color:uint;
	function Ship(color:uint, enable_buf:Boolean = false)
	{
	    this.color = color;
            draw();

            if (enable_buf)
                mMovBuf = new PositionRingBuffer(100);
	}

        public function fillMoveBuffer():void {
            for (var i:int = 0;i < 100;i++)
                mMovBuf.push(x, y, false);
        }

        public function appendMultiple(m:Ship):void
        {
            if (!mMultiples) mMultiples = [];

            mMultiples.push(m);
        }

        public function draw():void
        {
            var g:Graphics = graphics;
            g.clear();
	    g.lineStyle(0, color);
	    g.lineTo( -30, -10);
	    g.lineTo( -30, 10);
	    g.lineTo(0, 0);
	    g.endFill();

            if (mMultiples) {
                for each(var m:Ship in mMultiples)
                    m.draw();
            }
        }

        public function moveByKeyState(s:uint):void
        {
		if (s&KeyState.K_UP)         y -= speed;
		else if (s&KeyState.K_DOWN)  y += speed;
		if (s&KeyState.K_LEFT)       x -= speed;
		else if (s&KeyState.K_RIGHT) x += speed;

		if (s&KeyState.K_TRG1)
                    fireShot();

		if (x - width< 0) x = width;
			else if (x > Nemesis.W) x = Nemesis.W;

		if (y - height/2 < 0) y = height/2;
			else if (y + height/2 > Nemesis.H) y = Nemesis.H - height/2; 


                mMovBuf.push(x, y);
        }

        public function tick():void {
            if (mMultiples) {
                var len:int = mMultiples.length;
                for (var i:int = 0;i < len;i++) {
                    mMultiples[i].x = mMovBuf.getX((i+1)*8) - 10;
                    mMultiples[i].y = mMovBuf.getY((i+1)*8);
                }

                for each(var m:Ship in mMultiples)
                    m.tick();
            }

            mCount++;
         }

        public function fireShot():void {
		if (!(mCount%5)) {
			var bullet:Bullet = new Bullet();
			bullet.x = x;
			bullet.y = y;
			parent.addChild(bullet);
		}

            if (mMultiples) {
                for each(var m:Ship in mMultiples)
                    m.fireShot();
            }
        }
}

class Multiple extends Ship
{
    import flash.geom.Matrix;

    public static const GRAD_COLORS:Array = [0xffba33, 0xaa0000];
    public static const GRAD_ALPHAS:Array = [1, 1];
    public static const GRAD_RATIOS:Array = [100, 255];
    private var mGradTrans:Matrix = new Matrix();

    function Multiple()
    {
        super(0);
    }

    public override function draw():void
    {
        var g:Graphics = graphics;
        var r:Number = Math.sin(mCount*0.9)*0.7 + 9;

        mGradTrans.createGradientBox(r*2, r*1.8,0 , -r*1.1, -r*0.9);
        g.clear();
        g.beginGradientFill(GradientType.RADIAL, GRAD_COLORS, GRAD_ALPHAS, GRAD_RATIOS, mGradTrans);
        g.drawEllipse(-r, -r*0.8, r*2, r*1.6);
	g.endFill();
    }
}
class PositionRingBuffer
{
    private var xs:Array;
    private var ys:Array;
    private var length:uint;
    private var pos:int = 0;

    function PositionRingBuffer(len:uint)
    {
        length = len;
        xs = new Array(len);
        ys = new Array(len);
    }

    

    public function push(x:int, y:int, chk:Boolean = true):void {
        var i:int = (pos + length - 1) % length;
        if (chk) {
            if (xs[i] == x && ys[i] == y) return;
        }

        xs[pos] = x;
        ys[pos] = y;

        pos = ++pos % length;
    }

    public function getX(i:int):int {
        i = (pos - i + length) % length;
        return xs[i];
    }

    public function getY(i:int):int {
        i = (pos - i + length) % length;
        return ys[i];
    }
}

class Bullet extends Sprite
{
	public var color:uint;
	public var size:uint;
	public var speed:uint;
	function Bullet(color:uint = 0xeeddaa, size:uint = 3, speed:uint = 20)
	{
		this.color = color;
		this.size = size;
		this.speed = speed;
		
		this.graphics.beginFill(color);
		this.graphics.drawEllipse(-size, -size/2, size*1.8, size);
		this.graphics.drawCircle(-size*1.3, 0, size/2);
		this.graphics.drawCircle(-size*1.8, 0, size/3);
		this.graphics.endFill();
		
		addEventListener(Event.ENTER_FRAME, updateHandler);
	}
	
	private function updateHandler(e:Event):void 
	{
		this.x += speed;
		
		if (this.x + size / 2 > Nemesis.W) {
			this.parent.removeChild(this);
			removeEventListener(Event.ENTER_FRAME, updateHandler);
		}
	}
}


class MountainGen
{
	private var mPrevBuffer:Array;
	private var mHeight:int;
	private var mCount:int = 0;

	private var tmpBuffer:Array;
	private var mGenFunc:Function;
        private var mDark:Boolean;
	function MountainGen(h:int, generator:int = 0, dark:Boolean = false)
	{
                mDark = dark;
		mHeight = h;
		mGenFunc = generator ? genWav2 : genWav;
		mPrevBuffer = new Array(h);
		tmpBuffer   = new Array(h);
	}

	public function draw(b:BitmapData, x:int, y:int, rev:Boolean = true):void
	{
		var i:int;
		for (i = 0;i < mHeight;i++) {
			if (tmpBuffer[i]) {
				var c:int = tmpBuffer[i];
				b.setPixel32(x, rev ? (y+mHeight-i) : (y+i), makeColor(c));
			}

		}		
	}

        private function makeColor(c:int):uint
        {
            if (mDark)
                return 0xff000000 | (c/7+5) | ((c/7 + 80)<<16) | ((c/6+50) << 8);
            return 0xff000000 | (c/3+11) | ((c/5 + 170)<<16) | ((c/2+60) << 8);
        }

	public function step():void
	{
		var t:Number = Number(mCount) * 0.02;
		var h:int = mGenFunc(t) * mHeight;
		var i:int, k:int, m:int;

		for (i = 0;i < mHeight;i++) {
			mPrevBuffer[i] = tmpBuffer[i];
		}
		
		for (i = 0;i < mHeight;i++) {
			tmpBuffer[i] = (i < h) ? (Math.random()*80 + 80) : 0;

			if (tmpBuffer[i]) {
				if (mPrevBuffer[i])
					tmpBuffer[i] = (tmpBuffer[i] + mPrevBuffer[i]*7)/8;

				if (mPrevBuffer[i] == 0 || i == (h-1)) {
					m = 50;
					for (k = i;k >= 0 && m > 0;k--, m-=4) {
						if (m > 33) m--;
						tmpBuffer[k] += m;

						m += Math.random()*7;
					}
				}

				if (mPrevBuffer[i+1] && i == (h-1)) {
					m = -48;
					for (k = i;k >= 0 && m < 0;k--, m++) {
						tmpBuffer[k] += m;
						if (tmpBuffer[k]<1) tmpBuffer[k] = 1;
					}
				}

		   }
		}

	   for (i = 0;i < mHeight;i++)
			tmpBuffer[i] = (tmpBuffer[i] < 0) ? 0 : (tmpBuffer[i] > 255) ? 255 : tmpBuffer[i];

		mCount++;
	}

	private static function genWav(t:Number, nest:int = 0):Number
	{
		var v:Number = Math.sin(t);
		v += Math.sin(t*3) * 0.1;
		v += Math.cos(0.1 + t*10) * 0.02;
		v *= Math.sin(t*0.1);

		if (nest < 5)
			v += genWav2(t*2.0, 1+nest)*0.5;

		v = v*0.2 + 0.22;

		return (v<0) ? 0 : (v>1) ? 1 : v;
	}

	private static function genWav2(t:Number, nest:int = 0):Number
	{
		var v:Number = Math.sin(t);
		v += Math.cos(t*3) * 0.1;
		v += Math.cos(0.1 + t*9) * 0.02;
		v *= Math.cos(0.2 + t*0.15);

		if (nest < 5)
			v += genWav2(t*2.0, 1+nest)*0.5;

		v = v*0.2 + 0.23;

		return (v<0) ? 0 : (v>1) ? 1 : v;
	}
}

class StarBG extends Sprite
{
	private var mWidth:int;
	private var mHeight:int;

	private var mStars:Array;
	private var mStarVs:Array;
	private var mN:int;

	function StarBG(w:int, h:int, n:int)
	{
		mWidth = w;
		mHeight = h;
		mN = n;

		mStars  = new Array(n);
		mStarVs = new Array(n);
		for (var i:int = 0;i < n;i++) {
			mStars[i] = new Point(int( Math.random()*w ), int( Math.random()*h ));
			mStarVs[i] = Math.random() + 0.2;
		}
	}

	public function tick():void
	{
		var n:int = mN;

		for (var i:int = 0;i < n;i++) {
			mStars[i].x -= Number(mStarVs[i]);

			if (mStars[i].x < 0) {
				mStars[i].x += mWidth;
				mStars[i].y = int( Math.random()*mHeight );
			}
		}
	}

	public function draw():void
	{
		var g:Graphics = graphics;
		var n:int = mN;

		g.clear();
		for (var i:int = 0;i < n;i++) {
			g.beginFill(0xffffff);
			g.drawCircle( mStars[i].x, mStars[i].y , 0.4);
		}   
	}
}