Flash CS5 AS3 Looping

Hello,

I'm trying to set up a loop using actionscript 3 that will call a function to check if a token has been dropped on any of the 40 boxes on my stage. Each box has its own instance ID of target1, target2 etc...

The script is as follows at the moment, but currently it only completes the trace() for target40.

Can somebody explain to me why this is / what I've got to do to make the hittestobject script work across all 40 boxes?

EE Stuff link as can't load flash attachments here.

Thanks

for(var j:uint = 1; j <= 40; j++){
	var targetid:SimpleButton = getChildByName("target" + j) as SimpleButton;
	var tstring:String = new String("Target " + j);
	if(targetid != null){
		targetid.tabIndex = j;
		targetid.addEventListener(Event.ENTER_FRAME,
		function(yourEvent:Event):void{
		if (Token.hitTestObject(targetid)){
		trace(tstring);
		}
		})
		};
	};

Open in new window

bedsingarAsked:
Who is Participating?
 
CyanBlueConnect With a Mentor Commented:
I'd do something like this.

CyanBlue
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
import flash.net.URLRequest;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;


stop();//To pause on 1st frame


var startX:Number;
var startY:Number;

Token.addEventListener(MouseEvent.MOUSE_DOWN, TokenStart);
function TokenStart(TokenMove:Event):void
{
	Token.startDrag();
}

Token.addEventListener(MouseEvent.MOUSE_UP, TokenStop);
function TokenStop(TokenMove:Event):void
{
	Token.stopDrag();
	Token.stopDrag();
}

Token.buttonMode = true;

var isTokenSelected:Boolean = false;
var droppedTarget:int;

Token.addEventListener(MouseEvent.MOUSE_DOWN, onTokenClick);
Token.addEventListener(MouseEvent.MOUSE_UP, onTokenRelease);
Token.addEventListener(MouseEvent.MOUSE_MOVE, onTokenMove);

function onTokenClick(e:MouseEvent):void
{
	isTokenSelected = true;
}

function onTokenRelease(e:MouseEvent):void
{
	isTokenSelected = false;
	BoardText.text = "Dropped Target : " + droppedTarget;
}

function onTokenMove(e:MouseEvent):void
{
//	trace("isTokenSelected = " + isTokenSelected);
	if (isTokenSelected == true)
	{
		droppedTarget = 0;
		for (var j:uint = 1; j <= 40; j++)
		{
			var targetid:SimpleButton = getChildByName("target" + j) as SimpleButton;
			var tstring:String = "Target " + j;
			if (targetid != null)
			{
				targetid.tabIndex = j;
				if (Token.hitTestObject(targetid))
				{
					BoardText.text = "hitTestObject : " + tstring;
					droppedTarget = j;
				}
			}
		}
	}
}


for (var i:uint = 1; i <= 40; i++)
{
	var button:SimpleButton = getChildByName("target" + i) as SimpleButton;
	if (button != null)
	{
		button.tabIndex = i;
		button.addEventListener(MouseEvent.CLICK, mouseClick);

		//board text with tracer
		button.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
	}
}

function mouseClick(event:MouseEvent):void
{
	var index:uint = event.currentTarget.tabIndex;
	var IFRM_URL:URLRequest = new URLRequest("pages/00" + index + ".html");
	navigateToURL(IFRM_URL, 'IFRM01');
}

function mouseOver(event:MouseEvent):void
{
	var index:uint = event.currentTarget.tabIndex;
//	trace(index + " + "+ event.currentTarget);
	BoardText.text = ("Target " + index + " Hit") ;
}

Open in new window

0
 
bedsingarAuthor Commented:
Brilliant that works perfectly :)

So if I want to trigger the follow URL code for the box that the token is dropped on, I can add this code to the onTokenRelease function right?

Thanks for your help!
0
 
bedsingarAuthor Commented:
Solution worked perfectly & I can follow the logic so thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.