CLOCKもどき
Timerクラス
...
@author axcel-work
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
/* Timerクラス */
import flash.utils.Timer;
import flash.events.TimerEvent;
/* */
import caurina.transitions.Tweener;
/**
* ...
* @author axcel-work
*/
public class Main extends Sprite {
/* 背景 */
private var sprBack1:Sprite;
private var sprBack2:Sprite;
/* 時計表示 */
private var date:Date;
private var strClock:String;
private var txtClock:TextField;
/* Flg */
private var changeFlg:Boolean = true;
/* ターゲットの名前 */
private var targetSprite:Sprite;
/* 位置 */
private var cnt:int = 0;
private var movePoint:Point = new Point();
/*===============================================
* コンストラクタ
*/
public function Main():void{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
/*===============================================
* メソッド:init
*/
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
/* 背景2作成 */
sprBack2 = new Sprite();
sprBack2.graphics.beginFill( Math.random() * 0xFFFF00, 1 );
sprBack2.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
sprBack2.graphics.endFill();
addChild( sprBack2 );
/* 背景1作成 */
sprBack1 = new Sprite();
sprBack1.graphics.beginFill( Math.random() * 0xFFFF00, 1 );
sprBack1.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
sprBack1.graphics.endFill();
addChild( sprBack1 );
/* TextFieldの作成 */
txtClock = new TextField();
txtClock.selectable = false;
txtClock.width = stage.stageWidth;
addEventListener( Event.ENTER_FRAME, setClock );
addChild( txtClock );
/* TextFormatの作成 */
var tfClock:TextFormat = new TextFormat();
tfClock.size = 70;
tfClock.font = "Arial";
tfClock.align = "center";
tfClock.color = "0xFFFFFF";
txtClock.defaultTextFormat = tfClock;
txtClock.y = stage.stageHeight/2 - txtClock.height/2;
/* Timerの設置 */
var myTimer:Timer = new Timer( 1000, 0 );
myTimer.addEventListener( TimerEvent.TIMER, clockHandler );
myTimer.start();
}
/*===============================================
* イベントハンドラ:タイマーイベント
*/
private function clockHandler( evt:TimerEvent ):void {
/* フラグの更新 */
if ( changeFlg ) {
targetSprite = sprBack1;
changeFlg = false;
}
else {
targetSprite = sprBack2;
changeFlg = true;
}
/* 移動方向を算出 */
movePoint = getCourse( cnt );
if ( cnt == 3 ) {
cnt = 0;
}
else {
cnt++;
}
/* 移動開始 */
Tweener.addTween( targetSprite, { x:movePoint.x, y:movePoint.y, transition:"easeOutQuat", time:0.5, onComplete:swapChild, onCompleteParams:[ targetSprite ] } );
}
/*===============================================
* メソッド:移動終了後、諸々初期化
*/
private function swapChild( targetInstance:Sprite ):void {
/* 深度の変更 */
swapChildren( sprBack1, sprBack2 );
targetInstance.x = 0;
targetInstance.y = 0;
targetInstance.graphics.clear();
targetInstance.graphics.beginFill( Math.random() * 0xFFFF00, 1 );
targetInstance.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
targetInstance.graphics.endFill();
}
/*===============================================
* メソッド:セッター:時計をセットする
*/
private function setClock ( evt:Event ):void {
date = new Date();
strClock = (date.hours < 10 ? "0" : "") + date.hours;
strClock += ":";
strClock += (date.minutes < 10 ? "0" : "") + date.minutes;
strClock += ":";
strClock += (date.seconds < 10 ? "0" : "") + date.seconds;
txtClock.text = strClock;
}
/*===============================================
*メソッド:ゲッター:渡される値によって移動するポイントを算出
*/
private function getCourse( num:int ):Point {
var x:Number;
var y:Number;
switch( num ) {
case 0:
x = -stage.stageWidth;
y = 0;
break;
case 1:
x = 0;
y = -stage.stageHeight;
break;
case 2:
x = stage.stageWidth;
y = 0;
break;
case 3:
x = 0;
y = stage.stageHeight;
break;
}
return new Point( x, y );
}
}
}