【发布时间】:2023-03-14 20:40:01
【问题描述】:
我正在向 DOM 添加一个画布,然后使用 CreateJS 构建一个游戏。我正在初始化一个 CreateJS 代码,并添加动画和图形。 一切正常,但只有一次。如果我删除了画布和游戏代码,以后我将无法再次添加它 - 画布不再显示任何内容。
我的问题:如何正确清除内存中的所有 CreateJS 函数,并从新画布重新开始?似乎 CreateJS 继续保留旧的侦听器和元素,即使我删除了所有内容。
这是我的代码:
// remove canvas and then restart - this causes buggy behavior
this.removeAndRestart = function() {
document.getElementById("gamecontainer").innerHTML = '';
createjs.Ticker.removeAllEventListeners();
setTimeout(this.showGame, 1000);
}
// simply start the game - this works only the first time
this.showGame = function() {
document.getElementById("gamecontainer").innerHTML = '<canvas id="gamecanvas" width="900" height="400"></canvas>';
this.stage = new createjs.Stage("gamecanvas");
// create the ticker and update the stage
createjs.Ticker.setFPS(30);
createjs.Ticker.addEventListener("tick", tickHandler);
}
【问题讨论】:
-
您在重新启动游戏时是否在控制台中看到任何错误?你能在jsfiddle.net上举个例子吗?
-
这听起来可能是 EaselJS 中的一个错误。这是在社区论坛上交叉发布的:community.createjs.com/discussions/easeljs/… -- 我们将对其进行调查,并在那里回答。
-
我在这里创建了一个小提琴,它没有显示我的所有问题,但你可以看到在重新启动整个画布游戏应用程序后补间没有触发。 jsfiddle.net/BUyyv
标签: javascript canvas easeljs createjs