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

[Study] AS 3.0 Animation ch09_01

Get Adobe Flash player
by nmrtmnr 11 Jul 2015
    Embed
/**
 * Copyright nmrtmnr ( http://wonderfl.net/user/nmrtmnr )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hxX2
 */

package {
  import flash.display.Sprite;
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  import flash.text.TextField;
  import flash.events.Event;
  import flash.events.MouseEvent;

  public class Bubbles extends Sprite {
    private var balls:Array;
    private var numBalls:Number = 10;
    private var centerBall:Ball;
    private var bounce:Number = -1;
    private var spring:Number = 0.2;
    private var rand:Number;
    private var ballScale:Number;

    public function Bubbles() {
      init();
    }

    private function init():void {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;

      balls = new Array();
      centerBall = new Ball(90);
      addChild(centerBall);
      centerBall.x = stage.stageWidth / 2;
      centerBall.y = stage.stageHeight / 2;

      for(var i:uint = 0; i < numBalls; i++) {
        rand = Math.random() * 100;
        if(rand < 50) {
          ballScale = Math.random() * 10 + 5;
        } else {
          ballScale = Math.random() * 15 + 30;
        }

        var ball:Ball = new Ball(ballScale, 0x1c1c1c);
        ball.x = Math.random() * stage.stageWidth;
        ball.y = Math.random() * stage.stageHeight;
        ball.vx = Math.random() * 6 - 3;
        ball.vy = Math.random() * 6 - 3;
        addChild(ball);
        balls.push(ball);
      }

      addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private function onEnterFrame(event:Event):void {
      for(var i:uint = 0; i < numBalls; i++) {
        var ball:Ball = balls[i];
        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 *= bounce;
      } else if (ball.x - ball.radius < 0) {
        ball.x = ball.radius;
        ball.vx *= bounce;
      }
      if(ball.y + ball.radius > stage.stageHeight) {
        ball.y = stage.stageHeight - ball.radius;
        ball.vy *= bounce;
      } else if (ball.y - ball.radius < 0) {
        ball.y = ball.radius;
        ball.vy *= bounce;
      }
    }
  }

}

import flash.display.Sprite;

// Boxクラス
class Box extends Sprite {
  private var w:Number;
  private var h:Number;
  private var color:uint;
  public var vx:Number;
  public var vy:Number;

  public function Box(width:Number = 50, height:Number = 50, color:uint = 0xff0000) {
    w = width;
    h = height;
    this.color = color;

    init();
  }

  public function init():void {
    graphics.beginFill(color);
    graphics.drawRect(-w / 2, -h / 2, w, h);
    graphics.endFill();
  }
}
// ↑↑↑

// Arrowクラス
class Arrow extends Sprite {
  public function Arrow() {
    init();
  }

  private function init():void {
    graphics.lineStyle(1, 0, 1);
    graphics.beginFill(0xffff00);
    graphics.moveTo(-50, -25);
    graphics.lineTo(0, -25);
    graphics.lineTo(0, -50);
    graphics.lineTo(50, 0);
    graphics.lineTo(0, 50);
    graphics.lineTo(0, 25);
    graphics.lineTo(-50, 25);
    graphics.lineTo(-50, -25);
    graphics.endFill();
  }
}
// ↑↑↑

// Ballクラス
class Ball extends Sprite {
  public var radius:Number;
  public var color:uint;
  public var vx:Number = 0;
  public var vy:Number = 0;

  public function Ball(radius:Number = 40, color:uint=0xff0000) {
    this.radius = radius;
    this.color = color;
    init();
  }

  private function init():void {
    graphics.beginFill(color);
    graphics.drawCircle(0, 0, radius);
    graphics.endFill();
  }
}
// ↑↑↑