Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

gotoAndStop() not working as intended

Community Beginner ,
Jul 31, 2017 Jul 31, 2017

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.

1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 03, 2017 Aug 03, 2017

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);

     }

}

Translate
LEGEND ,
Jul 31, 2017 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2017 Aug 02, 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2017 Aug 02, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2017 Aug 02, 2017

The timeline of the movieclip is showing it is on frame 1, yet the currentFrame property says its on frame 0.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2017 Aug 02, 2017

That is how it is with CreateJS. Frame numbers count from 0 when you publish and test.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2017 Aug 02, 2017

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2017 Aug 02, 2017

If you gotoAndStop(1) it will go to and stop on the second frame. You can't see the timeline at the time you're testing, but if you mean that you can tell visually that it's the first frame, then there's an error happening.

What does the developer console say in your browser? Are there any errors?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 03, 2017 Aug 03, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 03, 2017 Aug 03, 2017

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);

     }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 03, 2017 Aug 03, 2017
LATEST

Many thanks Fumio, that worked!

Thank you all for your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines