SpriteFrame constructor in CocosCreator 1.1.2

Hi all
I use

var spriteFrame = new cc.SpriteFrame( texture );

and got this warning:

It’s not recommended to use SpriteFrame constructor (new SpriteFrame) to create SpriteFrame instance since it’s memory will be unable to manage. Instead please use cc.loader.loadRes to get SpriteFrame instance from loading, or define a cc.SpriteFrame property in your component and drag the SpriteFrame onto it.
Then i switch to
cc.loader.load(avatarLink, function(err, texture){
// i need spriteFrame, how to get it from texture
self.avatar.getComponent(cc.Sprite).spriteFrame= ???;
});

My question is how to set sprifreFrame when loader.load load successfull

When working with internal resources you can use new typed loader (but you’ll need to move your files to /resources):

cc.loader.loadRes("image.png", cc.SpriteFrame, function(err, data){
	// if loading succeeds data will be of type cc.SpriteFrame
	this.node.getComponent(cc.Sprite).spriteFrame = data
})

However I did a couple of quick tests and couldn’t reproduce any memory leaks after creating new cc.SpriteFrame(texture) manually, it seems that there’s nothing that garbage collector cannot handle. So probably you can do as you did before just ignore these warnings. And if memory problems kick in, you’ll know where to look :wink:


If you’re loading images from external http (like user’s facebook avatar), then I guess you’ll just have to ignore warnings, they refer to a differend use-case

1 Like

Thank you so much