-2

I have to control the timeline of an animation for html5 canvas. I made all buttons and edited all codes. strangely all buttons execute the same function.

this.stop()
//---------------------------------------------------------//
this.letterA.addEventListener("click", fl_MouseClickHandler.bind(this));

function fl_MouseClickHandler()
{
    this.gotoAndPlay(32);
}

//---------------------------------------------------------//

//---------------------------------------------------------//
this.letterB.addEventListener("click", fl_MouseClickHandler.bind(this));
}
function fl_MouseClickHandler()
{
    this.gotoAndPlay(212);
}
  • also, this code is "generated" by adobe CC. as I"m not a coder, just a meager animator, I just copied and pasted the same one editing the symbols' names. – Altair Araujo Jan 05 '17 at 21:46

2 Answers2

1

Please read about function hoisting; the function statements are hoisted to the top-of-the-scope, so that your code is equivalent to

function fl_MouseClickHandler() {
    this.gotoAndPlay(32);
}

function fl_MouseClickHandler() {
    this.gotoAndPlay(212);
}

...
this.stop()    
this.letterA.addEventListener("click", fl_MouseClickHandler.bind(this));
this.letterB.addEventListener("click", fl_MouseClickHandler.bind(this));

One solution would be to use function expressions:

var that = this;
this.letterA.addEventListener("click", function () { that.gotoAndPlay(32); });

or if you insist to use bind

this.letterA.addEventListener("click", (function () { this.gotoAndPlay(32); }).bind(this));
Community
  • 1
  • 1
  • I tried this: this.stop(); var letterA = this; this.letterA.addEventListener("click", function () { letterA.gotoAndPlay(97) }); var letterB = this; this.letterB.addEventListener("click", function () { letterB.gotoAndPlay(108) }); and it seems to have worked. I"ll try it on the main file and see if it worked. Thanks a lot! – Altair Araujo Jan 05 '17 at 21:38
0

From what I can tell, you are binding the same function to two different button click events. You have two different definitions of this same function. One is most likely being written over the other and therefore both buttons will call fl_MouseClickHandler which will call gotoAndPlay with the same timestamp.

Quick fix could be just to change the name of fl_MouseClickHandler:

function fl_MouseClickHandlerA() {
    this.gotoAndPlay(32);
}

function fl_MouseClickHandlerB() {
    this.gotoAndPlay(212);
}

this.letterA.addEventListener("click", fl_MouseClickHandlerA.bind(this));
this.letterB.addEventListener("click", fl_MouseClickHandlerB.bind(this));

Also, you should watch how you use "this" as you may not be scoped to what you think.

Mark B
  • 26
  • 5