我正在向DOM添加一个画布,然后使用CreateJS构建游戏。我正在初始化CreateJS ticker,并添加动画和图形。一切都很好,但只有一次。如果我删除画布和游戏代码,以后就无法再次添加它 - 画布就不再显示任何内容了。
我的问题是:如何正确清除所有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);
}