简体   繁体   English

如何完全清除CreateJS代码和画布?

[英]How to clear CreateJS code and canvas completely?

I am adding a canvas to the DOM, and then using CreateJS to build a game. 我正在向DOM添加一个画布,然后使用CreateJS来构建游戏。 I am initialising a CreateJS ticker, and adding animations and graphics. 我正在初始化CreateJS自动收录器,并添加动画和图形。 Everything works fine, but only once. 一切正常,但只有一次。 If I remove the canvas and the game code, I can't add it again later on - the canvas just doesn't show anything anymore. 如果我删除画布和游戏代码,我以后不能再添加它 - 画布只是不再显示任何内容。

My question: how do I properly clear all CreateJS functions from memory, and start anew with a new canvas? 我的问题:如何从内存中正确清除所有CreateJS函数,并重新开始使用新画布? It seems that CreateJS keeps holding on to old listeners and elements, even though I removed everything. 似乎CreateJS始终坚持使用旧的侦听器和元素,即使我删除了所有内容。

This is my code: 这是我的代码:

// 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);
}

If you are changing canvases, or resetting the stage, make sure to remove the DOM events from the stage. 如果要更改画布或重置舞台,请确保从舞台中删除DOM事件。 From the docs: 来自文档:


enableDomEvents(Boolean enable) enableDomEvents(布尔启用)

Enables or disables the event listeners that stage adds to DOM elements (window, document and canvas). 启用或禁用舞台添加到DOM元素(窗口,文档和画布)的事件侦听器。 It is good practice to disable events when disposing of a Stage instance, otherwise the stage will continue to receive events from the page. 处理Stage实例时禁用事件是一种很好的做法,否则该阶段将继续从页面接收事件。

When changing the canvas property you must disable the events on the old canvas, and enable events on the new canvas or mouse events will not work as expected. 更改canvas属性时,必须禁用旧画布上的事件,并在新画布上启用事件或鼠标事件将无法按预期工作。 For example: 例如:

myStage.enableDOMEvents(false);
myStage.canvas = anotherCanvas;
myStage.enableDOMEvents(true);

http://www.createjs.com/Docs/EaselJS/classes/Stage.html#method_enableDOMEvents http://www.createjs.com/Docs/EaselJS/classes/Stage.html#method_enableDOMEvents

I am not sure if this will solve your problem, but it could help. 我不确定这是否能解决您的问题,但它可以提供帮助。

i recommend you to use this 我建议你使用它

//create listener tick
createjs.Ticker.addEventListener('tick',tick);
//remove listener tick
createjs.Ticker.removeEventListener('tick',tick);
//exemple stopped object moves in Y axe if it exceeds heigth of canvas 
createjs.Ticker.addEventListener('tick',tick);
function tick(){
    console.log( myBitmap.x +" "+myCanvas.width);
    if (myBitmap.y < myCanvas.height){
        myBitmap.y=myBitmap.y+10;
    }else{
        createjs.Ticker.removeEventListener('tick',tick);
    }
    scene.update();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM