Skip to main content
Henri v
Participant
August 28, 2024
Answered

Problem: Button - GotoAndStop - addEventListener - didn't work

  • August 28, 2024
  • 1 reply
  • 412 views

Hello everyone,

I cannot solve the following problem and cannot find the error.

 

I used the exercise file from the ‘Adobe’ tutorial as a template. https://creativecloud.adobe.com/cc/learn/animate/web/animated-infographic?locale=en

 

This file can be tested from ‘Animate’ and the two buttons work. If I rebuild this file and use the same code, the control of the ‘Movieclip’ with the gotoAndStop command is not activated.
The code from the example file:

 

this.info_energy.addEventListener("click", fl_MouseClickHandler_2.bind(this));

function fl_MouseClickHandler_2()
{
	this.popup_energy.play();
}

this.info_network.addEventListener("click", fl_MouseClickHandler_1.bind(this));

function fl_MouseClickHandler_1()
{

this.popup_network.play();

}

 

The code from me:

 

var localThis = this;

function init() {
this.taste01.addEventListener("click", Mausklick_Taste01(this));
}

function Mausklick_Taste01()
{
	this.display1.gotoAndStop(1);
}

init();

 

I have a movieclip with the instance name ‘display1’. No matter what I have tried so far, the ‘gotoAndStop’ command is not triggered. In the Andobe example file it works as a preview, locally in the browser and on a server.

I have already searched the forum and tried everything possible.
- I have packed the whole code into an init function.
- used this with a variable
- exchanged the image number with a jump label

I can't find the basic difference to the Adobe example file. This file even works locally as an html file, with mine the button function does not even work locally but only from a server.

The two .fla files are here: https://hidrive.ionos.com/share/23zdm2gg3j 

I would appreciate an answer.

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

 

You have problems on handling context (the this keyword) and on passing the event handler function. Your code should be something like this:

var localThis = this;

function init()
{
	localThis.taste01.addEventListener("click", Mausklick_Taste01);
}

function Mausklick_Taste01()
{
	localThis.display1.gotoAndStop(1);
}

init();


Regards,
JC

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
August 28, 2024

Hi.

 

You have problems on handling context (the this keyword) and on passing the event handler function. Your code should be something like this:

var localThis = this;

function init()
{
	localThis.taste01.addEventListener("click", Mausklick_Taste01);
}

function Mausklick_Taste01()
{
	localThis.display1.gotoAndStop(1);
}

init();


Regards,
JC

Henri v
Henri vAuthor
Participant
August 28, 2024

Thank you very much.

JoãoCésar17023019
Community Expert
Community Expert
August 28, 2024

You're welcome.