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

ANCC HTML5 .How to make a click event execute only 1 times

Enthusiast ,
Sep 08, 2018 Sep 08, 2018

I use the code below to manipulate.

Use gotoAndStop to jump to this frame,

will be again "var kg = True"

Causes the button to click again.

Is there any other way to make the button perform only 1 times?

var kg = true

this.on("click", pd);
function pd()
{

if(kg)
{
  this.play();
  kg = false
}

}
this.stop();

1.2K
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 09, 2018 Sep 09, 2018

use:

if(!this.alreadyExecuted){

this.alreadyExecuted=true;

var pdF=pd.bind(this);

this.addEventListener('click',pdF);

}

function pd(){

this.removeEventListener('click',pdF);

this.play();

}

Translate
Community Expert ,
Sep 08, 2018 Sep 08, 2018

among the ways to do this:

var alreadyExecuted;

if(!alreadyExecuted){

alreadyExecuted=true;

var pdF=pd.bind(this);

this.addEventListener('click',pdF);

}

function pd(){

this.removeEventListener('click',pdF);

this.play();

}

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
Enthusiast ,
Sep 08, 2018 Sep 08, 2018

The code is still invalid,

When the movie loops and then jumps to the start, the button can be executed again, unable to implement 1 times.

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

Just use on with the "once" flag set.

https://createjs.com/docs/easeljs/classes/EventDispatcher.html#method_on

this.on("click", this.play, this, true);

And DON'T let your movie loop to the start.

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
Enthusiast ,
Sep 09, 2018 Sep 09, 2018

Everyone's methods have been tried,

When you jump to this frame, it will count again.

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

Sometimes  for this kind of problem I use a variable like

var active = 1;

if(active ==1){

// do something

active = 0;

}

This way since your variable has been set to 0 the action will not repeat since it is not reset to 1 if that makes sense.

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
Enthusiast ,
Sep 08, 2018 Sep 08, 2018

If you jump to this frame again.

Active will be equal to 1 again.

It caused the failure.

If you write to the global function directly then there is no problem.( active = 1;Do not use Var)

I want to know the way to not use global functions.

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

Then do not go back to the initial frame. I put most of my code on frame 1 and then control everything from there. I have been totally successful controlling plays exactly as I want especially when I want something to happen only once.

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
Enthusiast ,
Sep 09, 2018 Sep 09, 2018

Because special needs must jump to the code frame.

Fortunately, it is. Kglad's answer was implemented only once

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

use:

if(!this.alreadyExecuted){

this.alreadyExecuted=true;

var pdF=pd.bind(this);

this.addEventListener('click',pdF);

}

function pd(){

this.removeEventListener('click',pdF);

this.play();

}

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
Enthusiast ,
Sep 09, 2018 Sep 09, 2018

Thank you, really only executed once, thank you very much.

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