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

[練習]ボールがボールをよける動き

Get Adobe Flash player
by Tamanegi_kenshi 16 Aug 2010
/**
 * Copyright Tamanegi_kenshi ( http://wonderfl.net/user/Tamanegi_kenshi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/c1vxD
 */

package {
    import flash.display.AVM1Movie;
    import flash.events.Event;
    import flash.display.Sprite;
    
    public class FlashTest extends Sprite {
        
        private var ball:Ball;
        private var ball2:Ball;
        private var ballArr:Array = [];
       
        private var xNum:int = 10;
        private var yNum:int = 10;
        private var ballNum:int = xNum * yNum;
        
        private var firstX:Array = [];
        private var firstY:Array = [];
        
        public function FlashTest() {
            init();
        }
       
        private function init():void{
            ball = new Ball();
            addChild(ball);
            
            for(var i:int = 0; i < xNum; i++){
                for(var j:int = 0; j < yNum; j++){
            ball2 = new Ball();
            addChild(ball2);
            ball2.x = i * 50;
            ball2.y = j * 50
            firstX.push(ball2.x);
            firstY.push(ball2.y);
            ballArr.push(ball2);
            }
            }

            addEventListener(Event.ENTER_FRAME, onEnter);
        }
        
        private function onEnter(event:Event):void{
            ball.x = mouseX;
            ball.y = mouseY;
            
            for(var i:int = 0; i < ballNum; i++){
            var ball2:Ball = ballArr[i];
            var rad:Number = Math.atan2(ball2.y - mouseY, ball2.x - mouseX);
            var dist:Number = Math.sqrt(Math.pow(mouseX - ball2.x, 2) + Math.pow(mouseY - ball2.y, 2));
            var per:Number = 500 / dist;
            ball2.x += per * Math.cos(rad) + (firstX[i] - ball2.x) * 0.1;
            ball2.y += per * Math.sin(rad) + (firstY[i] - ball2.y) * 0.1;
            }
       
   

        }


    }
}
import flash.display.Sprite;
class Ball extends Sprite{
    function Ball(radian:int = 20, color:uint = 0xcccccc){
        graphics.beginFill(color);
        graphics.drawCircle(0, 0, radian);
        graphics.endFill()
    }

}