Skip to main content
Inspiring
August 16, 2021
Answered

AS3 Use the same button and code in diferent frames

  • August 16, 2021
  • 2 replies
  • 204 views

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 has been closed for replies.
    Correct answer JoãoCésar17023019

    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

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    August 17, 2021

    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

    ATGBAuthor
    Inspiring
    August 17, 2021

    Thank you!

    That solved it!

    Best

    Ana 

    JoãoCésar17023019
    Community Expert
    Community Expert
    August 17, 2021

    Awesome!

     

    You're welcome!

    ATGBAuthor
    Inspiring
    August 17, 2021

    No help here? 😞