需要频繁更新 Texture,怎么处理?

  • Creator 版本:2.2.0

  • 目标平台: Web,微信小游戏

  • 详细报错信息,包含调用堆栈:

CCDebug.js:246 object already destroyed
50.cc.warnID @ CCDebug.js:246
206.prototype.destroy @ CCObject.js:307
destroy @ CCTexture2D.js:618
(anonymous) @ CCClass.js:761
NewClass.UpdateTextureCanvas @ TestSpa.ts:103
NewClass.update @ TestSpa.ts:95
eval @ VM4924:3
(anonymous) @ component-scheduler.js:224
invoke @ component-scheduler.js:189
updatePhase @ component-scheduler.js:495
mainLoop @ CCDirector.js:948
callback @ CCGame.js:643

texture-2d.js:247 WebGL: INVALID_OPERATION: texImage2D: no texture bound to target
_setImage @ texture-2d.js:247
_setMipmap @ texture-2d.js:302
update @ texture-2d.js:107
handleLoadedTexture @ CCTexture2D.js:677
initWithElement @ CCTexture2D.js:532
NewClass.UpdateTextureCanvas @ TestSpa.ts:107
NewClass.update @ TestSpa.ts:96
eval @ VM5458:3
(anonymous) @ component-scheduler.js:224
invoke @ component-scheduler.js:189
updatePhase @ component-scheduler.js:495
mainLoop @ CCDirector.js:948
callback @ CCGame.js:643

callbacks-invoker.js:325 Uncaught TypeError: Cannot read property ‘load’ of null
at cc_Texture2D.213.proto.emit (callbacks-invoker.js:325)
at cc_Texture2D.handleLoadedTexture (CCTexture2D.js:684)
at cc_Texture2D.initWithElement (CCTexture2D.js:532)
at NewClass.UpdateTextureCanvas (TestSpa.ts:107)
at NewClass.update (TestSpa.ts:96)
at eval (eval at createInvokeImpl (component-scheduler.js:219), :3:65)
at CCClass._invoke (component-scheduler.js:224)
at CCClass.invoke (component-scheduler.js:189)
at CCClass.updatePhase (component-scheduler.js:495)
at 51.cc.Director.mainLoop (CCDirector.js:948)

  • 重现方式:
    代码:

start() {
this.m_testTexture = new cc.Texture2D();
this.target.spriteFrame.setTexture(this.m_testTexture);
this.m_Canvas = document.createElement(‘canvas’);
}

update(dt: number) {
this.m_testTexture.destroy();
this.m_testTexture.initWithElement(this.m_Canvas);
this.m_testTexture.handleLoadedTexture();
}

  • 出现概率:
    100%

请问大大怎么处理?
项目需要比较频繁的更新texture

为什么直接修改spriteFrame呢,只修改纹理,spriteFrame的其他属性还是原来老图的属性,

看代码update里面,就是只更新贴图

日志第一行说了对象已经销毁,把this.m_testTexture.destroy()去掉试试

取消了什么都不显示了

destroy好像是最后还是下一帧执行,要不试试再update destroy,再lateUpdate更新

解决了吗~我现在也是对贴图进行更新,在update中消耗太大~

为什么要用update呢,事件响应不行吗

在c++层面做性能应该更好点吧