色と回転 forked02: モザイクタイル
package {
import __AS3__.vec.Vector;
import flash.display.Sprite;
import flash.events.Event;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="60")]
public class mosaicwave extends Sprite
{
private var W:Number = 465;
private var H:Number = 465;
private var tileArray:Vector.<mosTile> = new Vector.<mosTile>(100);
private var mosNum:uint = 100;
private var unitNum:Number; //1タイル分の幅
private var m:Meander;
public function mosaicwave()
{
makeMosaic(); //モザイクタイル作成
m = new Meander(); //目標の子
m.makePoint();
addChild( m );
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
}
private function makeMosaic():void
{
unitNum = ( W - 10 ) / 10;
for(var i:uint=0; i<mosNum; i++) {
var mos:mosTile = new mosTile();
addChild(mos);
tileArray[i] = mos;
mos.x = int( i / 10 ) * unitNum + 30;
mos.y = int( i % 10 ) * unitNum + 30;
}
}
private function handleEnterFrame(e:Event):void
{
m.HandleEnterFrame();
for(var i:uint=0; i<mosNum; i++) {
tileArray[i].handleEnterFrame(m.x - tileArray[i].x, m.y + tileArray[i].mouseY);
}
}
}
}
import flash.display.Sprite;
import flash.geom.ColorTransform;
class mosTile extends Sprite
{
private var Layer_a:Sprite = new Sprite();
private var Layer_b:Sprite = new Sprite();
private var distance:Number;
private var dx:Number, dy:Number;
// ばね係数
private var SPRING_CONST:Number = 0.11;
// 慣性
private var FRICTION:Number = 0.89;
private var v:Number = 0;
private var myScale:Number;
public function mosTile()
{
setImg();
addChild(Layer_a);
addChild(Layer_b);
}
private function setImg():void
{
Layer_a.graphics.beginFill(0xffffff);
Layer_a.graphics.drawRect(-10,-3,20,6);
Layer_a.graphics.drawRect(-3,-10,6,7);
Layer_a.graphics.drawRect(-3,3,6,7);
Layer_a.graphics.endFill();
}
public function handleEnterFrame(px:Number, py:Number):void
{
dx = px;
dy = py;
distance = Math.sqrt(dx*dx + dy*dy);
if( distance < 100 ) {
myScale = 1 + ( 10 / (distance/10+1) );
Layer_a.rotation += distance * 0.1;
} else {
myScale = 1;
}
applyScale();
/*
Layer_b.graphics.clear();
Layer_b.graphics.lineStyle(1,0xffffff);
Layer_b.graphics.moveTo(0,0);
Layer_b.graphics.lineTo(px, py);
*/
}
private function applyScale():void
{
var scaledis:Number = myScale - Layer_a.scaleX;
var a:Number = SPRING_CONST * scaledis; //加速度
var ay:Number = SPRING_CONST * dy;
v += a; //加速度を速度に加算
v *= FRICTION;
Layer_a.scaleX += v;
Layer_a.scaleY += v;
Layer_a.transform.colorTransform = new ColorTransform(1,1,1,1,-100+-255*v*2, -50+-255*v*0.3, -200+-255*v*0.4,1);
}
}
class Meander extends Sprite
{
public var left:Number = 0;
public var right:Number = 465;
public var bottom:Number = 0;
public var top:Number = 465;
//param
public var xMax:uint = 5;
public var yMax:uint = 3;
public var xPos:Number = 100;
public var yPos:Number = 100;
public var xSpeed:Number = 0;
public var ySpeed:Number = 0;
public var xDelta:Number, yDelta:Number;
private var cnt:int = Math.round( 360 * Math.random() );
private var fcounter:uint = 0;
public function Meander()
{
//
}
public function makePoint():void
{
/*this.graphics.beginFill(0xff0000);
this.graphics.drawCircle(0,0,10);
this.graphics.endFill();*/
}
public function HandleEnterFrame():void
{
if(xPos < left) {
cnt += 90;
} else if(xPos > right) {
cnt += 90;
}
if(yPos < bottom) {
cnt += 90;
} else if(yPos > top) {
cnt += 90;
}
xSpeed = 15 * Math.cos(cnt * Math.PI/ 180);
ySpeed = 15 * Math.sin(cnt * Math.PI/ 180);
xPos = xPos + xSpeed;
yPos = yPos + ySpeed;
if(fcounter%360==0) {
xPos = 100 + ( right - 200 ) * Math.random();
yPos = 100 + ( top - 200 ) * Math.random();
}
this.x = xPos;
this.y = yPos;
fcounter++;
}
}