Skip to main content
Participant
July 26, 2022
Question

CANVAS HTML5 > Error evento gotoAndPlay in addedChild Movie CLip.

  • July 26, 2022
  • 2 replies
  • 126 views

Hi there,

I have a file with this mc structure with the instance names and vinculation names :
mc_pages > mc_pageloader0 >

 

I do un addChild in "mc_preloader0" del "mc_page0".

All works, the "mc_page0" load and play, and stop at the end.

In this movie clip "mc_page0" I have a Replay Button, that I want "this.gotoAndPlay(0);"

 

And it's not working... Why?

 

 

    This topic has been closed for replies.

    2 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    July 26, 2022

    Hi.

     

    If you log the this keyword inside of the recommence function to the browser's console you will notice that it doesn't refer to the current timeline. There are several ways of solving this problem: you can save a reference to the current timeline in a variable outside the function, you can use bind or use the on method from CreateJS.

     

    // EXTERNAL REFERENCE APPROACH
    var _this = this;
    
    _this.bt.addEventListener("click", recommence);
    
    function recommence()
    {
        _this.gotoAndPlay(0);
    };

     

    // BIND APPROACH
    this.bt.addEventListener("click", recommence.bind(this));
    
    function recommence()
    {
        this.gotoAndPlay(0);
    };

     

    // ON METHOD APPROACH
    this.bt.on("click", recommence, this);
    
    function recommence()
    {
        this.gotoAndPlay(0);
    };

     

    I hope it helps.

     

    Regards,

    JC

     

    Participant
    July 26, 2022

    Many thx JC !

     

    OK, the EXTERNAL REFERENCE APPROACH is working well.

    For you what is the best way?


    OK I think I have a conceptual problem with TimeLines.

    When we write :

    var _this = this;

    This is not a global variable (variable I declared on the main taimeLine... Like a _root), but a variable of this MovieCLip?

    Do you have a tutorial on hand that explains this concept in a simple way?

    Participant
    July 26, 2022

    That is the script for my button, in "mc_page0"

    this.bt.addEventListener("click", recommence);
    function recommence(){
    this.gotoAndPlay(0);
    };

    The button works if I do an console.log();

     

    I got this error :

    Uncaught TypeError: this.gotoAndPlay is not a function