0 Replies - 1924 Views - Last Post: 05 April 2010 - 12:52 PM Rate Topic: -----

#1 Guest_William*


Reputation:

random sequence array for AS2.0 - 3.0

Posted 05 April 2010 - 12:52 PM

So this was a project for my tech school to make a simon game using action-script 2.0
i was able to make the game have a preset sequence array for the for the computers turn
however i would like to know how i can improve on this to make each position of my array,
randomly generated. i thought maybe if i created a new variable set to a number called "RanNum"
and in its function set the math.random function to produce a number 0-3 one for each direction key.

Please let me know if their is an easier way to accomplish this result but anyway here is my code, and i can upload the FLA if needed

onClipEvent (load)
{
	//declaring and setting variables 
	var isCompTurn:Boolean = true;
	var isPlayerTurn:Boolean = false;
	var isEndTurn:Boolean = false;
	var isGameOver:Boolean = false;
	var seq:Array = new Array();
	seq = [["l", "l", "l", "r", "u", "d"], ["u", "u", "d", "d", "l", "r", "l", "r"], ["r", "r", "u", "d", "r", "l", "r"], ["d", "u", "d", "d", "u", "l", "l", "d"], ["u", "r", "u", "d", "u", "u", "l"]];
	var seqNum:Number = 0;
	var seqPos:Number = 0;
	var timer:Number = 0;
	

	//shortcut to targert path
	var cToy:MovieClip = this._parent.cToy;
	var pToy:MovieClip = this._parent.pToy;
	var spotLight:MovieClip = this._parent.spotLight;
	var bingo:MovieClip = this._parent.mBingo;
	var miss:MovieClip = this._parent.mMiss;
	var statGameOver:MovieClip = this._parent.statGameOver;
	var statInstr:MovieClip = this._parent.statInstr;
	var statYourTurn:MovieClip = this._parent.statYourTurn;
	var statGotIt:MovieClip = this._parent.statGotIt;
	var statWinner:MovieClip = this._parent.statWinner;
	

	//make signs invis
	bingo.gotoAndStop("blank");
	miss.gotoAndStop("blank");
	statGameOver.gotoAndStop("blank");
	statInstr.gotoAndStop("blank");
	statYourTurn.gotoAndStop("blank");
	statGotIt.gotoAndStop("blank");
	statWinner.gotoAndStop("blank");

	//delay function to slow down commands
	function delay(delayAmount:Number):Boolean
	{
		if (timer > delayAmount * 24)
		{
			timer = 0;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	//gets keys from users
	_root.onkeydown = function()
	{
		if (isGameOver && (Key.getCode() == Key.SPACE))
		{
			//restart
			isGameOver = false;
			//reset
			spotLight._x = 160;
			statGameOver.gotoAndStop("blank");
			statInstr.gotoAndStop("blank");

		}

		if (isPlayerTurn)
		{
			trace("pressing key : " + Key.getCode());
			var input:String;
			if (Key.getCode() == Key.UP)
			{
				pToy.gotoAndPlay("up");
				input = "u";
			}
			if (Key.getCode() == Key.DOWN)
			{
				pToy.gotoAndPlay("down");
				input = "d";
			}
			if (Key.getCode() == Key.LEFT)
			{
				pToy.gotoAndPlay("left");
				input = "l";
			}
			if (Key.getCode() == Key.RIGHT)
			{
				pToy.gotoAndPlay("right");
				input = "r";
			}

			if (input == seq[seqNum][seqPos])
			{
				trace("you got it right");
				bingo.gotoAndPlay(1);
				seqPos++;
			}
			else
			{
				miss.gotoAndPlay(1);
				isGameOver = true;
				//display game over
				statGameOver.gotoAndPlay(1);
				//display instr sign
				statInstr.gotoAndPlay(1);

			}

		}
		//end if playerturn       
	};
	Key.addListener(_root);
}

//GAME LOOP
onClipEvent (enterFrame) 
{
	if (isGameOver)
	{
		trace("gameover");
		isPlayerTurn = false;
		isCompTurn = true;
		seqNum = 0;
		seqPos = 0;
		timer = 0;
		isEndTurn = false;
		
		
	}
	else
	{
		//increment for delay
		timer++;
		//if player turn 
		if (isPlayerTurn)
		{
			//check if the seq is complete
			if (seqPos == seq[seqNum].length)
			{
				//trigger delay state
				trace("Waiting for DELAY");
				//setting player turn false
				//deactive input
				isPlayerTurn = false;
				//end of turn state
				isEndTurn = true;
				//reset timeer
				timer = 0;

			}
		}
		// if end of turn and delay is reatched 
		if (isEndTurn && delay(3))
		{
			//switch to computer
			isCompTurn = true;
			isEndTurn = false;
			seqNum++;
			seqPos = 0;
			spotLight._x = 160;

		}


		if (isPlayerTurn)
		{
			if (seqPos == seq[seqNumer].length)
			{
				isPlayerTurn = false;
				isCompTurn = true;
				seqNum++;

			}
		}

		if (isCompTurn)
		{

			//checking to see if there are any sequence postions
			if (seqPos < seq[seqNum].length)
			{
				//delay function inside
				if (delay(0.5))
				{
					//temp variable that contains possition
					var p:String;
					p = seq[seqNum][seqPos];
					//check possition and play animation
					switch (p)
					{
						case "u" :
							cToy.gotoAndPlay("up");
							break;
						case "d" :
							cToy.gotoAndPlay("down");
							break;
						case "l" :
							cToy.gotoAndPlay("left");
							break;
						case "r" :
							cToy.gotoAndPlay("right");
							break;

					}


					trace(seq[seqNum][seqPos]);
					seqPos++;
				}

			}
			else
			{
				//check for delay
				if (delay(0.5))
				{
					trace("Comp turn over...Player Turn Start");
					isCompTurn = false;
					isPlayerTurn = true;
					seqPos = 0;
					spotLight._x = 535;
				}
			}//end if - check seqPos

		}
		//end if - check isCompTurn        
	}
}


Is This A Good Question/Topic? 0

Page 1 of 1