Skip to main content
Known Participant
February 25, 2017
Answered

How does gotoAndStop work for html5 canvas?

  • February 25, 2017
  • 4 replies
  • 9973 views

I am finding that gotoAndStop will not work properly for an mc if the script is at the root level.

For example, if I have an mc called "bananas" which has ten frames, I can use "this.gotoAndStop(5);" on a script within the timeline of the mc itself and it seems to work.

However if I use "root.bananas.gotoAndStop(5);" from a script at the root of my Animate file, it does not work.

Is that how it is, or maybe I am doing something wrong?

Also, if you have multiple instances of an mc, does that cause problems with gotoAndStop?

This topic has been closed for replies.
Correct answer Colin Holgate

What is the value of root? The main timeline is also 'this' at its level, so this syntax would be more correct:

this.bananas.gotoAndStop(5);

but one potential problem is that at the time that line runs the movieclip hasn't yet arrived on the stage. Putting the script inside the movieclip is an easy solution, but you might want to have it go to a different frame sometimes. That can be done using window variables. Like in the main timeline on that first frame you could have:

window.whichbanana = Math.floor(Math.random()*10);

and inside the bananas movieclip you say:

this.gotoAndStop(window.whichbanana);

4 replies

Known Participant
March 7, 2017

Finally got around to re-testing, and got my thing working :-)

Because my canvas has a preloader screen, I was assuming everything was ready as soon as it appeared on the screen, but in fact, although all the assets had loaded, the elements within the canvas were not all constructed.

Originally I had set all the "gotoAndStops" immediately after the canvas had loaded - and nothing was happening.

I did not really need the gotoAndStops to run that early, so I moved them in the code to run later (after the user had clicked a few buttons, a splash screen had come and gone, etc.) - that fixed it.

Here's my final working code:

/* immediately on load */

var root = this;

/* inside a function later on, after the mcs all had time to build */

for (i = 1; i < 13; i++) {

    root.text_blocks["text_block_" + i].gotoAndStop(i-1);

}

So it looks as if my code was ok in the first place, except that I had it too early in the file.

Colin's first reply set me on the right track, so I will mark it as the correct answer, but the thread as a whole was very helpful.

Known Participant
February 26, 2017

Just to recap/clarify

What I was after is as follows:

I have an mc which has several frames, and I want to be able to go to a specific frame within that mc, but calling it from the main script that I have at the top level of my document. (As far as possible I prefer to have all my scripting in one place at the top level, rather than dotting it about on multiple frames and mcs.)

I don't really have an mc called banana, it was just a simplified example, to illustrate the situation.

so - I have tried using "this.banana.gotoAndStop" from my main script, but it doesn't work. It used to work in Flash/swf as I remember, but it does not seem to work with Animate/canvas.

Incidentally I like to start my script with

var root = this;

so that I can say "root.whatever...", rather than "this.whatever...". I find it easier to scan the code that way. That is why I was saying "root.banana.gotoAndStop". maybe that's not best practice - works for me though...

I am optimistic that Colin's posts will solve my problem. Unfortunately I am now tied up in some other non-Animate work and can't test it out for a few days.

Anyway, thanks very much to all of you for the help and comments. As soon as I get a chance to test it I will get back with my results.

Colin Holgate
Inspiring
February 26, 2017

Thanks for the extra info, though I'm still curious about what is your use case, for needing to tell a movieclip to go to a particular frame?

Watch out in your tests of code in this thread. Some places the mc is called bananas, and other places it's being accessed with this.banana.gotoAndStop, etc. That missing s might not be easy to notice.

just.emma
Inspiring
February 26, 2017

BTW, using a graphic symbol and the frame picker (instead of a movie clip) could also help you get the results you want: Create symbol instances in Animate CC

Colin Holgate
Inspiring
February 26, 2017

That wouldn't help if you wanted a random banana.

just.emma
Inspiring
February 26, 2017

Where did she say anything about wanting it to be random?  She asked how to go to a specific frame (5).

Colin Holgate
Colin HolgateCorrect answer
Inspiring
February 25, 2017

What is the value of root? The main timeline is also 'this' at its level, so this syntax would be more correct:

this.bananas.gotoAndStop(5);

but one potential problem is that at the time that line runs the movieclip hasn't yet arrived on the stage. Putting the script inside the movieclip is an easy solution, but you might want to have it go to a different frame sometimes. That can be done using window variables. Like in the main timeline on that first frame you could have:

window.whichbanana = Math.floor(Math.random()*10);

and inside the bananas movieclip you say:

this.gotoAndStop(window.whichbanana);

Known Participant
February 25, 2017

Thanks, will try this. Yes, when I say root I mean the top level "this".

Inspiring
February 26, 2017

it seems you need to put the code in the delay timer

try

var mtl = this;

setInterval(function(){

     mtl.banana.gotoAndStop(5)

}, 50);