KsK
KsK

Reputation: 675

sprite sheet not working for retina display

I have create a sprite sheet for non retina display and its working fine on simulator.. I have used the code

 -(Void)addSprites{
     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"image.plist"];

    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"image.png"];
    [self addChild:spriteSheet];

 // Load up the frames of our animation
    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i < 5; i++) {
        [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"image%d.png", i]]];
    }

    CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.20f];
// Create a sprite for our bear

    background = [CCSprite spriteWithSpriteFrameName:@"image1.png"];
    background.position = ccp(280, 175);
    self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim]];
    [spriteSheet addChild:background];  
}
 -(void)startAnimation{
    [background runAction:_walkAction];

}

And for for device i have created sprite sheet with retina image with double size and image named like image@2x.. the created plist is myplist@2xplist and image file is [email protected]

I mean there are 4 files

for non retina display.

1) imagefile.png(sprite sheet)

2) myPlist.plist

for Retina display.

1) [email protected](sprite sheet) the plist key name for every image is also like [email protected]

2) [email protected]

but the above code is not working for this code. m i doing something wrong or missing somthing? my app crash on device with error message

 CCSpriteFrameCache: Frame 'image1.png' not found
2013-05-03 16:19:49.764  *** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object    cannot be nil'

but the above code is working fine on simulator

Upvotes: 0

Views: 323

Answers (2)

Sahil Arora
Sahil Arora

Reputation: 213

Follow the given steps -

  1. Create two different spritesheets for both retina and norma resolution.

  2. Suppose you have four images image1.png,image2.png,image3.png,image4.png . Firstly make sure they have size according to retina display. Then create spritesheet and plist using these images. Save them as [email protected] and [email protected]

  3. Then get the same four images reduce their size to half. Make sure their name remains same. Create sheet using zwoptex with name animation.png and animation.plist.

Now you have two different versions of spritesheets and Plist for retina and normal. Load them using following code :

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"animation.plist"];

CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"animation.png"];
[self addChild:spriteSheet];

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"[email protected]"];

CCSpriteBatchNode *spriteSheethd = [CCSpriteBatchNode batchNodeWithFile:@"[email protected]"];
[self addChild:spriteSheethd];

Now use them . They will display fine

Upvotes: 0

Hamdullah shah
Hamdullah shah

Reputation: 804

By default cocos2d use -hd postfix not "@2x". And file names inside the sprite sheet need to be same without any "-hd" or @2x. Just the main sprite sheet file name need to be with postfix "-hd".

myPlist-hd.plist

Upvotes: 2

Related Questions