ActionScript Learning 101120

11.20.2010

PHPでサーバー時間を取得してFlashに表示する。

Flashでローカル時間ではなく、サーバーの時間を取得する必要があったので覚書しておく。

SAMPLE VIEW CODE

<?php
$year = date("Y");
$month = date("n");
$day = date("j");
$week = date("w");
$hour = date("G");
$minutes = date("i");
$seconds = date("s");

echo "year=".$year."&month=".$month."&day=".$day."&week=".$week."&hour=".$hour."&minutes=".$minutes."&seconds=".$seconds;

?>





package {
	
	import flash.events.*;
	import flash.display.*;
	import flash.text.*;
	import flash.net.*;

	public class NMain extends MovieClip {
		
		//-------------------------------------------------------------------------------- Properties
		
		private var _this:*;
		private var _stage:Stage;
		
		private var _main:MovieClip;
		
		
		
		//-------------------------------------------------------------------------------- Constractor
		
		public function NMain() {
			
			_this = this;
			
			_this.addEventListener(Event.ADDED_TO_STAGE, Init);
		}
		
		
		//-------------------------------------------------------------------------------- Function
		 
		/**
		 * initialize
		 */
		public function Init(eo:* = null):void {
			
			_this.removeEventListener(Event.ADDED_TO_STAGE, Init);
			
			_stage = stage;			
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.align = StageAlign.TOP_LEFT;		
			
			_main = new MovieClip();
			_this.addChild(_main);
			
			_stage.addEventListener(Event.RESIZE, stageresizeaction);
			stageresizeaction();
			
			inittext();
		}
		
		/**
		 * initialize text
		 */
		private var _texts:MovieClip;
		private var _tf_1:TextField;
		private var _tf_2:TextField;
		private var _tf_3:TextField;
		
		private function inittext():void {
			
			_texts = new MovieClip();
			
			var _t_1 = new t();
			_tf_1 = _t_1.tf;
			_texts.addChild(_tf_1);
			
			var _t_2 = new t();
			_tf_2 = _t_2.tf;
			_texts.addChild(_tf_2);
			
			var _t_3 = new t();
			_tf_3 = _t_3.tf;
			_texts.addChild(_tf_3);
			
			_tf_1.width = _tf_2.width = _tf_3.width = 1;
			_tf_1.height = _tf_2.height = _tf_3.height = 1;
			_tf_1.wordWrap = _tf_2.wordWrap = _tf_3.wordWrap = false;
			_tf_1.multiline = _tf_2.multiline = _tf_3.multiline = false;
			_tf_1.autoSize = _tf_2.autoSize = _tf_3.autoSize = "left";
			_tf_1.text = _tf_2.text = _tf_3.text = "";
			
			var format_1:TextFormat = new TextFormat();
			format_1.size = 40;
			
			var format_2:TextFormat = new TextFormat();
			format_2.size = 20;
			
			var format_3:TextFormat = new TextFormat();
			format_3.size = 14;
			
			_tf_1.defaultTextFormat = format_1;
			_tf_2.defaultTextFormat = format_2;
			_tf_3.defaultTextFormat = format_3;
			
			_main.addChild(_texts);
			
			getservertime();
		}
		/** */
		public function getservertime():void {
		
			var urlreq:URLRequest = new URLRequest("servertime.php");			
			urlreq.method = URLRequestMethod.GET;
			
			var urlloader:URLLoader = new URLLoader();			
			urlloader.dataFormat = URLLoaderDataFormat.VARIABLES;			
			urlloader.addEventListener(Event.COMPLETE, getservertime_complete);
			
			urlloader.load(urlreq);		
		}
		//
		private function getservertime_complete(eo:* = null):void {
		
			var urlvars:URLVariables = new URLVariables(eo.target.data);			

			for (var i:String in urlvars) {
			
				trace(i + ":" + urlvars[i]);
			}
			
			settexts(urlvars);
		}
		
		/**
		 * set texts
		 */
		private function settexts(datedata:*):void {
			
			var year = datedata["year"];
			var month = datedata["month"];
			var day = datedata["day"];
			
			var hour = datedata["hour"];
			var minutes = datedata["minutes"];
			var seconds = datedata["seconds"];
			
			var week = datedata["week"];
			
			var monthobj = getmonthobj(new Number(month));
			var month_jp = monthobj.jp;
			var month_en = monthobj.en;
			var month_txt = monthobj.txt;			
			
			var weekobj = getweekobj(new Number(week));
			var week_jp = weekobj.jp;
			var week_en = weekobj.en;
			var week_txt = weekobj.txt;
			
			_tf_1.text = year + " " + month + "/" + day + " " + hour + ":" +  minutes + ":" +  seconds;
			_tf_2.text = month_jp + " " + month_en + " " + month_txt;
			_tf_3.text = week_jp + " " + week_en + " " + week_txt;
			
			_tf_2.y = _tf_1.height;
			_tf_3.y = _tf_2.y + _tf_2.height;
			
			_texts.x = -Math.round(_texts.width / 2);
			_texts.y = -Math.round(_texts.height / 2);
		}
		/** */
		private function getmonthobj(monthnum:int):Object {
			
			var monthobj = {};
			
			switch (monthnum) {
			
				case 1:				
				monthobj.en = "January";	
				monthobj.jp = "1月";
				monthobj.txt = "The first month of the year.";
				break;
				
				case 2:				
				monthobj.en = "February";
				monthobj.jp = "2月";
				monthobj.txt = "The second month of the year.";
				break;
				
				case 3:
				monthobj.en = "March";
				monthobj.jp = "3月";
				monthobj.txt = "The third month of the year.";
				break;
				
				case 4:
				monthobj.en = "April";
				monthobj.jp = "4月";
				monthobj.txt = "The fourth month of the year.";
				break;
				
				case 5:
				monthobj.en = "May";
				monthobj.jp = "5月";
				monthobj.txt = "The fifth month of the year.";
				break;
				
				case 6:
				monthobj.en = "June";
				monthobj.jp = "6月";
				monthobj.txt = "The sixth month of the year.";
				break;
				
				case 7:				
				monthobj.en = "July";
				monthobj.jp = "7月";
				monthobj.txt = "The seventh month of the year.";		
				break;
				
				case 8:				
				monthobj.en = "August";
				monthobj.jp = "8月";
				monthobj.txt = "The eighth month of the year.";		
				break;
				
				case 9:				
				monthobj.en = "September";
				monthobj.jp = "9月";
				monthobj.txt = "The ninth month of the year.";		
				break;
				
				case 10:				
				monthobj.en = "October";
				monthobj.jp = "10月";
				monthobj.txt = "The tenth month of the year.";		
				break;
				
				case 11:				
				monthobj.en = "November";
				monthobj.jp = "11月";
				monthobj.txt = "The 11th month of the year.";		
				break;
				
				case 12:				
				monthobj.en = "December";
				monthobj.jp = "12月";
				monthobj.txt = "The 12th month of the year.";		
				break;
				
				default:				
				break;
			}
			
			return monthobj;
		}
		/** */
		private function getweekobj(weeknum:int):Object {
			
			var weekobj = {};
			
			switch (weeknum) {
			
				case 0:				
				weekobj.en = "Sunday";	
				weekobj.jp = "日曜日";
				weekobj.txt = "The first day of the week.";
				break;
				
				case 1:				
				weekobj.en = "Monday";
				weekobj.jp = "月曜日";
				weekobj.txt = "The second day of the week.";
				break;
				
				case 2:
				weekobj.en = "Tuesday";
				weekobj.jp = "火曜日";
				weekobj.txt = "The third day of the week.";
				break;
				
				case 3:
				weekobj.en = "Wednesday";
				weekobj.jp = "水曜日";
				weekobj.txt = "The fourth day of the week.";
				break;
				
				case 4:
				weekobj.en = "Thursday";
				weekobj.jp = "木曜日";
				weekobj.txt = "The fifth day of the week.";
				break;
				
				case 5:
				weekobj.en = "Friday";
				weekobj.jp = "金曜日";
				weekobj.txt = "The sixth day of the week.";
				break;
				
				case 6:				
				weekobj.en = "Saturday";
				weekobj.jp = "土曜日";
				weekobj.txt = "The seventh day of the week.";		
				break;
				
				default:				
				break;
			}
			
			return weekobj;
		}

		
		/**
		 * stage resize
		 */
		private function stageresizeaction(eo:* = null):void {
			
			_main.x = Math.round(_stage.stageWidth / 2);
			_main.y = Math.round(_stage.stageHeight / 2);
		}
	}
}


category : ActionScript / Flash / PHP

Demonstrations

Feature Samples

Author

虹村 マキオウ (nizimura makiou)

猫と太極拳を愛する横浜在住のフリーランスクリエイターです。

logo

Demo and Sample

Category