Skip to main content
Participating Frequently
July 31, 2017
Answered

gotoAndStop() not working as intended

  • July 31, 2017
  • 1 reply
  • 1968 views

Hello,

I have created a screen which has 2 movieclips which will be used as buttons; they are named button1_mc and button2_mc.

When each button is clicked it should go to frame 2 on the timeline (frame 1 as index starts at 0) of the movieclip to indicate it has been selected and reveal some accompanying text in a movieclip named either reveal1_mc or reveal2_mc. When one button is clicked, it should also set the other button back to frame 1 on its timeline to indicate it is no longer selected, and then hide the corresponding reveal_mc: I have created the "hideAll" function for this purpose.

My code:

var _this=this;

this.hideAll=function(){

     this.button1_mc.gotoAndStop(0);

     this.button2_mc.gotoAndStop(0);

     this.reveal1_mc.visible=false;

     this.reveal2_mc.visible=false;

              

}

this.hideAll();

this.button1_mc.addEventListener("click", reveal1);

function reveal1(){

              

     _this.hideAll();

     _this.reveal1_mc.visible=true;

     _this.button1_mc.gotoAndStop(1);

}

this.button2_mc.addEventListener("click", reveal2);

function reveal2(){

               

     _this.hideAll();

     _this.reveal2_mc.visible=true;

     _this.button2_mc.gotoAndStop(1);

}

The problem is that when i run the screen, the buttons automatically start on frame 2 as if they have been clicked (each of these movieclips has this.stop() on each frame too). Once one of the buttons is clicked however it then works as intended.

Can anyone suggest why it would be doing this?

Many thanks.

This topic has been closed for replies.
Correct answer Fumio Nonaka

Hi Colin,

Thank you for your response. I can visually tell which frame the Movieclip is on as it will turn a different colour. There doesn't seem to by any indication within the developer console of any errors occurring.

I have a demonstration of it here: test 

The grey state is when the movieclip is on its first frame.  (0 with CreateJS)

The green state is when the movieclip is on its second frame. (1 with CreateJS)

The hideAll() function I have included should initially set the states to grey, but they end up starting green, it all works as intended when one is clicked from thereon in.


How about changing your code like this:

this.hideAll=function(){

     if (this.button1_mc.currentFrame === 0) {

          this.button1_mc.stop();

     } else {

          this.button1_mc.gotoAndStop(0);

     }

     if (this.button2_mc.currentFrame === 0) {

          this.button2_mc.stop();

     } else {

          this.button2_mc.gotoAndStop(0);

     }

}

1 reply

Legend
July 31, 2017

You're probably trying to control their timelines before they're been fully instantiated. Just put a this.stop() statement in the first frame of each button clip.

Also you aren't using _this in your hideAll function.

Participating Frequently
August 2, 2017

Hi ClayUUID, thanks for the answer. Unfortunately this doesn't seem to fix the problem; it's almost as if it's ignoring the this.stop() on frame 1 of the button clips.

Another weird thing is that when I write out to the console the buttons currentFrame property it returns 0. (which is frame 1 of the movieclip).

Participating Frequently
August 2, 2017

Open the dev console when you're testing the page to see if there are any error messages.


There are no errors when using the F12 developer tools.

I played around with the hideAll function and when I set this.button1_mc.gotoAndStop(1), the timeline shows it is on frame 0, with the currentFrame property showing 1.

Does the indexing start at 1 in some cases?