[Study] AS 3.0 Animation ch05_04
/**
* Copyright nmrtmnr ( http://wonderfl.net/user/nmrtmnr )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qBGQ
*/
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
public class FollowMouse extends Sprite {
private var arrow:Arrow;
private var speed:Number = 5;
public function FollowMouse() {
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 {
arrow = new Arrow();
addChild(arrow);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
var dx:Number = mouseX - arrow.x;
var dy:Number = mouseY - arrow.y;
var angle:Number = Math.atan2(dy, dx);
arrow.rotation = angle * 180 / Math.PI;
var vx:Number = Math.cos(angle) * speed;
var vy:Number = Math.sin(angle) * speed;
arrow.x += vx;
arrow.y += 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(0x0000ff);
graphics.moveTo(-20, -10);
graphics.lineTo(0, -10);
graphics.lineTo(0, -20);
graphics.lineTo(20, 0);
graphics.lineTo(0, 20);
graphics.lineTo(0, 10);
graphics.lineTo(-20, 10);
graphics.lineTo(-20, -10);
graphics.endFill();
}
}
// ↑↑↑
// Ballクラス
class Ball extends Sprite {
private var radius:Number;
private var color:uint;
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();
}
}
// ↑↑↑