WEB開発のあれこれ PHP,JAVA,Javascript,objective-c,Ajax,Flex,Air,Linuxなどなど 最近雑記帳になりつつある・・・ 2011.10.23より忍者ブログより移転
<?version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:controller="*" width="210" height="250"paddingTop="3" paddingBottom="3" paddingLeft="10" paddingRight="10" horizontalScrollPolicy="off" verticalScrollPolicy="off">"controller" /><mx:Canvas id="mainPanel" width="200" height="200" styleName="mainPanel"><mx:Label id="getPoint" text="□" width="10" height="10" /><mx:Label id="charactor" text="■" width="10" height="10" x="0" y="0" />mx:Canvas><mx:Button id="startGame" label="リスタート" />mx:Application>
package controller{import flash.display.DisplayObject;import flash.events.KeyboardEvent;import flash.events.MouseEvent;import flash.events.TimerEvent;import flash.geom.Point;import flash.ui.Mouse;import flash.utils.Timer;import mx.controls.*;import mx.core.IMXMLObject;import mx.events.FlexEvent;import mx.events.*;public class controller implements IMXMLObject{private var v:Main;private var toArrow:int = 39;private var t:Timer;private var cArray:Array = new Array();/** * 初期化処理 */public function initialized(document:Object, id:String):void{v = document as Main;v.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);}/** * アプリケーション初期化処理 */public function creationCompleteHandler(evt:FlexEvent):void{v.mainPanel.setFocus();randomPoint();v.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownForMainPanelHandler);t = new Timer(100);t.addEventListener(TimerEvent.TIMER, onTimerForCharatorHandler);v.startGame.addEventListener(MouseEvent.CLICK, onClickStartGameHandler);t.start();}private function onClickStartGameHandler(evt:MouseEvent):void{if (!t.running) {toArrow = 39;var cnt:int = v.mainPanel.numChildren;var tgt:int = v.mainPanel.getChildIndex(v.charactor as DisplayObject);for (var i:int = cnt -1; i > 0; i--) {if (i != tgt) {v.mainPanel.removeChildAt(i);}}cArray = new Array();v.charactor.x = 0;v.charactor.y = 0;t.start();}}private function onTimerForCharatorHandler(evt:TimerEvent):void{var lbl:Label = new Label();lbl.text = "■";lbl.width = 10;lbl.height = 10;lbl.x = v.charactor.x;lbl.y = v.charactor.y;switch(toArrow){case 37:// ← if (v.charactor.x - 10 < 0) {Alert.show("ゲーム終了n" + cArray.length.toString() + "点", "終了");t.stop();return;}v.charactor.x -= 10;break;case 38:// ↑ if (v.charactor.y - 10 < 0) {Alert.show("ゲーム終了n" + cArray.length.toString() + "点", "終了");t.stop();return;}v.charactor.y -= 10;break;case 39:// → if (v.charactor.x + 10 >= 200) {Alert.show("ゲーム終了n" + cArray.length.toString() + "点", "終了");t.stop();return;}v.charactor.x += 10;break;case 40:// ↓ if (v.charactor.y + 10 >= 200) {Alert.show("ゲーム終了n" + cArray.length.toString() + "点", "終了");t.stop();return;}v.charactor.y += 10;break;}if (v.getPoint.x == v.charactor.x && v.getPoint.y == v.charactor.y) {randomPoint();cArray.push(new Point(v.getPoint.x, v.getPoint.y));var lbll:Label = new Label();lbll.text = "■";lbll.width = 10;lbll.height = 10;lbll.x = v.charactor.x;lbll.y = v.charactor.y;v.mainPanel.addChildAt(lbll, 1);}v.mainPanel.addChildAt(lbl, 1);cArray.splice(0,0, new Point(v.charactor.x, v.charactor.y));// 配列の最後のポイントを取得→削除 v.mainPanel.removeChildAt(cArray.length);cArray.pop();}private function onKeyDownForMainPanelHandler(evt:KeyboardEvent):void{trace(evt.keyCode);switch(evt.keyCode){case 37:// ← //v.charactor.x -= (v.charactor.x != 0)? 10: 0; if (toArrow != 39) {toArrow = 37;}break;case 38:// ↑ //v.charactor.y -= (v.charactor.y != 0)? 10: 0; if (toArrow != 40) {toArrow = 38;}break;case 39:// → //v.charactor.x += (v.charactor.x != 190)? 10: 0; if (toArrow != 37) {toArrow = 39;}break;case 40:// ↓ //v.charactor.y += (v.charactor.y != 190)? 10: 0; if (toArrow != 38) {toArrow = 40;}break;}}private function randomPoint():void{v.getPoint.x = Math.floor(Math.random() * 20) * 10;v.getPoint.y = Math.floor(Math.random() * 20) * 10;for (var i:String in cArray) {if ((new Point(v.getPoint.x, v.getPoint.y)).equals(cArray[i])) {randomPoint();}}}}}
0 件のコメント:
コメントを投稿