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 ch08_10

Get Adobe Flash player
by nmrtmnr 07 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/hTMh
 */

  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 ChainArray extends Sprite {
      private var balls:Array;
      private var numBalls:Number = 5;
      private var spring:Number = 0.1;
      private var friction:Number = 0.8;
      private var gravity:Number = 5;

      public function ChainArray() {
        init();

        var textField:TextField = new TextField();
        addChild(textField);
        textField.x = 10;
        textField.y = 10;
        textField.width = stage.stageWidth;
        textField.height = 20;

        textField.text = 'バネのチェーン (配列版)';
      }

      private function init():void {
        balls = new Array();
        for(var i:uint = 0; i < numBalls; i++) {
          var ball:Ball = new Ball(20);
          addChild(ball);
          balls.push(ball);
        }
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
      }

      public function onEnterFrame(event:Event):void {
        graphics.clear();
        graphics.lineStyle(1);
        graphics.moveTo(mouseX, mouseY);
        moveBall(balls[0], mouseX, mouseY);
        graphics.lineTo(balls[0].x, balls[0].y);

        for(var i:uint = 1; i < numBalls; i++) {
          var ballA:Ball = balls[i - 1];
          var ballB:Ball = balls[i];
          moveBall(ballB, ballA.x, ballA.y);
          graphics.lineTo(ballB.x, ballB.y);
        }
      }

      private function moveBall(ball:Ball, targetX:Number, targetY:Number):void {
        ball.vx += (targetX - ball.x) * spring;
        ball.vy += (targetY - ball.y) * spring;
        ball.vy += gravity;
        ball.vx *= friction;
        ball.vy *= friction;

        ball.x += ball.vx;
        ball.y += ball.vy;
      }
    }
  }

  import flash.display.Sprite;

  // 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();
    }
  }
  // ↑↑↑