是否可以从一个类访问另一个类的事件处理程序,如果可以,如何访问?我想从对象中删除事件处理程序。我有两个对象说 objectA 和 objectB。在 objectA 内部,如何删除 objectB 的事件处理程序。这是我尝试过的,但它不起作用。
MovieClip(root).objectB.removeEventListener(MouseEvent.CLICK, ready);
细节:
我有两个名为 Guest 和 Guest2 的课程。我想知道是否可以从 Guest2 类中删除 Guest 类中的事件监听器。下面是完整的代码。注意:两个类的代码完全相同
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.*;
public class Guest extends MovieClip
{
var walkSpeed:Number = 5;
var oldPosX;
var oldPosY;
var myGlow:GlowFilter = new GlowFilter();
public function Guest()
{
addEventListener(MouseEvent.MOUSE_OVER, addGlow);
}
function addGlow(event:MouseEvent):void
{
filters = [myGlow];
addEventListener(MouseEvent.MOUSE_OUT, removeGlow);
addEventListener(MouseEvent.CLICK, ready);
}
function removeGlow(event:MouseEvent):void
{
filters = [];
}
function ready(event:MouseEvent):void
{
filters = [myGlow];
stage.addEventListener(MouseEvent.MOUSE_DOWN, walk);
removeEventListener(MouseEvent.MOUSE_OUT, removeGlow);
**MovieClip(root).Guest02.addEventListener(MouseEvent.CLICK, walkTo);**
}
function walk(event:MouseEvent):void
{
oldPosX = parent.mouseX;
oldPosY = parent.mouseY;
rotation = Math.atan2(oldPosY - y,oldPosX - x) / Math.PI * 180;
filters = [];
stage.removeEventListener(MouseEvent.MOUSE_DOWN, walk);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function loop(event:Event):void
{
var dx:Number = oldPosX - x;
var dy:Number = oldPosY - y;
var distance:Number = Math.sqrt((dx*dx)+(dy*dy));
if (distance<walkSpeed)
{
// if you are near the target, snap to it
x = oldPosX;
y = oldPosY;
removeEventListener(Event.ENTER_FRAME, loop);
}
else
{
x = x+Math.cos(rotation/180*Math.PI)*walkSpeed;
y = y+Math.sin(rotation/180*Math.PI)*walkSpeed;
}
}
**function walkTo(event:MouseEvent):void
{
_Guest02.removeEventListener(MouseEvent.CLICK, ready);
}**
}
}