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

Vector3Dクラスを使ってマウスストーカー

マウスストーカーをベクトルを使って再現したもの
Get Adobe Flash player
by miniapp 18 Jan 2016
/**
 * マウスストーカーをベクトルを使って再現したもの
 */
package { 
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Vector3D;
    
    [SWF(backgroundColor="0xFFFFFF", width="465", height="465", frameRate="60")]
    public class VectorMouseStalker extends Sprite{
        
        public function VectorMouseStalker() {
            node = new Node();
            node2 = new Node();
            node2.distance = 100;
            
            node3 = new Node();
            node4 = new Node();
            node4.distance = 100;
            
            ball = createBall(0, 0xFFFFFF, 0x0);
            ball2 = createBall(1, 0x0, 0xFFFFFF);
            ball3 = createBox(1, 0x0, 0xFFFFFF);
            ball4 = createBox(1, 0xFFFFFF, 0x000000);
            
            addEventListener(Event.ENTER_FRAME, enterFrame);
        }
        
        public var node:Node;
        public var node2:Node;
        public var node3:Node;
        public var node4:Node;
        public var ball:Sprite;
        public var ball2:Sprite;
        public var ball3:Sprite;
        public var ball4:Sprite;
        
        private function createBall(thickness:Number, lineColor:uint, ballColor:uint):Sprite {
            var sp:Sprite = new Sprite();
            sp.graphics.lineStyle(thickness, lineColor);
            sp.graphics.beginFill(ballColor);
            sp.graphics.drawCircle(0, 0, 10);
            sp.graphics.endFill();
            addChild(sp);
            return sp;
        }
        
        private function createBox(thickness:Number, lineColor:uint, boxColor:uint):Sprite {
            var sp:Sprite = new Sprite();
            sp.graphics.lineStyle(thickness, lineColor);
            sp.graphics.beginFill(boxColor);
            sp.graphics.drawRect(-5, -5, 10, 10);
            sp.graphics.endFill();
            addChild(sp);
            return sp;
        }
        
        private function enterFrame(e:Event):void {
            var target:Vector3D = new Vector3D(mouseX, mouseY, 0);
            
            //マウスの座標までついてくる
            node.update(target);
            ball.x = node.position.x;
            ball.y = node.position.y;
            
            //マウスからdistanceで設定した分離れてついてくる
            node2.update2(target);
            ball2.x = node2.position.x;
            ball2.y = node2.position.y;
            
            //バネ運動
            node3.update3(target);
            ball3.x = node3.position.x;
            ball3.y = node3.position.y;
            
            //マウスからdistanceで設定した分離れてバネ運動
            node4.update4(target);
            ball4.x = node4.position.x;
            ball4.y = node4.position.y;
        }
    }
}

import flash.geom.Vector3D;
class Node { 
    
    public static var SPRING:Number = 0.08;
    public static var REDUCE:Number = 0.1;
    public static var FRICTION:Number = 0.96;
    
    public function Node(x:Number = 0, y:Number = 0, z:Number = 0) { 
        position = new Vector3D(x, y, z);
    }
    
    public var position:Vector3D;
    public var velocity:Vector3D = new Vector3D();
    public var distance:Number = 0;
    
    /**
     * 指定座標まで追いかける
     * @param    target
     */
    public function update(target:Vector3D):void { 
        var dv:Vector3D = target.subtract(position);
        
        dv.scaleBy(REDUCE);
        
        position = position.add(dv);
    }
    
    /**
     * 指定座標からdistance離れた分だけ離れて追いかける
     * @param    target
     */
    public function update2(target:Vector3D):void {
        var dv:Vector3D = target.subtract(position);
        
        var tempDistance:Number = Vector3D.distance(target, position);
        var diffDistance:Number = tempDistance - distance;
        
        dv.scaleBy(REDUCE * diffDistance / tempDistance);
        position = position.add(dv);
    }
    
    /**
     * バネ運動で追いかける
     * @param    target
     */
    public function update3(target:Vector3D):void {
        var dv:Vector3D = target.subtract(position);
        
        dv.scaleBy(SPRING);
        velocity = velocity.add(dv);
        velocity.scaleBy(FRICTION);
        
        position = position.add(velocity);
    }
    
    /**
     * 指定座標からdistance離れた分だけバネ運動で追いかける
     * @param    target
     */
    public function update4(target:Vector3D):void {
        var dv:Vector3D = target.subtract(position);
        
        var tempDistance:Number = Vector3D.distance(target, position);
        var diffDistance:Number = tempDistance - distance;
        
        dv.scaleBy(SPRING * diffDistance / tempDistance);
        velocity = velocity.add(dv);
        velocity.scaleBy(FRICTION);
        
        position = position.add(velocity);
    }
}