Skip to main content
iezqandarzulqarnaen
Known Participant
August 10, 2017
Answered

Hover script not working in animation AS3

  • August 10, 2017
  • 1 reply
  • 308 views

Hye guys,

This is a continuous thread ofButton Hover​.

After applying code form just.emma​ i tried to make some animation for the buttons.

the problem come after the button animation stop, the button hover is not working.

here is the .fla file : Dropbox - Adobe Animate CC

I have made two(2) buttons, 1 is static that hover is working & another 1 is with animation that hover not working.

Kindly help me inspect what is the problem.

Thank you.

This topic has been closed for replies.
Correct answer RandomlyFish

The code you have written for all the buttons run as soon as you start playing, but the button that the code is trying to access, does not exist at that point. So one way to fix this would be to keep each of the buttons off screen at the start. Another way to solve it would be to execute the code once all buttons are on the screen, but I would recommend the first solution.

Also, here's one way you could write the code, to make it a lot less repetetive:

function CreateButton (_button : DisplayObject, _animation : MovieClip) {

     var over : Function = function () {

          _animation.gotoAndPlay("animate");

     }

     var out : Function = function () {

          _animation.gotoAndStop(1);

     }

     _button.addEventListener(MouseEvent.MOUSE_OVER, over);

     _button.addEventListener(MouseEvent.MOUSE_OUT, out);

}

CreateButton(subMenuStatic_mc.glass_btn, this.glassdept_mc);

CreateButton(subMenuAnimate_btn.glass_btn2, this.glassdept_mc);

That way you only have to write one line of code for each new button you want to add. And if you want to change the behaviours of the buttons, you only need to change it inside the CreateButton function, instead of on multiple different lines of code.

1 reply

RandomlyFishCorrect answer
Inspiring
August 10, 2017

The code you have written for all the buttons run as soon as you start playing, but the button that the code is trying to access, does not exist at that point. So one way to fix this would be to keep each of the buttons off screen at the start. Another way to solve it would be to execute the code once all buttons are on the screen, but I would recommend the first solution.

Also, here's one way you could write the code, to make it a lot less repetetive:

function CreateButton (_button : DisplayObject, _animation : MovieClip) {

     var over : Function = function () {

          _animation.gotoAndPlay("animate");

     }

     var out : Function = function () {

          _animation.gotoAndStop(1);

     }

     _button.addEventListener(MouseEvent.MOUSE_OVER, over);

     _button.addEventListener(MouseEvent.MOUSE_OUT, out);

}

CreateButton(subMenuStatic_mc.glass_btn, this.glassdept_mc);

CreateButton(subMenuAnimate_btn.glass_btn2, this.glassdept_mc);

That way you only have to write one line of code for each new button you want to add. And if you want to change the behaviours of the buttons, you only need to change it inside the CreateButton function, instead of on multiple different lines of code.