Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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