AS3将一个EnterFrame事件侦听器添加到Sprite子类

时间:2014-01-13 12:14:14

标签: actionscript-3 flash sprite movieclip flashdevelop

我在FlashDevelop中使用AS3

CustomClassA extends Sprite

CustomClassB extends CustomClassA但还需要每帧执行一些代码

除了CustomClassA延长MovieClip而不是Sprite之外,还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

我将尝试创建一个简单的示例,使用虚拟概念,仅解释如何执行此操作的方法。你可以尝试类似的东西:

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();