CCSprite not showing Background Image - ios

I am facing an unknown error since from yesterday. I am creating CCSprites or CCMenuItemImage but it set black background instead of background image. Following is my code, I know its fine because I used it before many times.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"challenge_screen.plist"];
CCSprite *bg = [CCSprite spriteWithFile:#"ads.png"];
[bg setPosition:background.position];
// [bg setContentSize:CGSizeMake(100, 100)];
[self addChild:bg z:1000];
//CGSize windowSize = [[CCDirector sharedDirector] winSize];
CCMenuItemImage *coinMenuItem = [[CCMenuItemImage alloc] initWithNormalSprite:[CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"coin.png"]] selectedSprite:nil disabledSprite:nil block:^(id sender)
{
NSLog(#"I am Tapped");
}];
coinMenuItem.position = ccp(100, 100);
CCMenu *mainMenu = [CCMenu menuWithItems:coinMenuItem, nil];
mainMenu.position = CGPointZero;
[self addChild:mainMenu];
Attached is screenshot.
Thanks in advance.

I am guessing that you are loading this sprite sheet (challenge_screen.plist and the associated texture file, which frequently is challenge_screen.png or challenge_screen.pvr.*) in a color mode that doesn't have transparency.
First, make sure that the associated texture file shows transparency itself. Maybe something messed with this particular texture.
Once you checked that, if the associated texture is .PNG. then you have to set the texture loading format in code like this. You have to set the texture format before loading the texture itself (the texture loads as a side effect of adding the SpriteFrames to the cache).
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA4444];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"challenge_screen.plist"];`
...
You can also try the kCCTexture2DPixelFormat_RGBA8888 mode if RGBA444 produces banding with your graphics and if you are good regarding free memory.
On the other hand, if the texture is a PVR.*, then the format in which the texture loads is embedded in the file, and setting the texture format in code doesn't make a difference. You will then need to regenerate your sprite sheet using the appropriate format (through TexturePacker or similar).

Is your background in the sprite sheet? If so try:
CCSprite *bg = [CCSprite spriteWithSpriteFrameName:#"ads.png"];
If it is the menu item and you know the code works, it must be an asset issue.

Related

Crash when restarting a scene

So, here is my scene problem: I start from a Menu Scene, then go into the InGame Scene, and when the character is dead, I go to the Menu Scene again, all this using:
[[CCDirector sharedDirector] replaceScene:[MainMenu scene]];
and
[[CCDirector sharedDirector] replaceScene:[InGame scene]];
After losing the game, and trying to go back to the game, my SpriteSheet crashes with the error :
'CCSprite is not using the same texture id'
Here is how I init the animation :
- (void) initSprite:(NSString *)plist andTexture:(NSString *)texture_ {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:texture_];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i=1; i<=12; i++) {
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"%d.png",i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.05f];
texture = [CCSprite spriteWithSpriteFrameName:#"1.png"];
walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim]];
[texture runAction:walkAction];
texture.position = position;
texture.tag = HeroType;
[spriteSheet addChild:texture];
[self addChild:spriteSheet];
}
The crash occurs when I add the texture to the spriteSheet:
[spriteSheet addChild:texture];
I believe the problem comes from the deallocation of the texture..
I don't use ARC.
You probably have a "1.png" in the cache that corresponds to another animation that was created before quit-restart sequence. Maybe you want to purge the sprite frame cache (and possibly a whole lot of other things) prior to restarting.
I completely avoid "1.png" through 'NNNN.png' since in all likelihood all your animations will have them. Instead i use things like :
walk_classKey_upNNNN.png {up,down,left,right,idle,jump ... and any other map stance i need)
fight_classKey_strikeNNNN.png {strike,hurt,dead ... for example)
classKey is {fighter,rogue, ... etc ... whichever unique soldier type I have)
and i name my plists/textures the same
walk_fighter_up-hd.plist (using texture packer, the plist embeds the texture name).
fight_rogue_cheapShot-hd.plist (cheapShot is one of my rogue's skill in my current game).

""CCSprite is not using the same texture id"" workaround?

I am animating a whole body through SpriteSheets with CCSpriteBatchNode and CCSpriteFrameCache. Now user can add his own pic to that body which when i try to addChild to Spritesheet crashes with error "CCSprite is not using the same texture id"
Now i know the face CCSprite was not in that cache/texture(it was created through texturepacker) and the crash was normal but i wanted to know if there was a workaround to this as i have to add a face to that body through user interaction and animate that body. And by far using spritesheets is the best option for animation. anyone??
In this case what you can do is You take picture of user , then you make texture from user's image .
Then Add that texture to the CCTextureCache . Now you have texture of user image. Now you can use that texture in animation.
Make Texture from Sprite(You can make sprite from user image)
CCSprite *spr = nil;//your sprite
CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:spr.contentSize.width height:spr.contentSize.height];
spr.anchorPoint = ccp(0, 0);
spr.position = ccp(0, 0);
[renderTexture addChild:spr];
[renderTexture begin];
[spr draw]; // or [spr visit];
[renderTexture end];
CCTexture2D *result = renderTexture.sprite.texture;
Add that Texture in to Texture Cache.
[[CCTextureCache sharedTextureCache] addTexture]
When you create a CCSpriteBatchNode it is linked to a single texture. This is the point of a CCSpriteBatchNode: to draw different sprites that use the same texture to reduce OpenGL draw calls and increase efficiency.
The easiest workaround (if you have not reached a performance-critical point) would be to use a regular CCLayer instead of a CCSpriteBatchNode.
If you still want to add different CCSprites (say, the body, the limbs and the head of your character) to the same CCSpriteBatchNode, the you need to build a single sprite sheet (or texture pack) which contains all the body parts that you need to add to the CCSpriteBatchNode. This single sprite sheet will be the only one that the CCSpriteBatchNode will use. You won't be able to add CCSprites that are not using this sprite sheet.
You can not Manually add CCSprite to SpriteSheets. Because When you create animated Sprite using texturepacker then i hope that you know it also created with .plist file and it loas image from it with its size.
When you add manually CCSprite then it is nor found from SpriteFramesWithFile. may be you got error.
another way for add animated CCSprite without use of texturepacker
CCSprite *dog = [CCSprite spriteWithFile:#"dog1.gif"];
dog.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:dog z:2];
NSMutableArray *animFrames = [NSMutableArray array];
for( int i=1;i<=5;i++)
{
NSString* file = [NSString stringWithFormat:#"dog%d.gif", i];
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
[animFrames addObject:frame];
}
CCAnimation * animation = [CCAnimation animationWithSpriteFrames:animFrames];
animation.delayPerUnit = 0.07f;
animation.restoreOriginalFrame = YES;
CCAnimate *animAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
[dog runAction:animAction];
Above code is just describe that how can you add animated CCSprite without use of texturepacker
Here you can also change array of Images so you may be add manually image also.

Creating multiply effect in painting iPad app

I am trying to create a painting feature in an iPad app for iOS. I have managed to get the colour to appear through touch, but I would like to recreate the multiply functionality of photoshop so the underlying black and white image continues to show through the colour. I started doing it with opacity but going over the same spot will result in it eventually being removed. I am using cocos2d and this is the sample code.
in header
CCSprite *background;
CCRenderTexture *target;
CCSprite *brush;
in init method:
background = [CCSprite spriteWithFile:#"background.png"];
background.position = ccp(self.size.width/2, self.size.height/2);
[self addChild: background z:-1];
target = [[CCRenderTexture alloc] initWithWidth:self.size.width height:self.size.height pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[target setPosition:ccp(self.size.width/2, self.size.height/2)];
brush = [[CCSprite spriteWithSpriteFrameName:#"brush_spot.png"] retain];
[brush setColor:ccRED];
in -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event method:
[target begin];
[brush setPosition:<CALCULATED POSITION>];
[brush visit];
[target end];
I have tried using different blend functions on the brush but nothing has managed to create the look I want. I did get the correct effect when adding a sprite directly on top of the background and setting its blend function to
[sprite setBlendFunc:(ccBlendFunc) { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }];
but when I try to use this blend function on the brush nothing appears on the screen.
Thanks
Iain
The blend function blends whatever is on top with whatever is behind. When you render to a texture you blend with whatever the texture is blanked to.. Which I guess is something with alpha 0. So you should draw to the texture using opacity.
When you have filled the texture with color and attached it to a sprite, you can render it again and blend it with your background sprite. Then you can blend using multiply.

optimize animation in cocos2d failed

When I didn't optimize animation,it worked well.But when I optimized my animation,the program crashed.And Xcode log said:
Assertion failure in -[CCSpriteBatchNode addChild:z:tag:], /Users/hanpengbo/Documents/Xcode/cocos2d_helloWorld/cocos2d_helloWorld/libs/coco‌​s2d/CCSpriteBatchNode.m:183
in CCSpriteBatchNode.m:183,there is
NSAssert( child.texture.name == textureAtlas_.texture.name, #"CCSprite is not using the same texture id");
here is my code
// cache
CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:#"birdAtlas.plist"];
// frame array
NSMutableArray *framesArray=[NSMutableArray array];
for (int i=1; i<10; i++) {
NSString *frameName=[NSString stringWithFormat:#"bird%d.png", i];
id frameObject=[cache spriteFrameByName:frameName];
[framesArray addObject:frameObject];
}
// animation object
id animObject=[CCAnimation animationWithFrames:framesArray delay:0.1];
// animation action
id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
animAction=[CCRepeatForever actionWithAction:animAction];
// sprite
cocosGuy = [CCSprite spriteWithFile: #"Icon.png"];//cocosGuy is CCSprite,declared earler
cocosGuy.position = ccp( 200, 300 );
// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:#"birdAtlas.png"];
[self addChild:batchNode];
[batchNode addChild:cocosGuy];
[cocosGuy runAction:animAction];
UPDATE:
here is the corrected code,and it works well
// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:#"birdAtlas.png"];
[cocosGuy setTexture:[batchNode texture]];
[self addChild:batchNode];
[batchNode addChild:cocosGuy];
For this to work, your Icon.png texture should be in the birdAtlas.png texture, with appropriate declaration in the .plist. Batchnodes 1) are created with one texture (and only one), and 2) only accept as children sprites that are from the SAME texture.
... and , i dont know your intent, but typically you would have
CCSprite *cocosGuy = [CCSprite spriteWithSpriteFrame:[cache spriteFrameByName:#"bird1.png"];
in that case, i think the batch node add will work.
... and , not sure using a batch node will be of any consequence for an animation, if the texture only contains the animation frames for that one animation. Frames are displayed one at a time, so i dont think you will benefit from the batched draw call.

CCRenderTexture and CCDirector render differently?

i wanted to use CCRenderTexture in my project because i have a lot of CCLabelBMFont that are mostly static. Adding them all to my Scene caused some performance issues (Yes you can use BatchNotes etc. but it didnt really help). So I rendered them into a single Texture which increased the performance significantly! But the problem is that the rendered texture and the directly rendered node look different. I have no idea why!
I created a cocos2d-sample project and and created this:
CCSprite* testImage = [CCSprite spriteWithFile:#"N.png"];
testImage.position = ccp(100,100);
CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:786 height:1024];
[rt beginWithClear:0 g:0 b:0 a:0];
[testImage visit];
[rt end];
CCSprite* renderedSprite = [CCSprite spriteWithTexture:rt.sprite.texture];
renderedSprite.position = ccp(386,512);
// Flip because CCRenderTexture is flipped
renderedSprite.flipY = YES;
// Add normal node an the rendered sprite
testImage.position = ccp(130,100);
[self addChild:testImage];
[self addChild:renderedSprite];
The results look like this:
How can this be? How can i make them look the same?
The N on the right is the sprite added the "normal" way and it is displayed correctly. The N on the left is the texture.
Edit:
i found this tutorial which hints that the blend functions are different. so can i am looking for the correct function to make them look the same.
I found the solution!
I used this example to create the sprite but when i looked into the cocos2d-manual again i saw, that you can add the CCRenderTexture directly to your scene. Thats what i did and it solved the problem!
So the correct code should be:
CCSprite* testImage = [CCSprite spriteWithFile:#"N.png"];
testImage.position = ccp(100,100);
CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:786 height:1024];
[rt begin];
[testImage visit];
[rt end];
rt.position = ccp(386,512);
// Add normal node an the rendered sprite
testImage.position = ccp(130,100);
[self addChild:testImage];
[self addChild:rt];

Resources