iPhone: Application crashes when I add a layer - iphone

My applications crashes when I try to add a layer in it with CCSprite.
Here is some code I use:
CCLayer *layerPause = [CCLayer node];
CCSprite *spriteBackgroundPause = [[CCSprite alloc] initWithFile:#"BackgroundMenu.jpg"];
[layerPause addChild:spriteBackgroundPause];
[self addChild:layerPause z:27];
Here is picture also:

You have to retain the layerPause variable because it seems an autoreleased object, try in this way :
CCLayer *layerPause = [[CCLayer node] retain];
CCSprite *spriteBackgroundPause = [[CCSprite alloc] initWithFile:#"BackgroundMenu.jpg"];
[layerPause addChild:spriteBackgroundPause];
[self addChild:layerPause z:27];

Why not do it simple .. ? Like this:
CCLayer * layer = [CCLayer alloc]init];
[self addchild: layer];
CCSPrite * sprite = [CCSPrite spriteWithFile:#"ImageName.png"];
[layer addChild:sprite];

Related

cocos2d CCScene background orientation

I'm trying to add a background image for my scene using the following code:
CCSprite* background = [CCSprite spriteWithFile:#"background-hd.png"];
background.tag = 1;
background.anchorPoint = CGPointMake(0,0);
[self addChild:background];
My apps orientation is landscape and the image's orientation is landscape as well, however I get the image orientated in portrait:
I think this is the solution with Layer which set the scene with orientation...
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
StartLayer *layer = [StartLayer node];
CCSprite *background = [CCSprite spriteWithFile:#"background-hd.png"];
background.anchorPoint = ccp(0,0);
[layer addChild:background z:-1];
[scene addChild: layer];
return scene;
}
See this link for More information Cocos2d-Scenes-and-Layers....
Hope this helpful to you...

how to animated sprite for cocos2d in ccz format

According to this tutorial PVR images seem to be the best format for iOS sprites. However after creating a sprite sheet with Texturepacker and exporting out to this format I cannot get the animation to work in cocos2d.
According to the documentation here I should be using
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"LuckyCompiled.plist"];
But neither the tutorial nor the documentation explain how to do animation, except for this. Which is what the code below is based on.
But this only places the base image onto the layer and does not animate.
CCSprite *sprite = [[CCSprite alloc]init];
sprite.position = ccp(player.contentSize.width/2+40, winSize.height/2+40);
// Obtain the shared instance of the cache
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
// load the frames
[cache addSpriteFramesWithFile:#"LuckyCompiled.plist"];
// It loads the frame named "frame1.png".
// IMPORTANT: It doesn't load the image "frame1.png". "frama1.png" is a just the name of the frame
CCSpriteFrame *frame = [cache spriteFrameByName:#"lucky1.png"];
[sprite setDisplayFrame:frame];
[self addChild:sprite];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 10; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"lucky%d.png",i]];
[animFrames addObject:frame];
}
NSLog(#"animaframes %#",animFrames);
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:[NSArray arrayWithArray:animFrames]];
[sprite runAction:[CCAnimate actionWithAnimation:animation]];
Answer:
Needed to have a delay otherwise animation wasnt noticeable
[CCAnimation animationWithSpriteFrames:[NSArray arrayWithArray:animFrames]];
should have been (also no need to make is nsarray, mutable is fine)
[CCAnimation animationWithSpriteFrames:frames delay:0.1f];
Here is the code that I tried, it works fine.
CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:#"walkFrames.plist"];
player = [CCSprite spriteWithSpriteFrameName:#"f1.png"];
NSMutableArray *frames = [NSMutableArray arrayWithCapacity:8];
for (int i = 1; i < 9; i++) {
NSString *file = [NSString stringWithFormat:#"f%d.png", i];
CCSpriteFrame *frame = [frameCache spriteFrameByName:file];
[frames addObject:frame];
}
CCAnimation *walkAnim =[CCAnimation animationWithSpriteFrames:frames delay:0.1f];
CCAnimate *animate = [CCAnimate actionWithAnimation:walkAnim];
CCRepeatForever *rep = [CCRepeatForever actionWithAction:animate];
player.position = ccp(23, 285);
[player runAction:rep];
[self addChild:player];

Cocos-2d actions -- CCallFunc not doing anything

Basically, I'm trying to animate a sprite. When moving right I want one animation to play, and when moving left another to play. Here's my code-- nothing is happening at all, the sprite is merely shown on the screen.
-(void)moveLeft{
[sprite stopActionByTag:0];
NSMutableArray *spriteFrames2 = [NSMutableArray array];
CCSpriteFrame *frame4 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:#"KnightSpritesLeft10.png"];
CCSpriteFrame *frame5 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:#"KnightSpritesLeft11.png"];
CCSpriteFrame *frame6 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:#"KnightSpritesLeft12.png"];
[spriteFrames2 addObjectsFromArray:[NSArray arrayWithObjects:frame4,frame5,frame4,frame6, nil]];
CCAnimation *anim = [CCAnimation animationWithFrames:spriteFrames2 delay:0.15f];
CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
repeat.tag = 1;
[sprite runAction:repeat];
id moveToLeft = [CCMoveTo actionWithDuration:3.0 position:CGPointMake(0, sprite.position.y)];
[sprite runAction:moveToLeft];
}
-(void)moveRight{
[sprite stopActionByTag:1];
CGSize winSize = [[CCDirector sharedDirector]winSize];
NSMutableArray *spriteFrames = [NSMutableArray array];
CCSpriteFrame *frame1 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:#"KnightSpritesRight6.png"];
CCSpriteFrame *frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:#"KnightSpritesRight4.png"];
CCSpriteFrame *frame3 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:#"KnightSpritesRight5.png"];
[spriteFrames addObjectsFromArray:[NSArray arrayWithObjects:frame1,frame2,frame1,frame3, nil]];
CCAnimation* anim = [CCAnimation animationWithFrames:spriteFrames delay:0.15f];
CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
repeat.tag = 0;
[sprite runAction:repeat];
id moveToRight = [CCMoveTo actionWithDuration:3.0 position:CGPointMake(winSize.width, sprite.position.y)];
[sprite runAction:moveToRight];
}
-(id) init
{
if( (self=[super init])) {
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"KnightImages2.plist"];
sprite = [CCSprite spriteWithSpriteFrameName:#"KnightSpritesRight6.png"];
sprite.position = ccp(240, 180);
[self addChild:sprite];
id actionSequence = [CCSequence actions:[CCCallFunc actionWithTarget:self selector:#selector(moveRight)],[CCCallFunc actionWithTarget:self selector:#selector(moveLeft)], nil];
CCRepeatForever *repeatForever = [CCRepeatForever actionWithAction:actionSequence];
[sprite runAction:repeatForever];
}
return self;
}
The way you've set this up is that the following happens, frame by frame:
Frame 1
execute moveRight
execute moveLeft
Frame 2
execute moveRight
execute moveLeft
Ad infinitum.
Since you're starting the animation all over every time you call moveRight or moveLeft, nothing really happens. You need to wait some time before running another animation for it to actually play. You can use the CCDelayTime action for this:
id actionSequence = [CCSequence actions:
[CCCallFunc actionWithTarget:self selector:#selector(moveRight)],
[CCDelayTime actionWithDuration:2],
[CCCallFunc actionWithTarget:self selector:#selector(moveLeft)],
[CCDelayTime actionWithDuration:2],
nil];

cocos2d problem - layer not updating quick enough

i am adding overlays to a cocos2d layer in preparation for a screenshot...
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCScene* currentScene = [[CCDirector sharedDirector] runningScene];
GameLayer* topLayer = (GameLayer *)[currentScene.children objectAtIndex:0];
CCSprite *middleBackground = [CCSprite spriteWithFile:#"mid_bg.png"];
middleBackground.position = ccp(winSize.width * 0.5,winSize.height * 0.55);
[topLayer addChild:middleBackground z:3 tag:111];
CCSprite *bottomBackground = [CCSprite spriteWithFile:#"bot_bg.png"];
bottomBackground.position = ccp(winSize.width * 0.5,winSize.height * 0.1);
[topLayer addChild:bottomBackground z:3 tag:112];
CCSprite *topBackground = [CCSprite spriteWithFile:#"top_bg.png"];
topBackground.position = ccp(winSize.width * 0.5,winSize.height * 0.94);
[topLayer addChild:topBackground z:3 tag:113];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
CCSprite *topBackground2 = [CCSprite spriteWithFile:#"statusCover2.png"];
topBackground2.position = ccp(winSize.width * 0.5,winSize.height * 0.992);
[topLayer addChild:topBackground2 z:3 tag:114];
[[topLayer popoverController] dismissPopoverAnimated:YES];
then
[UIImage imageWithCGImage:UIGetScreenImage()];
However the later is not taking the overlays into account. It is taking a screenshot of the layer before the overlays are added.
Is there anyway I can tell the cocos2d scene/layer to update the frame NOW?
Because cocos2d is asynchronous you can't tell it to do something like that. However, you can schedule a callback to happen in two seconds and have that callback do the screen capture.
[self schedule: #selector(screenshot:) interval:2.0];
then define the selector like this:
-(void) screenshot:(id)sender {
[UIImage imageWithCGImage:UIGetScreenImage()];
[self unschedule:#selector(screenshot:)];
}
don't forgot to unschedule it.

Animation in Cocos2d.

I am trying to make a sprite animate in cocos2D. I believe I have the animation set up, but how do I draw the animating sprite onto the screen? Here is what I have:
id anim = [[[CCAnimation alloc] initWithName:#"char_walking" delay:1/12.0] autorelease];
[anim addFrame:#"run2.png"];
[anim addFrame:#"run1.png"];
[anim addFrame:#"run3.png"];
[anim addFrame:#"run4.png"];
[anim addFrame:#"run3.png"];
[anim addFrame:#"run1.png"];
id myAction = [CCAnimate actionWithAnimation:anim];
id repeating = [CCRepeatForever actionWithAction:myAction];
[character do:repeating];
character = [CCSprite spriteWithSpriteFrame:anim];
character.position = ccp(160, 240);
[self addChild:character];
Thanks in advance,
John
Maybe it was just a cut-and-paste error, but it looks like you're telling the sprite to repeat the animation BEFORE you create it, so the character sprite you are adding to the node never gets the CCAnimate action sent to it.
You're not adding spriteFrames as the addFrame method requires.
with this line:
[character do:repeating];
maybe you're looking for [character runAction:repeating];
character = [CCSprite
spriteWithSpriteFrame:anim];
Here, anim is not a spriteFrame, it's a CCanimation.
basically, you have a few problems.
you could try something like this using zwoptex to create your .plist file:
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:#"runImages.plist"];
CCSprite *startingImage = [CCSprite spriteWithSpriteFrameName:#"run1.png"];
[self addChild:startingImage];
//create your sprite frames
NSArray *animFrames = [[NSArray alloc] initWithCapacity:6];
[animFrames addFrame:[cache spriteFrameByName:#"run2.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run1.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run3.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run4.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run3.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run1.png"]];
//run the animation
CCAnimation *animation = [CCAnimation animationWithName:#"char_walking" delay:1/12.0 frames:animFrames];
id anim = [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];
[startingImage runAction:anim];