Skip to main content
Known Participant
October 11, 2019
質問

How can I stops actions/code at a certain frame?

  • October 11, 2019
  • 返信数 1.
  • 194 ビュー

So I have a fairly simple HTML5 project going with basic button navigation.  I navigate to one section, then back to the main menu, then to other sections etc.  I'm finding that when I launch the project and go to any given section of it, it works fine.  But when I go back and forth between the main section and other sections it gets increasingly more glitchy.

 

My best guess is that everytime I return to the first frame I'm retriggering the code there, which is conflicting with the code that is already 'running' if that makes sense.  What I think will solve the problem is if I add a line of code on the first frame that will end all currently active event listeners so that the code is essentially restarting fresh every time the user returns to the main menu to avoid any conflicting actions.

 

I'm pretty sure I've seen this done, and may have even done it myself at some point but I don't remember how to do it and I can't find a tutorial or example of it online.

 

Please help!

 

Thanks!

    このトピックへの返信は締め切られました。

    返信数 1

    JoãoCésar17023019
    Community Expert
    Community Expert
    October 14, 2019

    Hi.

     

    Yeah. You're right. When you revisit frames, code is run again if you don't do some check. In your case, probably you keep re-adding listeners.

     

    One easy way to avoid this is to check if the current frame is running for the first time by checking a custom property. For example:

     

    var root = this;
    
    if (root.frame0Started)
    {
        root.yourButton.on("click", root.yourEventHandler);
        root.frame0Started = true;
    }

     

    I hope this helps.

     

     

    Regards,

    JC