问题
how can i remove EventListener
from MovieClip
in actionscript? Below is my sample code.
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
var color: ColorTransform = new ColorTransform();
color.color = 0x00CC66;
colorChange_mc.buttonMode = true;
colorChange_mc.addEventListener(MouseEvent.MOUSE_OVER, changeColor(color));
colorChange_mc.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
function changeColor(color: ColorTransform): Function {
return function (e: MouseEvent): void {
colorChange_mc.transform.colorTransform = color;
}
}
function changeColorToNormal(e: MouseEvent): void {
var color: ColorTransform = new ColorTransform();
color.color = 0x000033;
colorChange_mc.transform.colorTransform = color;
}
changer_mc.buttonMode = true;
changer_mc.addEventListener(MouseEvent.MOUSE_DOWN, removeEvent);
function removeEvent(e: MouseEvent): void {
colorChange_mc.removeEventListener(MouseEvent.MOUSE_OVER, changeColor(color));
}
I created two MovieClip
s on the stage
, colorChange_mc has two EventListener
s one for mouseDown
and one for mouseOut
. When change_mc button is pressed I want to remove one of the EventListener
. I was able to remove EventListener
without passing parameter to changeColor function. But in my real class this parameter plays crucial role.
回答1:
Use a class-var instead - one you have already in your code. To keep straight I created a second ColorTransform
as a class-var - normalColor
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
var color: ColorTransform = new ColorTransform();
color.color = 0x00CC66;
var normalColor: ColorTransform = new ColorTransform();
normalColor.color = 0x000033;
colorChange_mc.buttonMode = true;
colorChange_mc.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
colorChange_mc.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
function changeColor(e:MouseEvent): void{
colorChange_mc.transform.colorTransform = color;
}
function changeColorToNormal(e:MouseEvent): void {
colorChange_mc.transform.colorTransform = normalColor;
}
changer_mc.buttonMode = true;
changer_mc.addEventListener(MouseEvent.MOUSE_DOWN, removeEvent);
function removeEvent(e:MouseEvent): void {
colorChange_mc.removeEventListener(MouseEvent.MOUSE_OVER, changeColor);
}
Greetings.
回答2:
The problem is that in order to remove the listener, you have to identify the function that you originally passed to it. But you have no reference to the function, because it is anonymous and lives in the global scope.
Think object oriented. Put all the functionality of changing the color into a class. All you actually want to do is
- specify a color value
- be able to disable the whole thing again.
Add two methods to the class of colorChange_mc
that let you do this:
public function set color(value:uint):void
and
public function set disable(value:Boolean):void
Alternatively, you can override
the enabled
setter function to tie your logic to it, if this is something you desire.
This way you can refactor most of the code that currently bloats your main time line into the class.
回答3:
According to my first answer and your comment with new facts here is a solution for more buttons/colors using a Dictionary
...
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.utils.Dictionary;
import flash.display.MovieClip;
var normalColor: ColorTransform = new ColorTransform();
normalColor.color = 0x000033;
//button1-30 are the instancenames of your buttons
var initObj:Array=[{btn:button1, color:0xFFFFFF},
{btn:button2, color:0xFF0000},
{btn:button3, color:0x00FF00},
...
{btn:button30, color:0x000000},
];
var dict:Dictionary = new Dictionary();
initButtons();
function initButtons():void
{
for each (var item:Object in initObj)
{
var btn = item.btn;
btn.addEventListener(MouseEvent.MOUSE_OVER, changeColor);
btn.addEventListener(MouseEvent.MOUSE_OUT, changeColorToNormal);
dict[btn] = {color:item.color};
}
}
function changeColor(e:MouseEvent):void
{
var btn:MovieClip = e.target as MovieClip;
var color:uint = dict[btn].color;
var cT:ColorTransform=new ColorTransform();
cT.color = color;
colorChange_mc.transform.colorTransform = cT;
}
function changeColorToNormal(e:MouseEvent):void
{
colorChange_mc.transform.colorTransform = normalColor;
}
Greetings.
回答4:
Thank you for your replies. As I said before the code that i posted above was just an example. Sorry if I asked unclear question. I solved my problem by disabling some specific buttons when colorChange button is pressed.
_userCardsButtons_mc.mouseEnabled = false;
_userCardsButtons_mc.mouseChildren = false;
_userCardsButtons_mc.buttonMode = false;
this code disabled event listeners that I added before.
来源:https://stackoverflow.com/questions/31043957/actionscript3-removeeventlistener-with-function-parameter