0
我使用AS3與FlashDevelop中AS3添加enterFrame事件偵聽器添加到雪碧子
CustomClassA extends Sprite
CustomClassB extends CustomClassA
還需要執行一些代碼的每一幀
是否有任何其他方式做到這一點,除了讓CustomClassA
擴展MovieClip
而不是Sprite
?
我使用AS3與FlashDevelop中AS3添加enterFrame事件偵聽器添加到雪碧子
CustomClassA extends Sprite
CustomClassB extends CustomClassA
還需要執行一些代碼的每一幀
是否有任何其他方式做到這一點,除了讓CustomClassA
擴展MovieClip
而不是Sprite
?
我會嘗試創建一個簡單的例子,使用虛擬概念,只是爲了解釋如何做到這一點。你可以嘗試這樣的:
public class CustomClassA extends Sprite
{
public function CustomClassA()
{
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
}
private function addedToStageHandler(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
//start your code here...
}
public function startEnterFrame():void
{
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
}
public function stopEnterFrame():void
{
this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
executeSomeCodeEveryFrameMethod();
}
public function executeSomeCodeEveryFrameMethod():void
{
//your enter frame code...
}
public function dispose():void
{
stopEnterFrame();
//garbage collection...
}
}
,然後創建CustomClassB:
public class CustomClassB extends CustomClassA
{
public function CustomClassB()
{
}
override public function executeSomeCodeEveryFrameMethod():void
{
//custom executeSomeCodeEveryFrameMethod
}
}
您可以測試使用:
var customClassB:CustomClassB = new CustomClassB();
customClassB.startEnterFrame();
添加監聽器的階段,而不是你的對象.. – Cherniv