CreateJS切换自动收报机的方式离

时间:2015-07-25 16:10:54

标签: listener game-engine cpu-usage createjs ticker

我正在开发一款恰好有几个场景的帆布游戏。完成后,每个场景可能最终成为静态最终帧。情况是“tick”事件的自动收报机和监听器仍在运行并继续呈现全速 - 这就要求使用cpu。

我试图在场景结束时删除侦听器并在用户交互时添加它们并启动下一个场景。

我想知道“createJS”这样做的方式是什么。

我看到其他一些选项,但有点迷失如何继续:

  • 缓存“整个”最后一帧。这会使得股票代码“完全没有”表现吗?
  • 暂停代码并检查handleTick方法中的暂停属性:似乎没有完全降低CPU使用率。

有人可以推荐一种方法吗?

旁注:我需要在与ticker绑定的tick函数中使用我真实的“this”对象。我怎样才能做到这一点?现在我使用这段代码:

 createjs.Ticker.addEventListener("tick", handleTick);
 function handleTick(event) {
     // Actions carried out each tick (aka frame)
     if (!event.paused) {
         // Actions carried out when the Ticker is not paused.
     }
 }

内部handleTick“this”不是我添加监听器的对象。

1 个答案:

答案 0 :(得分:0)

只要handleTick存在于您当前的范围内,简单的createjs.Ticker.removeEventListener("tick", handleTick);就可以了。 See this example

有几种方法可以访问分配了tick侦听器的对象的范围。例如,您可以简单地将this分配给局部变量,如下所示:

var _this = this;
function handleTick(){
    //"_this" now refers to the scope that defined handleTick.
}

或者你可以使用代表。在此示例中,我使用jQuery的代理函数将handleTick范围调整为this

var handleTick = $.proxy(function(){
    //"this" refers to the scope that defined handleTick.
}, this);
createjs.Ticker.addEventListener("tick", tickHandler);