문제

I'm using AS3 with FlashDevelop

CustomClassA extends Sprite

CustomClassB extends CustomClassA but also needs to execute some code every frame

Is there any other way to do this, apart from making CustomClassA extend MovieClip instead of Sprite?

도움이 되었습니까?

해결책

I'll try to create a simple example, using a dummy concept, only to explain a way how to do it. You could try something like:

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...
    }
}

and then, create your CustomClassB:

public class CustomClassB extends CustomClassA
{
    public function CustomClassB() 
    {
    }

    override public function executeSomeCodeEveryFrameMethod():void
    {
        //custom executeSomeCodeEveryFrameMethod
    }
}

you can test using:

var customClassB:CustomClassB = new CustomClassB();
customClassB.startEnterFrame();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top