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

BaitEating Fish 2D

マウスダウンで餌です
Vector2D:AdvancED ActionScript3.0 Animation.
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/6JRN
 */

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;
    import flash.events.MouseEvent;
    import flash.geom.Point;

    
    [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "0xFFFFFF")]
    public class Main extends Sprite
    {

        private var _moveContainer:SteeredContainer;
        private var _obstacles:Array = [];
        private var _pathList:Array = [];
        private var _loader:Loader;
        private var _bmd:BitmapData;
        
        private var _fish:PolyImage;
        private var _rot:Number = 0;
        
        private const IMG_URL:String = "http://www.digifie.jp/assets/images/fish_bone2.png";
        private const MAX_PATH:int = 30;
        
        public function Main()
        {
            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);
             setup()
        }
        
        private function setup():void
        {
            _moveContainer = new SteeredContainer();
            _moveContainer.edgeBehavior = Vehicle.WRAP;
            addChild(_moveContainer);
            _moveContainer.addChild(_fish);
            _fish.y = -_fish.height;
            
            _moveContainer.position = new Vector2D(stage.stageWidth * .5, stage.stageHeight * .5);
            _moveContainer.velocity.length = 5;
            _moveContainer.velocity.angle = Math.PI / 4;
            _moveContainer.maxSpeed = 3;
            
            stage.addEventListener(MouseEvent.CLICK, addPath);
            addEventListener(Event.ENTER_FRAME, update);
        }
        
        private function update(e:Event):void
        {
            _fish.update();
            turn();
            _moveContainer.rotation = _rot;
            _moveContainer.update();
            //
            _moveContainer.followPath(_pathList, true);
            //
            var l:int = _pathList.length;
            try{
                if(_fish.hitTestPoint(_obstacles[0].x, _obstacles[0].y, true)){
                    removeChild(_obstacles[0]);
                    _obstacles.shift();
                    _pathList.shift();
                }
            }catch(e:Error){
                
            }
        }
        
        private function addPath(e:MouseEvent):void
        {
            if(_pathList.length > MAX_PATH) return;
            var obstacle:Obstacle = new Obstacle(5);
            obstacle.x = mouseX;
            obstacle.y = mouseY;
            addChildAt(obstacle, 0);
            _obstacles.push(obstacle);
            //
            var v:Vector2D = new Vector2D(obstacle.x, obstacle.y);
            _pathList.push(v);
        }
        
        private function turn(e:Event = null):void
        {
            if(_moveContainer.x < _moveContainer.x + _moveContainer.velocity.x){
                _rot = Math.atan2(_moveContainer.velocity.y, _moveContainer.velocity.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(-(_moveContainer.velocity.y), -(_moveContainer.velocity.x)) * 180 / Math.PI;
                if(_fish.rotationY > 0){
                    _fish.rotationY -= 5;
                }else{
                    _fish.rotationY = 0;
                    _fish.removeEventListener(Event.ENTER_FRAME, turn);
                }
            }
        }

    }

}


//package jp.digifie.move
//{
    import flash.display.Sprite;

    internal class Obstacle extends Sprite
    {

        private var _radius:Number;
        
        public function Obstacle(r:Number)
        {
            _radius = r;
            draw();
        }
        
        public function get radius():Number
        {
            return _radius;
        }
        
        public function get position():Vector2D
        {
            return new Vector2D(x, y);
        }
        
        private function draw():void
        {
            graphics.lineStyle(2, 0);
            graphics.beginFill(0xFFFFFF);
            graphics.drawCircle(0, 0, _radius);
        }

    }

//}


//package
//{
    import flash.display.Sprite;

    internal class Vehicle extends Sprite
    {

        protected var _edgeBehavior:String = WRAP;
        protected var _mass:Number = 1;
        protected var _maxSpeed:Number = 10;
        protected var _position:Vector2D; //Position
        protected var _velocity:Vector2D; //Speeed
        protected var _color:int;
        
        // EdgeBehavior const
        public static const WRAP:String = "wrap";
        public static const BOUNCE:String = "bounce";
        
        public function Vehicle(color:int = 0xFFFFFF)
        {
            _position = new Vector2D();
            _velocity = new Vector2D();
            _color = color;
            //draw();
        }
        
        // --------- Default DrawImage (Overridable) -----------------/
        protected function draw():void
        {
            graphics.clear();
            graphics.beginFill(_color);
            graphics.lineStyle(0);
            graphics.moveTo(10, 0);
            graphics.lineTo(-10, 5);
            graphics.lineTo(-10, -5);
            graphics.lineTo(10, 0);
        }
        
        
        // --------- Update -----------------/
        public function update():void
        {
            // Speed trim MaxSpeed.
            _velocity.truncate(_maxSpeed); 
            
            // Position Added Speed
            _position = _position.add(_velocity);
            
            // EdgeBehavior WRAP or BOUNCE
            if(_edgeBehavior == WRAP){
                wrap();
            }else if(_edgeBehavior == BOUNCE){
                bounce();
            }
            
            // this Position Update
            this.x = _position.x;
            this.y = _position.y;
        }
        
        
        // --------- Bounce -----------------/
        private function bounce():void
        {
            if(stage != null){
                if(_position.x > stage.stageWidth){
                    _position.x = stage.stageWidth;
                    _velocity.x *= -1;
                }else if(_position.x < 0){
                    _position.x = 0;
                    _velocity.x *= -1;
                }
                if(_position.y > stage.stageHeight){
                    _position.y = stage.stageHeight;
                    _velocity.y *= -1;
                }else if(_position.y < 0){
                    _position.y = 0;
                    _velocity.y *= -1;
                }
            }
        }
        
        
        // --------- Wrap -----------------/
        private function wrap():void
        {
            if(stage != null){
                if(_position.x > stage.stageWidth) _position.x = 0;
                if(_position.x < 0) _position.x = stage.stageWidth;
                if(_position.y > stage.stageHeight) _position.y = 0;
                if(_position.y < 0) _position.y = stage.stageHeight;
            }
        }
        
        
        // --------- Set _edgeBehavior -----------------/ 
        public function set edgeBehavior(str:String):void
        {
            _edgeBehavior = str;
        }
        // --------- Get _edgeBehavior -----------------/ 
        public function get edgeBehavior():String
        {
            return _edgeBehavior;
        }
        
        
        // --------- Set _mass -----------------/ 
        public function set mass(n:Number):void
        {
            _mass = n;
        }
        // --------- Get _mass -----------------/ 
        public function get mass():Number
        {
            return _mass;
        }
        
        
        // --------- Set _maxSpeed -----------------/ 
        public function set maxSpeed(n:Number):void
        {
            _maxSpeed = n;
        }
        // --------- Get _mass -----------------/ 
        public function get maxSpeed():Number
        {
            return _maxSpeed;
        }
        
        
        // --------- Set _position -----------------/ 
        public function set position(v:Vector2D):void
        {
            _position = v;
            // set this Position
            this.x = _position.x;
            this.y = _position.y;
        }
        // --------- Get _position -----------------/ 
        public function get position():Vector2D
        {
            return _position;
        }
        
        
        // --------- Set _velocity -----------------/ 
        public function set velocity(v:Vector2D):void
        {
            _velocity = v;
        }
        // --------- Get _velocity -----------------/ 
        public function get velocity():Vector2D
        {
            return _velocity;
        }
        
        
        // --------- set Sprite.x -----------------/
        override public function set x(n:Number):void
        {
            super.x = n;
            _position.x = this.x;
        }
        // --------- set Sprite.y -----------------/
        override public function set y(n:Number):void
        {
            super.y = n;
            _position.y = this.y;
        }
    }

//}



//package
//{
    import flash.display.Sprite;
    
    internal class SteeredContainer extends Vehicle
    {
        
        // Basic property
        private var _maxForce:Number = 1;
        private var _steeringForce:Vector2D;
        
        // @ Arrive
        private var _arrivalThreshold:Number = 100;
        
        // @ followPath
        private var _pathIndex:int = 0;
        private var _pathThreshold:Number = 20;
        
        public function SteeredContainer(color:int = 0xFFFFFF)
        {
            _steeringForce = new Vector2D();
            super(color);

        }
        
        // --------- Update -----------------/
        override public function update():void
        {
            _steeringForce.truncate(_maxForce);
            _steeringForce = _steeringForce.divide(_mass);
            _velocity = _velocity.add(_steeringForce);
            _steeringForce = new Vector2D();
            super.update();
        }
        
        
        public function set maxForce(n:Number):void
        {
            _maxForce = n;
        }
        public function get maxForce():Number
        {
            return _maxForce;
        }
        
        
        // --------- @ Arrive -----------------/
        public function set arrivalThreshold(n:Number):void
        {
            _arrivalThreshold = n;
        }
        public function get arrivalThreshold():Number
        {
            return _arrivalThreshold;
        }
        
        // --------- @ followPath -----------------/
        public function set pathIndex(n:int):void
        {
            _pathIndex = n;
        }
        public function get pathIndex():int
        {
            return _pathIndex;
        }
        
        public function set pathThreshold(n:Number):void
        {
            _pathThreshold = n;
        }
        public function get pathThreshold():Number
        {
            return _pathThreshold;
        }
        
        
        // --------- @ Seek -----------------/
        public function seek(target:Vector2D):void
        {
            var v:Vector2D = target.subtract(_position);
            v.normalize();
            v = v.multiply(_maxSpeed);
            
            var force:Vector2D = v.subtract(_velocity);
            _steeringForce = _steeringForce.add(force);
        }
        
        
        // --------- @ Arrive -----------------/
        public function arrive(target:Vector2D):void
        {
            var v:Vector2D = target.subtract(_position);
            v.normalize();
            
            var dist:Number = _position.dist(target);
            if(dist > _arrivalThreshold){
                v = v.multiply(_maxSpeed);
            }else{
                v = v.multiply(_maxSpeed * dist / _arrivalThreshold);
            }
            
            var force:Vector2D = v.subtract(_velocity);
            _steeringForce = _steeringForce.add(force);
        }
        
        
        // --------- @ followPath -----------------/
        public function followPath(path:Array, isLoop:Boolean = false):void
        {
            var wayPoint:Vector2D = path[_pathIndex];
            if(wayPoint == null) return;
            
            if(_position.dist(wayPoint) < _pathThreshold){
                if(_pathIndex >= path.length - 1){
                    if(isLoop){
                        _pathIndex = 0;
                    }
                }else{
                    _pathIndex++;
                }
            }
            
            if(_pathIndex >= path.length - 1 && !isLoop){
                arrive(wayPoint);
            }else{
                seek(wayPoint);
            }
        }
    }

//}



//package
//{

    import flash.display.Graphics;
    
    internal class Vector2D
    {

        private var _x:Number;
        private var _y:Number;
        
        public function Vector2D(x:Number = 0, y:Number = 0)
        {
            _x = x;
            _y = y;
        }
        
        // --------- Draw Graphics -----------------/
        public function draw(g:Graphics, color:uint = 0):void
        {
            g.lineStyle(0, color);
            g.moveTo(0, 0);
            g.lineTo(_x, _y);
        }
        
        
        // --------- Copy Vector2D (@return this.clone) -----------------/
        public function clone():Vector2D
        {
            return new Vector2D(x, y);  // function get x() // function get y()
        }
        
        
        // --------- propaty 0 Reset (@return this) -----------------/
        public function zero():Vector2D
        {
            _x = 0;
            _y = 0;
            return this;
        }
        
        
        // --------- propaty 0 Check (@return Boolean) -----------------/
        public function isZero():Boolean
        {
            return _x == 0 && _y == 0;
        }
        
        
        // --------- Set Vector Length -----------------/
        public function set length(n:Number):void
        {
            var a:Number = angle; // function get angle()
            _x = Math.cos(a) * n;
            _y = Math.sin(a) * n;
        }
        // --------- Get Vector Length (@return Number) -----------------/
        public function get length():Number
        {
            return Math.sqrt(lengthSQ()); // function get lengthSQ()
        }
        
        
        // --------- Vector Length Square -----------------/
        public function lengthSQ():Number
        {
            return _x * _x + _y * _y;
        }
        
        
        // --------- Set Vector Angle -----------------/
        public function set angle(n:Number):void
        {
            var l:Number = length; // function get length()
            _x = Math.cos(n) * l;
            _y = Math.sin(n) * l;
        }
        // --------- Get Vector Angle (@return Number[Radian]) -----------------/
        public function get angle():Number
        {
            return Math.atan2(_y, _x);
        }
        
        
        // --------- Vector Normalize [Vector.length = 1] (@return this) -----------------/
        public function normalize():Vector2D
        {
            if(length == 0){
                _x = 1; // Case Voctor = 0 on (1, 0)
                return this;
            }
            var l:Number = length;
            _x /= l;
            _y /= l;
            return this;
        }
        
        
        // --------- Checked Vector Normalize (@return Boolean) -----------------/
        public function isNormalized():Boolean
        {
            return length == 1;
        }
        
        
        // --------- Trim of Vector.length (@return this) -----------------/
        public function truncate(max:Number):Vector2D
        {
            length = Math.min(max, length);
            return this;
        }
        
        
        // --------- Reverse Vector (@return this) -----------------/
        public function reverse():Vector2D
        {
            _x = -_x;
            _y = -_y;
            return this;
        }
        
        
        // --------- this to [@param Vector2D] Inner product (@return Number) -----------------/
        public function dotProd(v2:Vector2D):Number
        {
            return _x * v2.x + _y * v2.y;
        }
        
        
        // --------- [@param1 Vector2D] to [@param2 Vector2D] Angle (@return Number) -----------------/
        public function angleBetween(v1:Vector2D, v2:Vector2D):Number
        {
            if(v1.isNormalized()) v1 = v1.clone().normalize();
            if(v2.isNormalized()) v2 = v2.clone().normalize();
            return Math.acos(v1.dotProd(v2));
        }
        
        
        // --------- this to [@param Vector2D] right or left? (@return int [left = -1] [right = 1]) -----------------/
        public function sign(v2:Vector2D):int
        {
            return perp.dotProd(v2) < 0 ? -1 : 1; // function get perp()
        }
        
        
        // --------- this to Vertical Vector2D (@return Vector2D) -----------------/
        public function get perp():Vector2D
        {
            return new Vector2D(-y, x);
        }
        
        
        // --------- [@param1 Vector2D] to [@param2 Vector2D] Distance (@return Number) -----------------/
        public function dist(v2:Vector2D):Number
        {
            return Math.sqrt(distSQ(v2)); // function distSQ(Vectr2D)
        }
        
        
        // --------- [@param1 Vector2D] to [@param2 Vector2D] Square (@return Number) -----------------/
        public function distSQ(v2:Vector2D):Number
        {
            var dx:Number = v2.x - x;
            var dy:Number = v2.y - y;
            return dx * dx + dy * dy;
        }
        
        
        // --------- Add this to [@param2 Vector2D] (@return Vector2D) -----------------/
        public function add(v2:Vector2D):Vector2D
        {
            return new Vector2D(_x + v2.x, _y + v2.y);
        }
        
        
        // --------- Subtract this to [@param Vector2D] (@return Vector2D) -----------------/
        public function subtract(v2:Vector2D):Vector2D
        {
            return new Vector2D(_x - v2.x, _y - v2.y);
        }
        
        
        // --------- Multiply this to [@param Number] (@return Vector2D) -----------------/
        public function multiply(n:Number):Vector2D
        {
            return new Vector2D(_x * n, _y * n);
        }
        
        
        // --------- Divide this to [@param Number] (@return Vector2D) -----------------/
        public function divide(n:Number):Vector2D
        {
            return new Vector2D(_x / n, _y / n);
        }
        
        
        // --------- Equal this to [@param Vector2D] (@return Boolean) -----------------/
        public function equals(v2:Vector2D):Boolean
        {
            return _x == v2.x && _y == v2.y;
        }
        
        
        // --------- Set this._x -----------------/
        public function set x(n:Number):void
        {
            _x = n;
        }
        // --------- Get this._x (@return Number) -----------------/
        public function get x():Number
        {
            return _x;
        }
        
        
        // --------- Set this._y -----------------/
        public function set y(n:Number):void
        {
            _y = n;
        }
        // --------- Get this._y (@return Number) -----------------/
        public function get y():Number
        {
            return _y;
        }
        
        
        // --------- this to String (@return String) -----------------/
        public function toString():String{
            return "[Vector2D (x: " + _x + ", y: " + _y + ")]"
        }

    }

//}


//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 * .3;
            //
            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 += .5;
                if(_ofset > 5){
                    _ofset = 5;
                    _isRev = false;
                }
            }else{
                _ofset -= .5;
                if(_ofset < -5){
                    _ofset = -5;
                    _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();
        }

    }
//}