一個例子可能有幫助嗎?如果你有Flash IDE中,試試這個在您的時間表:
var ball:Shape = new Shape();
ball.graphics.beginFill(0xFF0000);
ball.graphics.drawCircle(0, 0, 30);
ball.graphics.endFill();
addChild(ball);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
addEventListener("myCustomEvent", myCustomEventListener);
function mouseMoveListener(event:MouseEvent):void
{
dispatchEvent(new Event("myCustomEvent"));
}
function myCustomEventListener(event:Event):void
{
ball.x = stage.mouseX;
ball.y = stage.mouseY;
}
這是代碼演示了的addEventListener和dispatchEvent如何對應。 MOUSE_MOVE事件在內部調度,但您可以使用dispatchEvent調度您自己的事件,就像MOUSE_MOVE一樣。
在此代碼會發生什麼事是,MOUSE_MOVE從舞臺上被檢測到,但不是處理,在mouseMoveListener,你發送另一個事件(被稱爲myCustomEvemt),這是在myCustomEventListener處理代替。它的作用就像MOUSE_MOVE事件,只有你派發了事件而不是Flash播放器。
希望這會有所幫助。
@coderex,請在回答時將您的問題標記爲已回答! – alecmce 2010-02-28 04:06:33