[Study] AS 3.0 Animation ch08_11
/**
* Copyright nmrtmnr ( http://wonderfl.net/user/nmrtmnr )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/1VqM
*/
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 MultiSpring extends Sprite {
private var ball:Ball;
private var handles:Array;
private var spring:Number = 0.1;
private var friction:Number = 0.8;
private var numHandles:Number = 4;
public function MultiSpring() {
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 {
ball = new Ball(20, 0x00ff00);
addChild(ball);
handles = new Array();
for(var i:uint = 0; i < numHandles; i++) {
var handle:Ball = new Ball(10, 0x343434);
handle.x = Math.random() * stage.stageWidth;
handle.y = Math.random() * stage.stageHeight;
handle.addEventListener(MouseEvent.MOUSE_DOWN, onPress);
addChild(handle);
handles.push(handle);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addEventListener(MouseEvent.MOUSE_UP, onRelease);
}
public function onEnterFrame(event:Event):void {
for(var i:uint = 0; i < numHandles; i++) {
var handle:Ball = handles[i] as Ball;
var dx:Number = handle.x - ball.x;
var dy:Number = handle.y - ball.y;
ball.vx += dx * spring;
ball.vy += dy * spring;
}
ball.vx *= friction;
ball.vy *= friction;
ball.x += ball.vx;
ball.y += ball.vy;
graphics.clear();
graphics.lineStyle(1);
for(i = 0; i < numHandles; i++) {
graphics.moveTo(ball.x, ball.y);
graphics.lineTo(handles[i].x, handles[i].y);
}
}
private function onPress(event:MouseEvent):void {
event.target.startDrag();
}
private function onRelease(event:MouseEvent):void {
stopDrag();
}
}
}
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();
}
}
// ↑↑↑