I’m facing an issue in Cocos Creator 3.8.2 when setting a Tiled Sprite dynamically at runtime. Initially, I was getting the error:
Uncaught TypeError: Cannot read property ‘vb’ of null
Issue Details:
- I have a Sprite component with the Type set to “Tiled” and Size Mode set to “Custom”.
- The SpriteFrame is set dynamically using a downloaded image.
- The node’s UITransform size is set to 576x1024, but the trimmed size of the SpriteFrame is 128x128.
- If I change the Sprite Type to “Simple”, everything works fine.
- If I change Size Mode to “Trimmed”, it also works, but this modifies the node size, which I don’t want.
Code:
var self = this;
cc.assetManager.loadRemote<cc.ImageAsset>(cc.native.fileUtils.getWritablePath()+savePath_ + "/" + imageName_, function (err, imageAsset) {
}
self.setTextureToSprite("image.jpg", imageAsset, self.MySprite);
Only when I used a regular function (function() {}
) with ‘this’ directly, the issue was resolved:
cc.assetManager.loadRemote<cc.ImageAsset>(cc.native.fileUtils.getWritablePath()+savePath_ + "/" + imageName_, function (err, imageAsset) {
}
this.setTextureToSprite("image.jpg", imageAsset, this.MySprite);
Why does self = this;
cause an issue in Cocos 3.8.2 when using a Tiled Sprite?
Why does this issue occur only with a “Tiled” Sprite and “Custom” Size Mode, but not with “Simple” or “Trimmed”?
Why do we commonly use var self = this;
in Cocos TypeScript? Is it still necessary in newer Cocos versions?