Skip to main content
Participating Frequently
September 22, 2011
Answered

new to AC3.. a bit of help please

  • September 22, 2011
  • 1 reply
  • 468 views

Hi all,

     First off i am sorry if i did not tag the code peoperly (i am not sure how i am supposed to that).

it has been a while since i used flash, and now i am using it for ranch program with lots of trees etc....

  Here is what i am trying to do:

button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage);

function fl_ClickToGoToWebPage(event:MouseEvent):void

{

    navigateToURL(new URLRequest("ranch.php?id=1"), "tdes");

}

button_2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage);

function fl_ClickToGoToWebPage(event:MouseEvent):void

{

    navigateToURL(new URLRequest("ranch.php?id=2"), "tdes");

}

and so on... i have 130 of these... do I have to do them manually one by one, or is there a way to incorporate a loop.

a bit more complicated i guess:

is there a way to check what buttons (instances) are there first and excucute only when instance button_x is available .. would it matter.

I mean let us say

button_12.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage);

function fl_ClickToGoToWebPage(event:MouseEvent):void

{

    navigateToURL(new URLRequest("ranch.php?id=12"), "tdes");

}

But there is no instance name button_12 or will it just pass through it and continue the loop (if a loop is possible) or will it cause an error and stop the loop there..

This topic has been closed for replies.
Correct answer kglad

you can use:

for(var i:int=1;i<=130;i++{

if(this["button_"+i]){

this["button_"+i].addEventListener(MouseEvent.CLICK,f);

}

}

function f(e:MouseEvent):void{

var n:String=e.currentTarget.name.split("_")[1];

navigateToURL(new URLRequest("ranch.php?id="+n),"tdes");

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
September 22, 2011

you can use:

for(var i:int=1;i<=130;i++{

if(this["button_"+i]){

this["button_"+i].addEventListener(MouseEvent.CLICK,f);

}

}

function f(e:MouseEvent):void{

var n:String=e.currentTarget.name.split("_")[1];

navigateToURL(new URLRequest("ranch.php?id="+n),"tdes");

}