冷凝AS3定时器

时间:2014-03-13 01:16:16

标签: actionscript-3 flash timer

好的,所以我有一个电影剪辑网格,总共25个,我试图多次复制cornTimer而不停止计时器。如果玩家激活所有25个牌,他们将需要25个cornTimer。我想知道我是否可以添加像cornTimer + count这样的东西:timer = new timer; 这样的事情。还有其他建议吗?这部分游戏你在等轴测图上创建了一个小农场。

var menu:menuBG = new menuBG();
var farmSlots:Array = ["empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty","empty"];
var farmSelected = "none";

var cornTimer:Timer = new Timer(100,150);//100 = 10 secs//

farmSlot1.addEventListener(MouseEvent.CLICK, farmClick1);
farmSlot2.addEventListener(MouseEvent.CLICK, farmClick2);


cornTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);


function farmClick1(e:MouseEvent):void {
    addChild(menu);
    menu.x = 400;
    menu.y = 90;
    menu.buyCornBtn.addEventListener(MouseEvent.CLICK, buyCorn);
    farmSelected = farmSlot1;
}

function buyCorn(e:MouseEvent):void {
    menu.buyCornBtn.addEventListener(Event.ENTER_FRAME, cornloading);
    cornTimer.start();
    farmSelected.progressB.visible = true;
    removeChild(menu);
}


function cornloading(e:Event):void {
    var total:Number = 150;
    var loaded:Number = cornTimer.currentCount;
    farmSelected.progressB.bar.scaleX = loaded / total;
    farmSelected.loader_txt.text = Math.round((loaded/total)*100)+ "%";
}

function onComplete(e:Event):void {
    farmSelected.gotoAndStop("corn");
    removeEventListener(Event.ENTER_FRAME, cornloading);
}


function farmClick2(e:MouseEvent):void {
    addChild(menu);
    menu.x = 400;
    menu.y = 90;
    menu.buyCornBtn.addEventListener(MouseEvent.CLICK, buyCorn);
    farmSelected = farmSlot2;
}

1 个答案:

答案 0 :(得分:0)

您可以在舞台上添加一个ENTER_FRAME事件处理程序,并将通知函数保存在一个数组中。因此,将在处理程序中调用通知函数,您不需要创建多个计时器。

当磁贴变为活动状态时,在通知时添加通知。并且notify函数将接受一个参数,该参数指示两帧之间的传递时间。您可以计算图块中传递的总时间,并在总时间等于您想要的值时执行某些操作。

这是一个可以添加通知的简单类。

import flash.display.Stage;
import flash.events.Event;
import flash.utils.getTimer;

public class NotifyUtil {


    private static var _instance:NotifyUtil;

    public static function getInstance():NotifyUtil
    {
        if (_instance == null)
        {
            _instance = new NotifyUtil();
        }

        return _instance;
    }

    public function init(stage:Stage):void
    {
        stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private var lastTime:int = 0;

    private function onEnterFrame(e:Event):void
    {
        if (lastTime == 0)
        {
            lastTime = getTimer();
            return;
        }

        var now:int = getTimer();

        var passedTime:int = now - lastTime;

        for each (var f:Function in notifies)
        {
            f.apply(null, [passedTime]);
        }

        lastTime = now;
    }

    private var notifies:Array = [];

    public function addNotify(handler:Function):void {

        if (handler == null)
        {
            return;
        }

        if (notifies.indexOf(handler) == -1)
        {
            notifies.push(handler);
        }
    }

    public function removeNotify(handler:Function):void
    {
        if (handler == null)
        {
            return;
        }

        var index:int = notifies.indexOf(handler);

        if (index != -1)
        {
            notifies.splice(index, 1);
        }
    }

}