random array and user input logic test

going from actionscript 2 a2 to as3 can be tough, and this forum will show the road to as3. as3 is not best suited for small animation or web projects, and is in someways a red headed stepchild of java and c++. But, it still has a purpose, for now.
Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

random array and user input logic test

Post by darknkreepy3# »

Here is the actionscript code for google bots and people just looking for valid code in as3. It covers randomization, unique values, and array comparisons.

Here is the .FLA file compatible with as3 flash, flex etc...

remember to right click(pc) or option click(apple) and save as... to your system
http://www.supercala.net/tuts/as3/array_test.fla

Code: Select all

function addGuess(e:MouseEvent):void
	{
	var tgt:Object=e.target;
	trace(tgt.ID);
	}
//-----
function init_numPad():void
	{
	var colAmt:int=3;
	var rowAmt:int=3;
	var colPos:int=-1;
	var rowPos:int=0;
	
	var amt:int=colAmt*rowAmt;
	var posX:Number=0;
	var posY:Number=0;
	var bt_dist:Number=120;
	//
	for(var a:int=0;a<amt;a++)
		{
		colPos++;
		//
		if(colPos==colAmt)
			{
			colPos=0;
			rowPos++;
			}
		posX=colPos*bt_dist;
		posY=rowPos*bt_dist;
		
		T1.buttons[a]=new bt_num();
		var tgt:Object=T1.buttons[a];
		tgt.x=posX;
		tgt.y=posY;
		tgt.tF_num.text=a;
		tgt.mc_backart.ID=a;
		tgt.mc_backart.addEventListener(MouseEvent.CLICK,addGuess);
		/*
		since the tF_num texfield is ontop of mc_backart
		the textfield should be disabled for mouse events so any click in the square space will work
		*/
		tgt.tF_num.mouseEnabled=false;
		tgt.ic_chosen.visible=false;
		tgt.ic_random.visible=false;
		T1.root.pl_numPad.addChild(T1.buttons[a]);
		}
	}
//-----
function init_randomChoices():void
	{
	var total:int=3;
	var unique:Boolean=false;
	var ran:int=0;
	var b:int=0;
	//create 3 (0,1,2) unique random numbers
	for(var a:int=0;a<total;a++)
		{
		trace("*-----"+a+"-----*");
		//
		do
			{
			unique=true;
			ran=Math.random()*9;
				//loop through and check the ran number against all existing unique numbers in the array
				for(b=0;b<a;b++)
					{
					trace("("+b+")"+T1.randomChoices[b]+":"+ran);
					if(ran==T1.randomChoices[b])
						{
						unique=false;
						}
					}
			}
			while(unique==false);
		trace("unique:"+ran);	
		T1.randomChoices[a]=ran;
		T1.buttons[ran].ic_random.visible=true;
		}
	}
//-----
function init_GUI():void
	{
	init_numPad();
	init_randomChoices();
	}
//-----
function main():void
	{
	T1.guesses=new Array();
	T1.randomChoices=new Array();
	T1.buttons=new Array();
	init_GUI();
	}
var T1=new Object();
T1.root=this;
main();
Post Reply