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

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

Enthusiast ,
Sep 08, 2018 Sep 08, 2018

Copy link to clipboard

Copied

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

Views

819

Translate

Translate

Report

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

}

Votes

Translate

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

Copy link to clipboard

Copied

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

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Everyone's methods have been tried,

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Because special needs must jump to the code frame.

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

use:

if(!this.alreadyExecuted){

this.alreadyExecuted=true;

var pdF=pd.bind(this);

this.addEventListener('click',pdF);

}

function pd(){

this.removeEventListener('click',pdF);

this.play();

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

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