cocos2d - Replacing cached sprite image - cocos2d-iphone

I'm new to cocos2d. I've been following a tutorial which I've got working but when I updated the image I used to shoot fireballs by deleting and replacing it from the resources folder, the image no longer renders. What do I have to do when replacing an image?
Before I swapped the images:
CCSprite *projectile = [CCSprite spriteWithFile:#"Projectile.png"
rect:CGRectMake(0, 0, 20, 20)];
After swapping images:
CCSprite *projectile = [CCSprite spriteWithFile:#"Fireball.png" rect:CGRectMake(0, 0, 20, 20)];
Do I need to delete the cache somehow and re-add the image?
EDIT:
I added this line below my code and it worked:
[projectile setTexture:[[CCTextureCache sharedTextureCache]
addImage:#"Fireball.png"]];
Strange how I have to add a new line of code to replace an image.

I find that sometimes i need to clean and build for the updated texture to finally make it to the app's bundle ... (I suspect this happens when i change a resource while the app is running, but frankly i have lost too much time with this to investigate). Now i de-facto clean and build when changing resources.
I am not certain this is a 'cocos2d' discussion, but rather an Xcode one. The cocos2d texture cache is entirely constructed in memory, during the execution of your app. It is a mechanism that 1) helps keep the memory footprint smaller, and 2) also boosts performance since a texture file is only loaded once if you dont clear the cache. Xcode on the other hand keeps copies of all your files in multiple places. By 'cleaning' a build, you ensure that previous versions of files are removed, and all the current resources (including textures) are placed in the appropriate places for the development environment to function properly.

Related

Memory Issue in cocos2dx

Thanx in advance..I have a problem , how we release memory in cocos2dx ??
In my game, my first scene takes a lot of memory because there are so many animations run at a single time on this scene,there are so many animations , so i am satisfied with this but when we go to next scene, it does not release previous memory used,this is my problem so how we release memory used by previous scene when we change the scene?
To go between scenes you can either call pushScene or replaceScene.
Push Scene
If you're pushing between scenes then I recommend that you have a loading scene in between. It should be lightweight so that you have a chance to release everything from your old scene. This is where the onEnter(DidFinish) and onExit(DidStart) methods come in handy.
The chain of calls would be:
oldScene->onExitDidStart()
loadScene->onEnter()
oldScene->onExit() <-- this is where you release everything
loadScene->onEnterDidFinish() <-- this is where you load up the new scene
newScene->onEnter()
and so on... If you've managed your memory correctly then you shouldn't have 2 heavy scenes at once.
Replace Scene
This is a much easier scenario. You simply need to make sure that for the new scene you load as little as possible until the old scene has completely disappeared. I.e. when onEnterDidFinish() is called, or even 1 frame after it.
Cocos2d-x supports auto release memory. For example, when you create a sprite by calling Sprite::create() it will add this sprite to the auto-release pool. If you don't add this sprite to become the child (or n-th leveled child) of the current scene, it will be released.
Do not create an object using new,use create() instead when you call the next scene, everything from the last scene are released.

adding a normal CCSprite to CCSpriteBatchNode crashes

I am getting this exception in logs
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite is not using the same texture id'
What I am trying to do is adding a normal "myfile.png" file to a SpriteBatchNode
**declaration of batch node
CCSpriteBatchNode *_backgroundLayer = [CCSpriteBatchNode batchNodeWithFile:#"sprites.png"];
** usage
This line works perfect
CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:#"PngFileKeptInSpriteSheet.png"];
[_backgroundLayer addChild:sprite1];
But, when I use a direct *.png file to add to batch node, It crashes
CCSprite *sprite2 = [CCSprite spriteWithFile:#"myfile.png"];
crashes on line
[_backgroundLayer addChild:sprite2];
On further debugging I found that:
The assertion failure is in file CCSpriteBatchNode.m
inside method -(void) addChild:(CCSprite*)child z:(NSInteger)z tag:(NSInteger) aTag
at line NSAssert( child.texture.name == textureAtlas_.texture.name, #"CCSprite is not using the same texture id");
P.S. : by "normal" I mean not taken from *.plist file
First and foremost, I'd update cocos2D. However, that isn't your problem and probably isn't "fixed" in the latest version anyway. This isn't really a bug
Batch nodes require that all the sprites that you intend to batch are using the same texture. When you load a sprite sheet, it uses one, large texture. When you call spriteWithFile, cocos2d creates a texture from that image.
It's rare that you'll want to create a batch node from a sprite using spriteWithFile. The only scenario I can think of is when you want to draw the same image many times. (Rather than many images from the same texture).
In short, what you are trying to do is unsupported and doesn't make much sense anyway as the two sprites wouldn't be batchable.

How to load the loading scene background in cocos2d without blanking out?

I have a cocos2d game that has a loading scene where we load a bunch of assets. The game starts with the splash screen, and then launches the loading scene. The loading scene starts by loading the background, so the user sees the loading scene background while the assets are being loaded.
I load the loading scene background by calling CCSprite::spriteWithFile: and passing the filepath: loadingbackground.pvr.ccz
It seems to work differently on different devices:
On iphone (3gs) simulator, I see the loading scene as expected.
On iphone retina simulator, I don't see the loading scene (there aren't many assets yet, so may be happening quickly) and it goes directly to the main menu scene.
On the ipad 3 device, the splash screen comes up, and then there is a half second of black screen, and then the main menu scene shows up.
I want to see what I can do to avoid that black screen showing up on iPad 3. I suspect this is because of the time taken to load the loading background.
I have tried the following optimizations (mostly based on #Steffen's blog post on memory optimization):
Moved the loading background (originally 2.3 MB RGB8 png file) into a pvr.ccz spritesheet by itself, which reduced its size to 1.8 MB.
Removed the image from the texture soon after use.
I still see a black screen on iPad 3. Any suggestions?
Update: Found the issue - I had some code where I was overriding OnEnter and calling [[CCDirector sharedDirector]replaceScene] in it, and also calling the same from the background thread. Removed the OnEnter overload and it worked without flicker.
Thanks
Ignore whatever happens in Simulator. That's not relevant, focus on the device.
When the loading scene initializes and you add the loading scene's background, make sure you schedule update and load your assets in the update method. Otherwise if you load the assets in init, the background won't be drawn because you're loading all the assets before cocos2d gets to redraw the screen.
If this still fails, simply send the visit message to the background sprite followed by [[CCDirector sharedDirector] drawScene]. This forces a redraw of the scene.
Is this cocos2d-iphone or cocos2d-x ? make sure the tags are correct :)
I think you're referring to the startup flicker, there are a few ways to avoid that.
first thing you need to make sure you're handling the rootViewController correctly for iOS 6 and iOS 5 and below, there are a little changes for each.
You can find a little reference here:
http://www.cocos2d-iphone.org/forum/topic/34471
Second thing you need to know that simulator's behaviour is not stable, you should always rely on real devices for testing, but it's very likely you'll still have the flicker issues.
Sorry I didn't provide example code, but you haven't supported enough information to know what's the real issue here.

CCRenderTexture saveToFile takes up a large amount of memory

I'm working on a game and I want to create thumbnails of each level so I can use them in a menu. I do this with CCRenderTexture like this:
CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:bounds.size.width height:bounds.size.height];
ScreenShotLayer *scene = [[ScreenShotLayer alloc] init];
[renderer begin];
[scene visit];
[renderer end];
[renderer saveToFile:[NSString stringWithFormat:#"level%#.png", level.levelId] format: kCCImageFormatPNG];
This all works well and my image is saved, but it takes up a huge amount of memory and never releases it. Without saving this data my game takes 8 MB of RAM. When I save the image it takes over 430 MB.
If I leave everything the same and just comment out the saveToFile call I'm back to my 8 MB memory usage. I've debugged this with the leaks tool and it doesn't find any leaks.
Is this a framework issue? Is there a better way to handle this? Am I doing something silly?
My whole game is open source and you can get it at https://github.com/zgrossbart/bridges. This code happens in LevelMgr.mm.
Thanks for the help,
Zack
I found a solution to this problem on the Cocos2d forum and with a bit of poking around the code. You can read the forum post her:
http://www.cocos2d-iphone.org/forum/topic/48890
I still see CCRenderTexture leak a little memory. The Cocos2d team has tested this and says it's working in 2.1-beta. There's also a big performance improvement I could have made to my code.
I was using CCRenderTexture to create a thumbnail image of a Cocos2d scene. I did that by using CCRenderTexture to create a full size image and then using a special UIImage to resize and crop the image. It's much more efficient to create a CCRenderTexture of the size I want in the end and scale my Cocos2d scene to the size I want in the end.

whats happen when leaving a CCscene in cocos2d?

using cocos2d.
i need to know what happen when leaving a scene(replace scene) with the next:
shared frame cache (plist and png 's that where spriteSheets )
sprites (the removed?)
actions (they stoped and released?)
bodies (box2d) (i have to destroy them or its auto? )
is that happen auto , or i need to do something on the dealloc( ex: removing childs)
thanks .
All caches remain unchanged, it's up to you to remove textures, sprite frames, etc. as needed.
Sprites and all other nodes of the scene are removed automatically. So are actions.
Box2D is a separate library and not integrated in cocos2d's source code. The Box2D source code is merely distributed with cocos2d-iphone. That means you are responsible for cleaning up all Box2D objects and the world. There's nothing automatic there.