使用Cocos2d-html5,有什么方法可以“退出”引擎并重新初始化其状态,以便以后可以重新启动引擎。我们在单个页面的Web应用中使用Cocos2d游戏。如果用户导航到另一个页面,我们希望以编程方式删除canvas div并尝试退出Cocos2d引擎。不幸的是,似乎没有好办法做到这一点,并试图加载一个新游戏导致错误。Cocos2d-html5 - 重新启动/退出引擎可能吗?
如何清理卸载场景并退出引擎?
我们正在使用V3.1并尝试了几种方法,它们都不起作用。例如:
- 尝试重置保存在cc对象中的许多变量(详细内容如下)。这种方法迫使你重置在_开始时看起来是私有的变量,这些变量不在文档中。
- 挑选,选择和重写一些CCBoot.js,但这看起来很复杂,并且不能持续引擎更新,因为它的大部分依赖于整个库。
其他方法我想到了,但听起来就像一个黑客:
3.空整个CC对象不知何故再次运行该脚本,但可能意味着剥离出一个脚本标记或模块添加和重新运行。
4.将cc对象包裹在另一个对象中,以便重置。 CCBoot.js看起来像它可能会附加一些东西到窗口对象。
我已经得到了第一种方法最远,但我坚持与上下文问题。当离开画布,之前我从DOM中删除它。我把这些:
// Remove anything that might possibly keep a reference to the old context
cc.textureCache.removeAllTextures();
cc._drawingUtil = null;
cc.stencilBits = null;
// Purge the cc.director, schedules, event listeners, running scene, animations, cached data.
cc.director.purgeDirector();
// Remove references to the context
cc._renderContext = null;
cc.webglContext = null;
window.gl = null;
cc._mainRenderContextBackup = null;
// Remove reference to DOM elements
cc._canvas = null;
cc.container = null;
cc._gameDiv = null;
// Reset CCBoot variables that might stop us from re-initialising
cc._rendererInitialized = false;
cc._setupCalled = false;
cc._renderType = -1;
那么当我们重新启动了第二次以后的呼叫
// Reset all system and flag variables
cc._initSys(cc.game.config, cc.game.CONFIG_KEY);
cc.game.run();
这里是错误的,我得到的那种。它看起来像它无法正常复位的背景:
WebGL: INVALID_OPERATION: bindTexture: object not from this context
WebGL: INVALID_OPERATION: texImage2D: no texture
WebGL: INVALID_OPERATION: uniformMatrix4fv: location is not from current program
WebGL: INVALID_OPERATION: vertexAttribPointer: no bound ARRAY_BUFFER