アットウィキロゴ
Fieds_labo4
掲示板 掲示板 ページ検索 ページ検索 メニュー メニュー

Fieds_labo4

Practice-life01

最終更新:

fieds_labo4

- view
管理者のみ編集可

ライフゲームもどき

はじめてのActionScript 3~ライフゲームを作ってみる
http://codezine.jp/article/detail/627
をFlex3SDKで演習。
ライフゲームがよくわかってなかったりします^^;
  • Timerの値が小さいとクリックがうまく拾えなかった。PCの問題かな?
  • START-STOP-CLEAR-STARTを繰り返すと300Kずつ確保メモリが増えていきます。致命的@@;
  • どこが悪いんだぁ~

  • Creature.as
package  
{
    import flash.display.*;
    import flash.events.*;
	/**
	 * ...
	 * @author ss
	 */
    public class Creature extends Sprite
    {
        // embed image - relational path
        [Embed(source='../image/dead2.gif')] private static const ImageDead:Class;
        [Embed(source='../image/live.gif')] private static const ImageLive:Class;
		// bitmap status
		private var cur_bmp:Bitmap = null;
		private var isLive:Boolean = false;
 
		public function Creature()
		{
			setDead();
			addEventListener(MouseEvent.CLICK, onClick);
		}
		private function onClick(evt:MouseEvent):void {
			(isLive) ? setDead() : setLive();
		}
		private function removeBmp():void {
			if (cur_bmp != null) removeChild(cur_bmp);
		}
		private function changeImage(bmpClass:Class):void {
			removeBmp();
			cur_bmp = new bmpClass as Bitmap;
			addChild(cur_bmp);
			isLive = (bmpClass == ImageDead) ? false : true;
		}
		public function setLive():void {
			changeImage(ImageLive);
		}
		public function setDead():void {
			changeImage(ImageDead);
		}
		public function getAlive():Boolean {
			return isLive;
		}
    }
 
 
}
 

  • Main.as
package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.Timer;
 
	/**
	 * ...
	 * @author ss
	 */
	public class Main extends Sprite 
	{
        [Embed(source='../image/start.gif')] private static const StartImg:Class;
        [Embed(source='../image/stop.gif')] private static const StopImg:Class;
        [Embed(source='../image/clear.gif')] private static const ClearImg:Class;
        private var map:Array;
        private const MAX_ROWS:int = 20;
        private const MAX_COLS:int = 32;
        private var ns_timer:Timer;
		private var startBtn:SimpleButton;
		private var stopBtn:SimpleButton;
		private var clearBtn:SimpleButton;
 
 
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			pre_process();
		}
 
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
		}
		private function pre_process():void {
			var btnimg1:Bitmap = new StartImg();
			var btnimg2:Bitmap = new StopImg();				
			var btnimg3:Bitmap = new ClearImg();				
			// SimpleButton(upState:DisplayObject = null, overState:DisplayObject = null, 
			//			downState:DisplayObject = null, hitTestState:DisplayObject = null)
			startBtn = new SimpleButton(btnimg1, btnimg1, btnimg1, btnimg1);
			stopBtn = new SimpleButton(btnimg2, btnimg2, btnimg2, btnimg2);
			clearBtn = new SimpleButton(btnimg3, btnimg3, btnimg3, btnimg3);
			startBtn.x = 0;
			startBtn.y = 0;
			stopBtn.x = 80;
			stopBtn.y = 0;
			clearBtn.x = 160;
			clearBtn.y = 0;
			// map object declare
            map = new Array();
            for (var i:int = 0; i < (MAX_ROWS * MAX_COLS); i++) map[i] = new Creature();
			//
			addChild(startBtn);
			addChild(stopBtn);
			addChild(clearBtn);
			startBtn.addEventListener(MouseEvent.CLICK, startBtnClickListener);
			stopBtn.addEventListener(MouseEvent.CLICK, stopBtnClickListener);
			clearBtn.addEventListener(MouseEvent.CLICK, clearBtnClickListener);
		}
		private function startBtnClickListener(e:MouseEvent):void {
			// startBtn.enabled = false;
			LifeGame();
			removeChild(clearBtn);
			removeChild(startBtn);
		}
		private function stopBtnClickListener(e:MouseEvent):void {
			ns_timer.stop();
			// removeEventListener(TimerEvent.TIMER, onTick);
			// startBtn.enabled = true;
			addChild(startBtn);
			addChild(clearBtn);
		}
		private function clearBtnClickListener(e:MouseEvent):void {
            for (var i:int = 0; i < (MAX_ROWS * MAX_COLS); i++) {
				//image clear
 //				if (map[i].getAlive() == true) {
 //					map[i].setDead();
 //					addChild(map[i]);
 //				}
				if (map[i].getAlive() == true) map[i].setDead();
				addChild(map[i]);
            }
		}
		public function LifeGame():void {
            //map = new Array();
            for (var i:int = 0; i < (MAX_ROWS * MAX_COLS); i++) {
                var x:int = i % MAX_COLS;
                var y:int = i / MAX_COLS;
                map[i].x = x * map[i].width;
                map[i].y = y * map[i].height + 30;
                if (Math.random() > 0.7) map[i].setLive();
                addChild(map[i]);
            }
			ns_timer = new Timer(1000);
			ns_timer.addEventListener(TimerEvent.TIMER, onTick);
			ns_timer.start();
        }
		private function onTick(eve:TimerEvent):void {
			var i_pos:int = Math.floor(Math.random() * 640);
            var x_pos:int = i_pos % MAX_COLS;
            var y_pos:int = i_pos / MAX_COLS;
			var live_counter:int = 0;
			// x-1,y-1
			if ((x_pos > 0) && (y_pos > 0)) {
				if (map[(i_pos - 33)].getAlive() == true) live_counter++ ;
			}
			// x,y-1
			if (y_pos > 0) {
				if (map[(i_pos - 32)].getAlive() == true) live_counter++ ;
			}
			// x+1,y-1
			if ((x_pos < 31) && (y_pos > 0)) {
				if (map[(i_pos - 31)].getAlive() == true) live_counter++ ;
			}
			// x-1,y
			if (x_pos > 0) {
				if (map[(i_pos - 1)].getAlive() == true) live_counter++ ;
			}
			// x+1,y
			if (x_pos < 31) {
				if (map[(i_pos + 1)].getAlive() == true) live_counter++ ;
			}
			// x-1,y+1
			if ((x_pos > 0) && (y_pos < 19)) {
				if (map[(i_pos + 31)].getAlive() == true) live_counter++ ;
			}
			// x,y+1
			if (y_pos < 19) {
				if (map[(i_pos + 32)].getAlive() == true) live_counter++ ;
			}
			// x+1,y+1
			if ((x_pos < 31) && (y_pos < 19)) {
				if (map[(i_pos + 33)].getAlive() == true) live_counter++ ;
			}
			if (map[i_pos].getAlive() == true) { // live
				if ((live_counter == 2) || (live_counter == 3)) map[i_pos].setLive();
				else map[i_pos].setDead();
			}
			else { // dead
				if (live_counter == 3) map[i_pos].setLive();
				else map[i_pos].setDead();
			}
		}
 
	}
 
}
 




記事メニュー
最近更新されたスレッド
ウィキ募集バナー