sprite sheet not working for retina display

痴心易碎 提交于 2019-12-25 04:12:27

问题


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 imagefile@2x.png

I mean there are 4 files

for non retina display.

1) imagefile.png(sprite sheet)

2) myPlist.plist

for Retina display.

1) imagefile@2x.png(sprite sheet) the plist key name for every image is also like image@2x.png

2) myPlist@2x.plist

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


回答1:


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




回答2:


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 animation@2x.png and animation@2x.plist

  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:@"animation@2x.plist"];

CCSpriteBatchNode *spriteSheethd = [CCSpriteBatchNode batchNodeWithFile:@"animation@2x.png"];
[self addChild:spriteSheethd];

Now use them . They will display fine



来源:https://stackoverflow.com/questions/16357400/sprite-sheet-not-working-for-retina-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!