[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Flash CS5 AS3 Looping

Posted on 2012-09-08
3
Medium Priority
?
1,053 Views
Last Modified: 2016-08-29
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

0
Comment
Question by:bedsingar
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
CyanBlue earned 2000 total points
ID: 38379895
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
 

Author Comment

by:bedsingar
ID: 38379916
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
 

Author Closing Comment

by:bedsingar
ID: 38379929
Solution worked perfectly & I can follow the logic so thanks for your help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question