Skip to main content
Inspiring
Aug 16, 2021 4:06 PM UTC
Answered

AS3 Use the same button and code in diferent frames

Hi!

This is probably very basic... but...

in as3 (air for desktop) is there a way of, inside a movieclip, use the same code (goto1) , for the exactly sabe button along different keyframes without need to repeat the code several times?

I am able to do it if I start in frame 1, but not if I star it in frame 2.

 

Thank you all

 

    This topic is closed to new replies. Start a new post to keep the conversation going.
    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Aug 17, 2021 11:48 AM UTC

    Hi.

     

    An easy approach is to add a mouse event listener to the parent and check for the name property. Like this:

     

    import flash.events.MouseEvent;
    
    function clickHandler(e:MouseEvent):void
    {
    	if (e.target.name === "yourButtonName")
    		gotoAndStop(2);
    }
    
    stop();
    addEventListener(MouseEvent.CLICK, clickHandler);

     

     

    In this way you don't need to worry about re-adding the listener everywhere.

     

    I hope this helps.

     

    Regards,

    JC

    4 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    Aug 17, 2021 11:48 AM UTC

    Hi.

     

    An easy approach is to add a mouse event listener to the parent and check for the name property. Like this:

     

    import flash.events.MouseEvent;
    
    function clickHandler(e:MouseEvent):void
    {
    	if (e.target.name === "yourButtonName")
    		gotoAndStop(2);
    }
    
    stop();
    addEventListener(MouseEvent.CLICK, clickHandler);

     

     

    In this way you don't need to worry about re-adding the listener everywhere.

     

    I hope this helps.

     

    Regards,

    JC

    Translate
    ATGBAuthor
    Inspiring
    Aug 17, 2021 2:40 PM UTC

    Thank you!

    That solved it!

    Best

    Ana 

    Translate
    JoãoCésar17023019
    Community Expert
    Community Expert
    Aug 17, 2021 2:43 PM UTC

    Awesome!

     

    You're welcome!

    Translate
    ATGBAuthor
    Inspiring
    Aug 17, 2021 9:22 AM UTC

    No help here? 😞

     

    Translate