2008-11-05(Wed) 16:46
[AS3.0]時計を作ってみる(3)

前回の続き。
9以下の場合10の位に0を代入する場合、if文でもいいかもですが、
以下のようにすると簡単にできます。
(5 < 10 ? "0" : "")
10より小さい場合0になり10より多い場合なにもない。

試しに日の取得で書くと、
date_txt.text = (date.date < 10 ? "0" : "") + date.date;
こんな感じになると思います。

これでとりあえず前回の問題は解決。

次に、Flashなんでリアルタイムで秒数が動いてる感じにしたいと思います。
EventListenerのところに今回作ったのを全部いれちゃいます。

addEventListener(Event.ENTER_FRAME, enterframeTimer);
function enterframeTimer(event):void {
	var date : Date = new Date();
	//年を取得
	year_txt.text = String(date.fullYear);
	//月を取得
	var month = date.month + 1;
	month_txt.text = (month < 10 ? "0" : "") + month;
	//日を取得
	date_txt.text  = (date.date < 10 ? "0" : "") + date.date;
	//曜日配列
	var ary = ["sun","mon","tue","wed","thu","fri","sat"];
	day_txt.text  = String(ary[date.day]);
	//時間を取得
	hours_txt.text = (date.hours < 10 ? "0" : "") + date.hours;
	//分を取得
	minutes_txt.text = (date.minutes < 10 ? "0" : "") + date.minutes;
	//秒を取得
	seconds_txt.text = (date.seconds < 10 ? "0" : "") + date.seconds;
}

これでとりあえず動いてます。


Comment Form
Name
E-mail
URL
Comment

TrackBack URL