Mouse stalker 003_2 ( forked from: Mouse stalker 003 )
Super Simple Mouse stalker
超単純マウスストーカー改
領域クリックで背景色が黒/白に交代
// forked from Aquioux's Mouse stalker 003
// Super Simple Mouse stalker
// 超単純マウスストーカー改
// 領域クリックで背景色が黒/白に交代
package {
/**
* @author YOSHIDA, Akio
*/
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#ffffff")]
public class Main extends Sprite {
private var back:Shape; // 背景
private var flg:uint; // 背景色指定フラグ(0:黒、1:白)
private var numOfFollowShape:uint = 8; // マウス追随シェイプの数
private var vctr:Vector.<FollowShape>; // マウス追随シェイプ格納ベクター
public function Main():void {
Wonderfl.capture_delay(10);
// 下地
back = new Shape();
flg = Math.floor(Math.random() * 2); // flg : 0 or 1
drawBack(flg);
addChild(back);
// マウス追随シェイプ
vctr = new Vector.<FollowShape>(numOfFollowShape, true);
var target:FollowShape = null;
for (var i:uint = 0; i < numOfFollowShape; i++) {
var followShape:FollowShape = new FollowShape(flg, target);
followShape.x = -50;
followShape.y = -50;
vctr[i] = followShape;
addChild(followShape);
target = followShape;
}
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(MouseEvent.CLICK, clickHandler);
}
// 背景描画
private function drawBack(flg:uint):void {
back.graphics.clear();
back.graphics.beginFill(0xffffff*flg);
back.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
back.graphics.endFill();
}
private function enterFrameHandler(e:Event):void {
var i:uint = numOfFollowShape;
while (i--) { vctr[i].motion(); }
}
private function clickHandler(e:MouseEvent):void {
// 背景色、マウス追随シェイプのブレンドモードの変更
flg == 1 ? flg = 0 : flg = 1;
drawBack(flg);
var i:uint = numOfFollowShape;
while (i--) { vctr[i].setBlendMode(flg); }
}
}
}
// マウス追随シェイプクラス
import flash.display.Shape;
import flash.display.BlendMode;
import flash.filters.BlurFilter;
import flash.filters.BitmapFilterQuality;
class FollowShape extends Shape {
private var vx:Number = 0;
private var vy:Number = 0;
private var spring:Number = 0.01;
private var friction:Number = 0.9;
private var target:FollowShape;
public function FollowShape(flg:uint, target:FollowShape = null):void {
// 追随相手(一つ前の FollowShape)
// 最初に生成された FollowShape は target が null になる
this.target = target;
// 形状描画
var size:uint = 30;
graphics.beginFill(Math.random() * 0xffffff);
graphics.drawCircle(0, 0, size);
graphics.endFill();
// ブレンドモード設定
setBlendMode(flg);
// BlurFilter 適用
filters = [new BlurFilter(16, 16, BitmapFilterQuality.HIGH)];
}
// ブレンドモード設定
public function setBlendMode(flg:uint):void {
// 背景色の白(flg==1)/黒(flg==0)で BlendMode を変える
flg == 1 ? blendMode = BlendMode.SUBTRACT : blendMode = BlendMode.ADD;
}
public function motion():void {
var tx:Number;
var ty:Number;
if (target == null) {
tx = root.mouseX;
ty = root.mouseY;
} else {
tx = target.x;
ty = target.y;
}
vx += (tx - x) * spring;
vy += (ty - y) * spring;
vx *= friction;
vy *= friction;
x += vx;
y += vy;
}
}