I recently came across an issue with disabling buttons in ActionScript 3 without removing the MouseEvent listeners. In AS2 its simple if you just set the enabled property on the button/movieclip to false. In AS3 you have to do a little more than that. You need to set the mouseEnabled property on the button/movieclip to false but I also found that the MOUSE_OUT event will get triggered when that occurs so you need to do some custom code to disable that functionality.
In AS2
buttonClip.onRelease = function() { this.enabled = false; } buttonClip.onRollOver = function() { // do something } buttonClip.onRollOut = function() { // do something }
In AS3
buttonClip.addEventListener( MouseEvent.MOUSE_DOWN, buttonClick ); buttonClip.addEventListener( MouseEvent.MOUSE_OVER, buttonOver ); buttonClip.addEventListener( MouseEvent.MOUSE_OUT, buttonOut ); function buttonClick(_e:Object) { _e.target.mouseEnabled = false; } function buttonOver(_e:Object) { // do something } function buttonOut(_e:Object) { if(_e.target.mouseEnabled == false) return; // do something }
You’ll notice in AS3 how you need to check if the mouseEnabled property is set to false before you handle the mouse out functionality. In my project I was changing the button color on mouse over and changing it back on mouse out. When they clicked on the button, I set the mouseEnabled to false but the buttonOut function was getting triggered automatically, so this workaround fixed the issue. It’s not too much extra code but was annoying to figure out what was going on.
Recent Comments