2
votes

I am new to Cocos2D and I have question about texuture loading.

When I use code like follows to create two sprites ,does it load the texture "alien.png" two times into memory? Or does it save only one copy and create only one OpenGL texture ?

sp1=[CCSprite spriteWithFile:@"alien.png"];
sp2[CCSprite spriteWithFile:@"alien.png"];
1

1 Answers

3
votes

If you have a look at the way Cocos2D is implemented (don't forget that it's open source and you can drill down into every method), you will see that [CCSprite spriteWithFile:@"alien.png"] uses [[CCTextureCache sharedTextureCache] addImage: filename]; to load/cache the texture. The addImage method on the CCTextureCache singleton checks to see if the texture is already cached, and only if it's not will start to load it from the specified path. Now, with the texture cached, it doesn't matter how many times you draw it on the screen, you will not load the texture in memory more than once.