-
Just wondering if there is an easier way....
My production includes a sitemap which will have at least 50 items, each of which points to a different label in the timeline. It's easy enough to put a standard down-button event handler and function on each item, but that's a LOT of code because of the number of buttons.
I was wondering if there is a simpler way of doing this? Something like loading the instance names into a variable? Any ideas?
-
newb of many sorts
you could give each button a name that's identical to the timeline label. Then when you click a button, go to that frame...
Code:
Btn1.name = 'label1';
Btn2.name = 'label2';
Btn3.name = 'label3';
Btn1.addEventListener(MouseEvent.CLICK, gotoLabel);
Btn2.addEventListener(MouseEvent.CLICK, gotoLabel);
Btn3.addEventListener(MouseEvent.CLICK, gotoLabel);
function gotoLabel(e:MouseEvent):void
{
gotoAndStop(e.target.name);
}
Search first, asked questions later.
-
Great... I think that should work. Many thanks
-
Actually, that didn't work. I get the error - "The name property of a Timeline-placed object cannot be modified"
Not sure what to make of it...
-
Bald By Choice
I would store all the buttons/movieclips in an Array and use that to pair up appropriate labels/strings. This way, you only have to build one array. Contrary to Ralgoth's suggestion, I would use just one event event listener (rather than one for each of the 50 movieclips) and then check which button was hit in the method using a for loop—less code and (probably) more resource efficient.
Actionscript Code:
var btnArray:Array=[[mc1,"label1"],[mc2,"label2"],[mc3,"label3"],[mc4,"label4"],[mc5,"label5"],[mc6,"label6"],[mc7,"label7"]];
addEventListener(MouseEvent.CLICK,onClick,false,0,true);
function onClick(e:MouseEvent):void{ for(var i:int=0;i<btnArray.length;i++){ if(e.target==btnArray[i][0]){ trace(btnArray[i][1]); } } }
-
Hi Jasondefra, thanks for that suggestion. I ran a test and it works fine. I am displaying the strings in the array in the output window when I click the associate button. But, in my presentation in need to move the playhead to a frame in the movie when each button is clicked. What code do I need to add to do that? (Sorry, I'm an action script newbie...)
This is my code in my test.
stop();
var btnArray:Array=[[btn_10,"ten"],[btn_20,"twenty"],[btn_30,"thirty"]];
addEventListener(MouseEvent.CLICK,onClick,false,0, true);
function onClick(e:MouseEvent):void {
for (var i:int=0; i<btnArray.length; i++) {
if (e.target==btnArray[i][0]) {
trace(btnArray[i][1]);
}
}
}
-
Bald By Choice
As long as your frame locations are appropriately labeled, it should just be a matter of putting this line underneath the trace(btnArray[i][1]); statement:
Actionscript Code:
gotoAndStop(btnArray[i][1]);
Let me know if that works for you.
-
Perfect! Thanks so much, it's an elegant solution to what would have otherwise been a scrambled egg of code.
Just one other question, as the frame destinations would be in the parent movie clip would I simply change the code to read:
MovieClip(parent).gotoAndStop("info_enrol_eonline" ,"main");(btnArray[i][1]);
Is that correct?
Thanks
-
Bald By Choice
This is how you'll want to do it:
Actionscript Code:
MovieClip(parent).gotoAndStop(btnArray[i][1]);
edit: This is assuming you're not messing with multiple scenes.
-
Of course.. silly me. Thanks so much Jason I really appreciate your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|