AS3如何从计时器在舞台上添加一个类?

时间:2013-04-17 00:01:35

标签: actionscript-3 timer addchild

你们很多帮助:)我的下一个问题是:)。

我在课程MyTimer.as和Thief1_mc.as影片剪辑中有计时器。 如何在MyTimer的舞台上添加孩子(Thief1_mc)?一切看起来都很简单,唯一的问题就是“舞台”属性。 MyTimer类不能将“stage”作为参数发送,因为它不在舞台上。我尝试在Main类的舞台上添加MyTimer,如addChild(MyTimer),跟踪说MyTimer在舞台上,但我仍然无法将舞台参数传递给Thief1_mc。我需要发送这个参数,因为类Thief1_mc必须使用属性“stage”在舞台上添加它。

代码:

public class Thief1_mc extends MovieClip
{

    //this variable type Stage will contain stage
    private var stageHolder:Stage;

    public function Thief1_mc()
    {
        //constructor
    }

    //function that creates this object with passed "stage" argument from the caller
    public function createItself(st):void
    {
        //variable that contain the stage so I can use this argument anywhere in the class 
        stageHolder = st;
        //i have to refer to the stage by passed "st" parameter to create this object
        stageHolder.addChild(this);
        //initial position
        this.x = 380;
        this.y = 230;
    }

}

}

MyTimer类和“_thief1.createItself(stage)”调用者与舞台文件

public class MyTimer extends Sprite
{
    private static var nCount:Number = 120;
    private static var currentCount:Number;
    private static var _timer:Timer = new Timer(1000,nCount);

    private static var _timerDispather:Timer;
    private static var _thief1:Thief1_mc = new Thief1_mc ;

    public function MyTimer()
    {
        // constructor code
    }

    //another timer
    private static function increaseInterval(interval:int):void
    {
        _timerDispather = new Timer(interval);
        _timerDispather.addEventListener(TimerEvent.TIMER, onUpdateTimeAnotherTimer);
        _timerDispather.start();
    }
    //another timer;
    private static function onUpdateTimeAnotherTimer(e:Event):void
    {

        _thief1.createItself(stage);//the most important part
    }

    public static function activateTimer():void
    {
        currentCount = nCount;
        _timer.addEventListener(TimerEvent.TIMER, onUpdateTime);
        _timer.start();
    }

    public static function deactivateTimer():void
    {
        _timer.removeEventListener(TimerEvent.TIMER, onUpdateTime);
        _timer.stop();
        _timer.reset();
        currentCount = nCount;

        //another timer
        _timerDispather.removeEventListener(TimerEvent.TIMER, onUpdateTimeAnotherTimer);
        _timerDispather.stop();
        _timerDispather.reset();
    }

    private static function onUpdateTime(e:Event):void
    {
        currentCount--;


        if (currentCount == 0)
        {
            _timer.removeEventListener(TimerEvent.TIMER, onUpdateTime);
            _timer.stop();
            _timer.reset();

        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

您的代码在几个地方倒退。它的流动性不是很好,而且你现在遇到的问题在项目的某个阶段将会是十倍。

首先,您的MyTimer类不应该扩展Sprite。它不会被渲染,也不会以图形方式表示任何内容。

其次,你的计时器课程正在进行中。我会修改它来管理你的计时器和计时器事件。在您的计时器类中创建一个列表,该列表将包含一些其他元素,这些元素可以有一个方法触发器来执行其他操作,例如创建和添加Thief1_mc

此简化版可能如下所示:

public class Updater
{

    private var _timer:Timer;
    private var _toUpdate:Vector.<IUpdatable> = new Vector.<IUpdatable>();


    public function Updater()
    {
        _timer = new Timer(60);
        _timer.start();
        _timer.addEventListener(TimerEvent.TIMER, _notifyUpdatables);
    }


    private function _notifyUpdatables(e:TimerEvent):void
    {
        for each(var i:IUpdatable in _toUpdate)
        {
            i.update(this);
        }
    }


    public function addUpdatable(updatable:IUpdatable):void
    {
        _toUpdate.push(updatable);
    }


    public function removeUpdatable(updatable:IUpdatable):void
    {
        var index:int = _toUpdate.indexOf(updatable);
        if(index >= 0) _toUpdate.splice(index, 1);
    }

}

从这里我们需要创建一个接口,我们将在每次update()计时器滴答时希望能够调用Updater的类上实现这个接口:

public interface IUpdatable
{
    function update(updater:Updater):void;
}

现在我要做的就是有一个类扩展Sprite并管理应用程序/游戏的图形。它会实现我所描述的IUpdatable界面,也可以处理添加Thief1_mc

public class View extends Sprite implements IUpdatable
{

    public function update(updater:Updater):void
    {
        // Create a Thief.
        var thief:Thief = new Thief();

        updater.addUpdatable(thief);
        addChild(thief);
    }

}

您的Thief可以利用我们拥有的IUpdatable界面,并在创建时添加到更新队列中,正如我上面所做的那样。只是为了有一个完整的例子,这里是小偷班:

public class Thief extends Sprite implements IUpdatable
{

    public function update(updater:Updater):void
    {
        // Make this Thief so some stuff.
        //
    }

}

以下是如何在文档类中将它们组合在一起的:

public class App extends Sprite
{

    private var _updater:Updater;
    private var _view:View;


    public function App()
    {
        _updater = new Updater();
        _view = new View();

        _updater.addUpdatable(_view);

        stage.addChild(_view);
    }

}

起初这可能有点压倒性,看起来很多工作,但你现在有了一个很好的基础,可以轻松添加更多元素。

我们不是让你的一个班级试图像你最初那样管理计时器和添加盗贼,而是将责任分开并稍微加强了流程。 Updater完全与存储IUpdatable个实例相关,并且每次update()内的Timer方法都会调用它们。 View类管理图形,每次通过Thief更新时都会添加Updater。最初将View添加到舞台中,因此您需要做的就是将盗贼添加到自身中以显示它们。

如果你采取这种方式并重新调整计时器在Updater内的工作方式,我认为你会在你想要的地方,但具有更好的理解和结构。

相关问题