2次元で3次元表現
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;
import flash.text.TextField;
[SWF(backgroundColor="#000000")]
public class Rotate extends Sprite
{
private var base:Sprite; //すべての文字のインスタンスを乗せるためのSprite
private var timer:Timer;
private var plus:Boolean = true;
private var numberTF:Array;
private var n:Number = 0;
private var numberX:Array, numberY:Array, numberZ:Array, numberSize:Array;
private const hankei:Number = 300; //環の半径
private const screenZ:Number = 400; //3次元座標値を2次元のステージに投影するときのスクリーンのx座標値
private const centerZ:Number = 950; //時間文字を3次元上の環状に配置するときの環の中心の奥行き。
public function Rotate()
{
//Spriteの初期化
base = new Sprite();
base.x = this.stage.stageWidth/4 +50; //基準点を決めている
base.y = this.stage.stageHeight/12;
addChild(base);
numberTF = new Array();
numberX = new Array();
numberY = new Array();
numberZ = new Array();
numberSize = new Array();
//文字の作成
for(var i:Number = 0; i < 50; i++){
var symbol:Sprite = createSymbol();
if(i < 10) (symbol.getChildAt(0) as TextField).text = "0"+i;
else (symbol.getChildAt(0) as TextField).text = i.toString();
numberTF.push(symbol);
base.addChild(symbol);
numberX.push(0);
numberY.push(300);
numberZ.push(0);
numberSize.push(1);
}
timer = new Timer(33);
timer.addEventListener(TimerEvent.TIMER, loop);
timer.start();
var timer2:Timer = new Timer(500);
timer2.addEventListener(TimerEvent.TIMER, count);
timer2.start();
this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
//環を回す
private function mouseDown(event:MouseEvent):void{
plus = !plus;
}
private function count(event:TimerEvent):void{
if(plus) n++;
else n--;
if(n < 0) n = 49;
}
private function loop(event:TimerEvent):void{
var num:Number = Math.abs(n) % 50;
for(var i:Number = 0; i < 50; i++){
var theAngle:Number = (-num/50*Math.PI*2 + i/50*Math.PI*2); //環に配置する角度を求める
var theX:Number = hankei*Math.sin(theAngle); //3次元空間上のx座標を求める
var theZ:Number = centerZ - hankei*Math.cos(theAngle); //3次元空間上のz座標を求める
numberX[i] += (theX - numberX[i])/4; //目的の位置まで徐々に動かす
numberZ[i] += (theZ - numberZ[i])/4;
if(i == num) numberSize[i] += (9 - numberSize[i])/4; //正面にきている文字を拡大する
else numberSize[i] += (1 - numberSize[i])/4;
numberTF[i].x = numberX[i]*screenZ/numberZ[i]; //3次元の座標から2次元座標への変換
numberTF[i].y = numberY[i]*screenZ/numberZ[i];
numberTF[i].scaleX = numberSize[i]*screenZ/numberZ[i]; //サイズの変更
numberTF[i].scaleY = numberSize[i]*screenZ/numberZ[i];
}
}
private function createSymbol():Sprite{
var sp:Sprite = new Sprite();
var moji:TextField = new TextField();
moji.width = 18;
moji.height = 20;
moji.x = -moji.width/2;
moji.y = -moji.height/2;
moji.textColor = 0xffffff;
moji.text = "";
sp.addChild(moji);
return sp;
}
}
}