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: ActionScriptアニメーションより練習1

...
ActionScript3.0アニメーションのP234の練習
Get Adobe Flash player
by pdovini 30 Mar 2011
    Embed
// forked from yoshinakana's forked from: ActionScriptアニメーションより練習1
// forked from sakusan393's ActionScriptアニメーションより練習1
package  {
    import flash.display.Sprite;
    import flash.events.Event;
    
    /**
     * ...
     * ActionScript3.0アニメーションのP234の練習
     */
    public class Bubbles extends Sprite {
        private var balls:Array;
        private var centerBall:Ball;
        private var numBalls:Number = 20;
        private var bouce:Number = -0.9;
        private var spring:Number = .1;
        private var gravity:Number = 1.5;
        
        public function Bubbles() {
            init();
        }
        
        private function init():void{
            balls = new Array();
            centerBall = new Ball(100, 0xCCCCCC);
            centerBall.vx = Math.random() * 15 - 7
            centerBall.vy = Math.random() * 15 - 7;
            
            addChild(centerBall);
            centerBall.x = stage.stageWidth / 2;
            centerBall.y = stage.stageHeight / 2;
            
            
            for (var i:uint = 0; i <numBalls; i++) {
                var ball:Ball = new Ball(Math.random() * 40 + 5, Math.random() * 0xFFFFFF);
                ball.x = Math.random() * stage.stageWidth;
                ball.y = Math.random() * stage.stageHeight;
                ball.vx = Math.random() * 5 - 3;
                ball.vy = Math.random() * 5 - 3;
                addChild(ball);
                balls.push(ball);
                
            }
            addEventListener(Event.ENTER_FRAME, xEnterFrame);
            
        }
        
        private function xEnterFrame(e:Event):void {
            moveCenter(centerBall);
            for (var i:uint = 0; i < numBalls; i++) {
                var ball:Ball = balls[i];
                ball.vy += gravity;
                move(ball);
                var dx:Number = ball.x - centerBall.x;
                var dy:Number = ball.y - centerBall.y;
                var dist:Number = Math.sqrt(dx * dx + dy * dy);
                var minDist:Number = ball.radius + centerBall.radius;
                
                if (dist < minDist) {
                    var angle:Number = Math.atan2(dy, dx);
                    var tx:Number = centerBall.x + Math.cos(angle) * minDist;
                    var ty:Number = centerBall.y + Math.sin(angle) * minDist;
                    ball.vx += (tx - ball.x) * spring;
                    ball.vy += (ty - ball.y) * spring;
                }
            }
        }
        
        private function move(ball:Ball):void {
            ball.x += ball.vx;
            ball.y += ball.vy;
            if (ball.x + ball.radius > stage.stageWidth) {
                ball.x = stage.stageWidth - ball.radius;
                ball.vx *= bouce;
            } else if (ball.x - ball.radius < 0) {
                ball.x = ball.radius;
                ball.vx *= bouce;
            }
            if (ball.y + ball.radius > stage.stageHeight) {
                ball.y = stage.stageHeight - ball.radius;
                ball.vy *= bouce;
            } else if (ball.y - ball.radius < 0) {
                ball.y = ball.radius;
                ball.vy *= bouce;
            }
        }
        private function moveCenter(ball:Ball):void {
            ball.x += ball.vx;
            ball.y += ball.vy;
            if (ball.x + ball.radius > stage.stageWidth) {
                ball.x = stage.stageWidth - ball.radius;
                ball.vx *= bouce;
            } else if (ball.x - ball.radius < 0) {
                ball.x = ball.radius;
                ball.vx *= bouce;
            }
            if (ball.y + ball.radius > stage.stageHeight) {
                ball.y = stage.stageHeight - ball.radius;
                ball.vy *= bouce;
            } else if (ball.y - ball.radius < 0) {
                ball.y = ball.radius;
                ball.vy *= bouce;
            }
        }
        
        
    }
    
}
import flash.display.Sprite;
class Ball extends Sprite {
    
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var radius:Number;
    public var color:Number;
    
    public function Ball(radius:Number = 5, color:Number = 0xFF6600 ) {
        this.radius = radius;
        this.color = color;
        init();
    }
    
    private function init():void{
        graphics.beginFill(color);
        graphics.drawCircle(0, 0,radius);
        graphics.endFill();
    }
    
}