首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >压缩AS3计时器

压缩AS3计时器
EN

Stack Overflow用户
提问于 2014-03-13 09:16:16
回答 1查看 22关注 0票数 1

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-13 10:47:06

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

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

下面是一个简单的类,您可以添加notify。

代码语言:javascript
复制
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);
        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22367243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档