A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Just wondering if there is an easier way....

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    15

    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?

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    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.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    15
    Great... I think that should work. Many thanks

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    15
    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...

  5. #5
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    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]);
            }
        }
    }
    Follow me on Twitter: http://twitter.com/jasondefra

  6. #6
    Junior Member
    Join Date
    Jan 2010
    Posts
    15
    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]);

    }
    }

    }

  7. #7
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    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.
    Follow me on Twitter: http://twitter.com/jasondefra

  8. #8
    Junior Member
    Join Date
    Jan 2010
    Posts
    15
    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

  9. #9
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    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.
    Follow me on Twitter: http://twitter.com/jasondefra

  10. #10
    Junior Member
    Join Date
    Jan 2010
    Posts
    15

    Thumbs up

    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center