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

Fish 2D

マウスカーソルを追っかける程度・・・
Get Adobe Flash player
by mousepancyo 03 Jun 2011
/**
 * Copyright mousepancyo ( http://wonderfl.net/user/mousepancyo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/8Q9G
 */

package  {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.system.Security;
    import flash.system.LoaderContext;
    
    [SWF(width=465, height=465, backgroundColor=0xFFFFFF,frameRate=60)]
    
    public class Main extends Sprite
    {

        private const IMG_URL:String = "http://www.digifie.jp/assets/images/fish_bone.png";
        
        private var _loader:Loader;
        private var _bmd:BitmapData;
        
        private var _fish:PolyImage;
        private var _p:PhysicsPoint = new PhysicsPoint(0, 0);
        private var _rot:Number = 0;
        
        public function Main() 
        {
            Security.loadPolicyFile("http://www.digifie.jp/crossdomain.xml");
            loadImage(IMG_URL);
        }
        
        private function loadImage(url:String):void
        {
            var context:LoaderContext = new LoaderContext;
            context.checkPolicyFile = true;
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
            _loader.load(new URLRequest(url), context);
        }
        
        private function onLoaded(e:Event):void
        {
            _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
            _bmd = new BitmapData(_loader.width, _loader.height, true, 0);
            _bmd.draw(_loader);
            //
            _fish = new PolyImage(_bmd);
            addChild(_fish);
            addEventListener(Event.ENTER_FRAME, update);
        }
        
        private function update(e:Event):void
        {
            _p.setAcceleration((mouseX - _p.x) * 5, (mouseY - _p.y) * 5);
            _p.update();
            _fish.x = _p.x;
            _fish.y = _p.y;
            _fish.rotation = _rot;
            _fish.update();
            turn();
        }
        
        
        private function turn(e:Event = null):void
        {
            if(_fish.x < mouseX){
                _rot = Math.atan2(mouseY - _fish.y, mouseX - _fish.x) * 180 / Math.PI;
                if(_fish.rotationY < 180){
                    _fish.rotationY += 5;
                }else{
                    _fish.rotationY = 180;
                    _fish.removeEventListener(Event.ENTER_FRAME, turn);
                }
            }else{
                _rot = Math.atan2(-(mouseY - _fish.y), -(mouseX - _fish.x)) * 180 / Math.PI;
                if(_fish.rotationY > 0){
                    _fish.rotationY -= 5;
                }else{
                    _fish.rotationY = 0;
                    _fish.removeEventListener(Event.ENTER_FRAME, turn);
                }
            }
        }

    }
    
}


// --------------------- PolyImage ---------------------//

//package 
//{
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.geom.Point;

    internal class PolyImage extends Sprite
    {

        private var _vertices:Vector.<Number> = new Vector.<Number>();
        private var _indices:Vector.<int> = new Vector.<int>();
        private var _uvData:Vector.<Number> = new Vector.<Number>();
        private var _pX:Vector.<Number> = new Vector.<Number>();
        private var _pY:Vector.<Number> = new Vector.<Number>();
        private var _pointList:Vector.<Point> = new Vector.<Point>();
        private var _bmd:BitmapData;
        private var _bm:Bitmap;
        private var _shape:Shape = new Shape();
        private var _xLen:int = 3;
        private var _yLen:int = 2;

        public function PolyImage(bmd:BitmapData)
        {
            _bmd = bmd;
            setup();
        }

        private function setup():void
        {
            addChild(_shape);
            _shape.x = -_bmd.width * .5;
            _shape.y = -_bmd.height * .5;
            //
            for (var i:int=0; i < _yLen; i++) {
                for (var j:int=0; j < _xLen; j++) {
                    _uvData.push(j / (_xLen-1), i / (_yLen-1));
                    _vertices.push(_bmd.width * j / (_xLen-1), _bmd.height * i / (_yLen-1));
                    _pX.push(_bmd.width * j / (_xLen-1));
                    _pY.push(_bmd.height * i / (_yLen-1));

                    if (i != (_yLen - 1) && j != (_xLen - 1)) {
                        _indices.push(_xLen * i + j, _xLen * i + j + 1, _xLen * (i + 1) + j);
                        _indices.push(_xLen * i + j + 1, _xLen * (i + 1) + j, _xLen * (i + 1) + j + 1);
                    }
                }
            }

            for (i=0; i < _xLen * _yLen; i++) {
                var p:Point = new Point();
                p.x = _pX[i];
                p.y = _pY[i];
                _pointList.push(p);
            }

            createTriangles(_vertices, _indices, _uvData);
        }

        
        private var _isRev:Boolean;
        private var _ofset:Number = 0;
        private var _acc:Number = 0.9;
        
        public function update():void
        {
            var nX:int = 0;
            var nY:int = 0;
            
            if(_isRev){
                _ofset += 1;
                if(_ofset > 10){
                    _ofset = 10;
                    _isRev = false;
                }
            }else{
                _ofset -= 1;
                if(_ofset < -15){
                    _ofset = -15;
                    _isRev = true;
                }
            }
            
            for (var i:int=0; i < _vertices.length; i++) {
                if (i % 2 != 1) {
                    if (nX < _pointList.length) {
                        var ofsetX:Number;
                        if(nX %2 == 0){
                            ofsetX = _ofset * nX * .5;
                        }else{
                            ofsetX = _ofset * -nX * .5;
                        }
                        //
                        if(nX >= 3) ofsetX *= -1;
                        //
                        _vertices[i] = _pointList[nX].x + ofsetX;
                        nX++;
                    }
                } else {
                    if (nY < _pointList.length) {
                        var ofsetY:Number;
                        if(nY %2 == 0){
                            ofsetY = _ofset;
                        }else{
                            ofsetY = _ofset * -1;
                        }
                        //
                        if(nY >= 3) ofsetY *= -1;
                        //
                        _vertices[i] = _pointList[nY].y + ofsetY;
                        nY++;
                    }
                }
            }

            createTriangles(_vertices, _indices, _uvData);
        }

        private function createTriangles(ver:Vector.<Number>, ind:Vector.<int>, uv:Vector.<Number>, cull:String = "none"):void
        {
            _shape.graphics.clear();
            //_shape.graphics.lineStyle(1, 0xFF0000, 0.5)
            _shape.graphics.beginBitmapFill(_bmd, null, true, true);
            _shape.graphics.drawTriangles(ver, ind, uv, cull);
            _shape.graphics.endFill();
        }

    }
//}


// --------------------- PhysicsPoint ---------------------//
//package
//{
    import flash.geom.Point;
    import flash.utils.Timer;
    import flash.events.Event;

    internal class PhysicsPoint extends Point
    {

        private var _ax:Number, _ay:Number;
        
        public var vx:Number, vy:Number;
        public var resistance:Number;
        public var pastTime:Number;
        
        public function PhysicsPoint(px:Number, py:Number)
        {
            x = px;
            y = py;
            resistance = .9;
            vx = 0;
            vy = 0;
            _ax = 0;
            _ay = 0;
            pastTime = new Date().getTime();
        }
        
        public function update():void
        {
            var now:Number = new Date().getTime();
            var t:Number = (now - pastTime) * .001;
            
            x += vx * t + .5 * _ax * t * t;
            y += vy * t + .5 * _ay * t * t;
            
            vx += _ax * t;
            vy += _ay * t;
            
            vx *= resistance;
            vy *= resistance;
            
            _ax = 0;
            _ay = 0;
            
            pastTime = now;
        }
        
        public function setAcceleration(ax:Number, ay:Number):void
        {
            _ax += ax;
            _ay += ay;
        }
    }

//}