3D演習その2 +Zモーション
ActionScript3.0Animation読みながら、とりあえず3D投影してみる。
Z軸で動かしてみる
// forked from yd_niku's 3D演習その1
package {
// ActionScript3.0Animation読みながら、とりあえず3D投影してみる。
// Z軸で動かしてみる
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import caurina.transitions.Tweener;
public class FlashTest extends Sprite {
private var renderList:Array = [];
private var screenX:Number;
private var screenY:Number;
public function FlashTest() {
stage.frameRate = 60;
screenX = stage.stageWidth/2;
screenY = stage.stageHeight/2;
setup();
addEventListener( Event.ENTER_FRAME, render );
putThemFar();
}
private function setup():void {
var xNum:uint = 8;
var yNum:uint = 8;
var zNum:uint = 10;
var xInterval:uint = 50;
var yInterval:uint = 50;
var zInterval:uint = 50;
for( var i:int=0; i<xNum; ++i ) {
for( var j:int=0; j<yNum; ++j ) {
for( var k:int=0; k<zNum; ++k ) {
createBall(
(i-xNum*0.5)*xInterval,
(j-yNum*0.5)*yInterval,
(k-zNum*0.2)*zInterval
);
}
}
}
}
private function createBall( sx:Number, sy:Number, sz:Number ):void {
var sp :Sprite = new Sprite();
sp.graphics.lineStyle(0,0xffffff);
sp.graphics.beginFill(0xcc6600);
sp.graphics.drawCircle( 0, 0, 10 );
sp.graphics.endFill();
addChild( sp );
renderList.push( {
x: sx,
y: sy,
z: 0,
tz:sz,
view:sp
});
}
private function putThemFar():void {
var l:uint = renderList.length;
for( var i:uint=0; i<l; ++i ) {
var obj:Object = renderList[i];
var view:DisplayObject = obj.view;
var tz:Number= obj.tz;
Tweener.addTween( obj, { z: tz, time:4, transition:"easeInOutQuart" } );
}
}
private var focus:uint = 250;
private function render(e:Event):void {
renderList.sortOn( "z", Array.DESCENDING|Array.NUMERIC );
var l:uint = renderList.length;
for( var i:uint=0; i<l; ++i ) {
var obj:Object = renderList[i];
if( obj.z > -focus ) {
var scale :Number= focus/(focus+obj.z);
obj.view.x = screenX + obj.x * scale;
obj.view.y = screenY + obj.y * scale;
obj.view.scaleX = obj.view.scaleY = scale;
obj.view.alpha = Math.min( 1, scale );
obj.view.visible = true;
this.setChildIndex( obj.view, i );
}
else {
obj.view.visible = false;
}
}
}
}
}