Skip to main content
Inspiring
June 29, 2020
Answered

Simple button frame navigation in HTML5 stops working

  • June 29, 2020
  • 1 reply
  • 443 views

I have been trying out simple navigation between two frames (frame 0 and frame 4) with a button. 

 

on the first frame I just have a text and a button with the code:

 

this.stop();

this.btn_scena.addEventListener("click", fl_ClickToGoToAndStopAtFrame.bind(this));

function fl_ClickToGoToAndStopAtFrame()
{
this.gotoAndStop(4);
}

 

and on frame 4:

 

this.btn_scena.addEventListener("click", fl_ClickToGoToAndStopAtFrame.bind(this));

function fl_ClickToGoToAndStopAtFrame()
{
this.gotoAndStop(0);
}

 

 

It works for about 10 times and then stops, since I just used Code Snippets and didn't make my own code I'm not sure how this could possibly not work. 

Thanks in advance. 

 

    This topic has been closed for replies.
    Correct answer veliborm2817778

    then you must have "different" btn_scena's on frame 0 and frame 4.  to remedy, move btn_scena to its own layer in frame 0 and extend (if needed) its layer to frame 4 with no other keyframes.  remove any other btn_scena's.


    There alredy was only one button with its own layer. 

    However you did give me an idea.

    For whatever reason, it works if the button has a different instance name in frame 0 and 4. 

    Thanks for the help!

     

    1 reply

    kglad
    Community Expert
    Community Expert
    June 29, 2020

    i'm not sure why it works, at all: the first time frame 4 plays, you have two same named functions and you have two listeners.  ie, the frame 0 function still exists so there should be an error about duplicate functions but animate doesn't detect the error so that's an animate/javascript failing.

     

    the other problem (duplicate listeners) is causing the problem with what you probably see which is the responsiveness after clicking gradually slowing with each click because you're executing the frame 0 function 10 times and the frame 4 function 10 times after the tenth time frame 4 plays.

     

    anyway, to fix the problem use:

     

    this.stop();

    this.frame0_executed;

     

    if(!this.frame0_executed){

    this.frame0_executed=true;

    this.btn_scena.addEventListener("click", fl_ClickToGoToAndStopAtFrame_0.bind(this));

    }

    function fl_ClickToGoToAndStopAtFrame_0()
    {
    this.gotoAndStop(4);
    }

     

    and on frame 4:

     

    this.frame4_executed;

    if(!this.frame4_executed){

    this.frame4_executed=true;

    }

    this.btn_scena.addEventListener("click", fl_ClickToGoToAndStopAtFrame_4.bind(this));

    function fl_ClickToGoToAndStopAtFrame_4()
    {
    this.gotoAndStop(0);
    }

    Inspiring
    June 29, 2020

    Thank you very much but with that code it works only once. 

    It goes from frame 0 to frame 4 and back to 0 but stops working after that. 

    It seems that the event listener stops working properly for some reason. That would explain why it did kind of work before.

    kglad
    Community Expert
    Community Expert
    June 29, 2020

    then you must have "different" btn_scena's on frame 0 and frame 4.  to remedy, move btn_scena to its own layer in frame 0 and extend (if needed) its layer to frame 4 with no other keyframes.  remove any other btn_scena's.