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

Html5 - Button toggle movieclip only works every second reload

Community Beginner ,
Sep 10, 2018 Sep 10, 2018

Hi,

I have a simple interactive where i am showing/hiding movieclips using Toggle.

The strange thing is that they work the first time, but if i go to the next frame, then back, it doesn't work.

if i go to the next frame and back again, it works again (Every second time).

I quickly created another test animate file to see what was going out (This just had 1 Moviclip), and same result.

I called the debug log, and it seems to loop through the if else twice & prints out 4 values...

I just have two buttons (Home_btn & Toggle_btn)

// Stop at this frame

this.stop();

this.Icon1_mc.alpha = 0;

// log Movicelip alpha

console.log("Frame loaded, Movieclip alpha = " + this.Icon1_mc.alpha);

this.Home_btn.addEventListener("click", fl_GoHome.bind(this));

this.Toggle_btn.addEventListener("click", fl_ToggleIt.bind(this));

function fl_ToggleIt()

{

console.log("Movieclip alpha = " + this.Icon1_mc.alpha);

  // Hide the first and show the next here

      if (this.Icon1_mc.alpha  == 1){

  this.Icon1_mc.alpha = 0;

      }

  else {

  this.Icon1_mc.alpha = 1;

  }

}

this.Home_btn.addEventListener("click", fl_GoHome.bind(this));

function fl_GoHome()

{

this.gotoAndStop(0);

}

IF anyone has any clues it would be greatly appreciated.... it' baffling...

if anyone has 5 minutes to test, that would be greatly appreciated: test.fla - Google Drive

292
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 , Sep 10, 2018 Sep 10, 2018

Hi.

It's because multiple listeners are added to the same object everytime you change frames.

One way to prevent this is adding a custom boolean property to the main timeline and check if the frame is being visited for the first time.

Frame 0 (1)

if (!this.hasStarted)

    this.Start_btn.addEventListener("click", fl_GoFrame1.bind(this));

Frame 1 (2)

if (!this.hasStarted)

{

    this.Home_btn.addEventListener("pressup", fl_GoHome.bind(this));

    this.Home_btn.addEventListener("click", fl_GoHome.bind(this));

...
Translate
Community Expert ,
Sep 10, 2018 Sep 10, 2018

Hi.

It's because multiple listeners are added to the same object everytime you change frames.

One way to prevent this is adding a custom boolean property to the main timeline and check if the frame is being visited for the first time.

Frame 0 (1)

if (!this.hasStarted)

    this.Start_btn.addEventListener("click", fl_GoFrame1.bind(this));

Frame 1 (2)

if (!this.hasStarted)

{

    this.Home_btn.addEventListener("pressup", fl_GoHome.bind(this));

    this.Home_btn.addEventListener("click", fl_GoHome.bind(this));

    this.Toggle_btn.addEventListener("pressup", fl_ToggleIt.bind(this));

    this.hasStarted = true;

}

Regards,

JC

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 ,
Sep 10, 2018 Sep 10, 2018

Thanks for taking the time to assist JC, so simple, so clean...

Works perfect.

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 ,
Sep 11, 2018 Sep 11, 2018
LATEST

You're welcome!

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