SpriteFrame动态加载图片,不能显示

  • Creator 版本:2.1.2

  • 目标平台: Web

我的代码大概如下,编辑器拖了两个png到textureA和textureB,然后点击按钮调用changeTexture
但是显示有点奇怪
是什么原因呢?



@property({ type: cc.Texture2D })
textureA: cc.Texture2D = null
@property({ type: cc.Texture2D })
textureB: cc.Texture2D = null
changeTexture() {
let sp = cc.find(“Canvas/cocos”).getComponent(cc.Sprite)
if (this.textureA === sp.spriteFrame.getTexture()) {
sp.spriteFrame.setTexture(this.textureB)
cc.log(“TexB”)
} else {
sp.spriteFrame.setTexture(this.textureA)
cc.log(“TexA”)
}
}

图片是

自动合图的话,A,B可能合到一个Texture了,只是rect不同。比如B是rect(0,0,100,200),A是rect(100,0, 20,20),设置texture需要同时设置后面的参数rect等等。 最稳妥还是设置spriteframe=spfA( spfB)