this.texture.initWithElement is not a function

构建cocos creator 3d 小游戏项目的排行榜时, 开发者工具报 如下错误。

onLoad() {

    this.texture = new cc.Texture2D();

}

update(dt: number) {

           var openDataContenxt = wx.getOpenDataContext();

           var sharedCanvas = openDataContenxt.canvas;               

           this.texture.initWithElement(sharedCanvas);

           this.texture.handleLoadedTexture();

           this.wxSprite.spriteFrame = new cc.SpriteFrame(this.texture);

           this.subContextView.updateSubContextViewport();

  }

这个很明显是 texture 为 null,断点一下看就知道问题出在哪里了

texture不为null

那你子域用的引擎是 3D 还是 2D,3D 没有 initWithElement 方法

3D 引擎不支持子域

我也遇到了,加载远程图片跨域问题,论坛上找了这个方法,报错,creator是3.x版本,有方案吗?

楼主咋解决的?能分享下吗


试试?

1赞

谢谢,解决了

可以设置好了 texture 然后 直接动态该 texture 的img 吗