・[mixiアプリ]四則演算ゲーム携帯版のflashliteの処理構造
・[mixiアプリ]対戦リバーシの通信対戦の処理構造
に関して書こうと思ったのですが、また次の機会に。
今回は、actionscript3.0でストップウォッチの作成方法に関して。
ストップウォッチの作成方法にはenterframeとtimerの2種類があり
ざっと検索してみたところ、enterframe系の処理はサンプルソースがたくさんあったのですが、
timer系で処理しているものがみあたらなかったので書いてみました。
動作サンプル(左の四角をクリックでSTART、右の四角をクリックでSTOP)
あまり時間をかけないでサクッと書いたため、
命名や処理構造が最適化されていない部分はご了承下さい。
package {
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
public class FlashTest extends Sprite {
public var MiliSeconds = 0;
public var Clock = new Timer(10);
public var myTextField:TextField = new TextField();
public var startBTN:Sprite = new Sprite();
public var stopBTN:Sprite = new Sprite();
public function FlashTest() {
// timer text
myTextField.x = 100;
myTextField.y = 70;
myTextField.text ="0:00:00";
this.addChild(myTextField);
// start button
startBTN.graphics.beginFill(0xFFCC00);
startBTN.graphics.drawRect(60, 5, 40, 40);
this.addChild(startBTN);
startBTN.addEventListener(MouseEvent.MOUSE_UP, ClockStart);
startBTN.addEventListener(MouseEvent.MOUSE_OVER, mOver);
startBTN.addEventListener(MouseEvent.MOUSE_OUT, mOut);
// stop button
stopBTN.graphics.beginFill(0xCCFF00);
stopBTN.graphics.drawRect(160, 5, 40, 40);
this.addChild(stopBTN);
stopBTN.addEventListener(MouseEvent.MOUSE_UP, ClockStop);
stopBTN.addEventListener(MouseEvent.MOUSE_OVER, mOver);
stopBTN.addEventListener(MouseEvent.MOUSE_OUT, mOut);
// clock
Clock.addEventListener(TimerEvent.TIMER, this.Tick);
}
public function Tick(e:TimerEvent):void{
MiliSeconds++;
var s:int = Math.floor(MiliSeconds / 100);
var minutes:int = Math.floor( s / 60);
var seconds:int = s % 60;
var mili:int = MiliSeconds % 100 ;
myTextField.text = minutes + ":" + (seconds > 9 ? seconds : "0" + seconds) +":"+ (mili > 9 ? mili : "0" + mili);
}
public function mOver( e){
e.target.alpha=0.5
}
public function mOut( e ){
e.target.alpha =1
}
public function ClockStart(e){
MiliSeconds = 0;
Clock.start();
}
public function ClockStop(e){
Clock.stop();
}
}
}


