追跡の実験( Chaser Test )
いろんな方法でマウスを追跡させてみます。
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aevG
*/
/*
いろんな方法でマウスを追跡させてみます。
*/
package {
import flash.geom.Point;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.display.Bitmap;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.events.Event;
import flash.display.Sprite;
import com.bit101.components.*;
[SWF(frameRate="60")]
public class FlashTest extends Sprite {
private var lastChaser:Chaser;
private var chasers:Vector.<Chaser> = new Vector.<Chaser>;
private const W:int = 465;
private const H:int = 465;
private const length:int = 10;
private var box:ComboBox;
private var check:CheckBox;
private var back:BitmapData;
private var _ct:ColorTransform = new ColorTransform(0.8,0.6,0.6);
private var filter:BlurFilter = new BlurFilter();
public function FlashTest() {
var j:Chaser = new Chaser( Math.random()*W, Math.random()*H );
for( var i:int = 0; i < length; i++ ){
chasers.push( j )
j.scale = i*2 + 5;
j.next = new Chaser( Math.random()*W, Math.random()*H );
j = j.next;
}
chasers.push( j )
lastChaser = j;
chasers.reverse();
setComps();
back = new BitmapData( W, H, false, 0 );
stage.addChildAt( new Bitmap(back), 0 )
stage.mouseChildren = true;
stage.addEventListener( "mouseDown", onClick )
this.addEventListener( "exitFrame", onFrame )
graphics.beginFill( 0xFFFFFF, 1 );
graphics.lineStyle( 2, 0xFFFFFF, 1 );
}
private function setComps():void{
box = new ComboBox( stage, 2, 2, "linear", Chaser.typeList );
box.selectedIndex = 0;
box.addEventListener(Event.SELECT, onChange);
check = new CheckBox( stage, 110, 10, "delay", onChange );
}
private function onChange( e:Event ):void{
var l:uint = chasers.length
for( var i:uint = 0; i < l; i++ ){
var j:Chaser = chasers[i];
j.type = Chaser.typeList[ box.selectedIndex ];
j.delay = check.selected ? 1 : 0;
}
}
private function onClick(e:Event):void{
var l:uint = chasers.length
for( var i:uint = 0; i < l; i++ ){
var j:Chaser = chasers[i];
j.x = Math.random() * W;
j.y = Math.random() * H;
j.vx = 0;
j.vy = 0;
}
}
private function onFrame(e:Event):void{
lastChaser.x = mouseX;
lastChaser.y = mouseY;
var g:Graphics = graphics;
var prev:Chaser = chasers[0];
g.clear();
var l:uint = chasers.length
for( var i:uint = 0; i < l; i++ ){
var j:Chaser = chasers[i];
j.chase();
g.lineStyle( 2, 0xFFFFFF, 0.5 );
g.moveTo( prev.x, prev.y );
g.lineTo( j.x, j.y );
g.beginFill( 0xFFFFFF, 0.5 );
g.drawCircle( j.x, j.y, 3 );
prev = j;
}
}
}
}
class Chaser{
public var next:Chaser;
public var x:Number;
public var y:Number;
public var scale:Number = 4;
public var delay:Number = 0;
public var k:Number = 0.80;
public var type:String = "linear";
static public const CLOSE:String = "close"
static public const LINEAR:String = "linear"
static public const DISTANT:String = "distant"
static public const GRAV:String = "grav";
static public const SPRING:String = "spring";
static public const typeList:Array = [ LINEAR, CLOSE, DISTANT, GRAV, SPRING ];
public var vx:Number = 0;
public var vy:Number = 0;
public function Chaser( x:Number, y:Number ):void{
this.x = x;
this.y = y;
}
public function chase():void{
if(next){
switch( type ){
case CLOSE : _close(); break;
case LINEAR : _linear(); break;
case DISTANT : _distant(); break;
case GRAV : _grav(); break;
case SPRING: _spring(); break;
}
}
}
private function _close():void{
move( next.x, next.y );
}
private function _linear():void{
var x:Number = this.x; var y:Number = this.y;
var dx:Number = next.x - x;
var dy:Number = next.y - y;
if( dx * dx + dy * dy > scale * scale ){
var dir:Number = Math.atan2( dy, dx );
var vx:Number = Math.cos(dir) * scale;
var vy:Number = Math.sin(dir) * scale;
x += vx;
y += vy;
}else{
x = next.x;
y = next.y;
}
move( x, y );
}
private function _distant():void{
var x:Number = this.x; var y:Number = this.y;
var dx:Number = next.x - x;
var dy:Number = next.y - y;
var dir:Number = Math.atan2( dy, dx );
var vx:Number = Math.cos(dir) * scale;
var vy:Number = Math.sin(dir) * scale;
x = next.x - vx;
y = next.y - vy;
move( x, y );
}
private function _grav():void{
var x:Number = this.x; var y:Number = this.y;
var vx:Number = this.vx; var vy:Number = this.vy;
var dx:Number = next.x - x;
var dy:Number = next.y - y;
var dir:Number = Math.atan2( dy, dx );
vx += Math.cos(dir) * scale;
vy += Math.sin(dir) * scale;
accel( vx, vy );
move( x + vx, y + vy );
}
private function _spring():void{
var x:Number = this.x; var y:Number = this.y;
var vx:Number = this.vx; var vy:Number = this.vy;
var dx:Number = next.x - x;
var dy:Number = next.y - y;
var dir:Number = Math.atan2( dy, dx );
var dis:Number = Math.sqrt(dx * dx + dy * dy)
vx += Math.cos(dir) * scale * dis * 0.01;
vy += Math.sin(dir) * scale * dis * 0.01;
accel( vx, vy );
move( x + vx, y + vy );
}
private function move( x:Number, y:Number ):void{
var r:Number = delay;
if( delay == 0 ){
this.x = x;
this.y = y;
}else{
this.x = ( x + this.x*r ) / (1 + r);
this.y = ( y + this.y*r ) / (1 + r);
}
}
private function accel( vx:Number, vy:Number ):void{
var r:Number = delay;
if( delay == 0 ){
this.vx = vx * k;
this.vy = vy * k;
}else{
this.vx = vx / (1 + r) * k;
this.vy = vy / (1 + r) * k;
}
}
}