cocos2d can't add multiple sprites and animation

I am tring to follow this post post and combine it with the ShootThemUp example from LearnCocos2d. Unfortunately I am a bit confused as I got an issue with animating the parts of a subclass of CCSprite and an issue in accessing to the frame names inside the texture map.

ACCESSING SPRITE FRAME NAMES

I do get the following expection:

CCSpriteFrameCache: Trying to use file 'game-art.png' as texture

when trying to access the frame name as in the ShootEmUp example:

head = [CCSprite initWithSpriteFrameName:@"head.png"];

Here is the modified code:

@implementation ShipEntity

@synthesize lifes;

+(id) ship
{
    self = [[[self alloc] init] autorelease];
    [self addParts];
    return self;
}

-(void) addParts
{

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"game-art.plist"];



    body = [CCSprite node];
//      head = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"head.png"]];

    head = [CCSprite initWithSpriteFrameName:@"head.png"];

    [head setPosition:CGPointMake(10,10)];
    [body addChild:head];
    [self addChild:body];
/**
    // create an animation object from all the sprite animation frames
    CCAnimation* anim = [CCAnimation animationWithFrame:@"head-anim" frameCount:3 delay:0.08f];

    // add the animation to the sprite (optional)
    [head addAnimation:anim];

    // run the animation by using the CCAnimate action
    CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
    [head runAction:repeat];
    **/
    [self scheduleUpdate];

    //should use spawn method..
    initialHitPoints=2;
    hitPoints=initialHitPoints;
    lifes=3;
    [[GameScene sharedGameScene] setLifeCount:lifes];

}

If I do replace:

head = [CCSprite initWithSpriteFrameName:@"head.png]; 

with

head = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"head.png"]];

then it does add it. Why do I need to do this? In the ShootthemUp example it did work by accessing the sprite frame name directly.I think I need to find a way to make the plst file visible withing all the source code I am writing..

ANIMATION ISSUE Finally, I tried to add an animation by uncommenting the following bit of code (assume I do have the head-anim files in the game-art.png and game-art.plist files:

        CCAnimation* anim = [CCAnimation animationWithFrame:@"head-anim" frameCount:3 delay:0.08f];

    // add the animation to the sprite (optional)
    [head addAnimation:anim];

    // run the animation by using the CCAnimate action
    CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
    [head runAction:repeat];

But it did not work.. any suggestion?

This is the stacktrace of the exception:

012-02-28 12:15:14.068 ShootEmUp[26182:40b] cocos2d: CCSpriteFrameCache: Trying to use file 'game-art.png' as texture
2012-02-28 12:15:14.109 ShootEmUp[26182:40b] cocos2d: CCSpriteFrameCache: Trying to use file 'game-art.png' as texture
2012-02-28 12:15:14.109 ShootEmUp[26182:40b] +[CCSprite initWithSpriteFrameName:]: unrecognized selector sent to class 0x10cbf0
2012-02-28 12:15:14.111 ShootEmUp[26182:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[CCSprite initWithSpriteFrameName:]: unrecognized selector sent to class 0x10cbf0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013f0be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x015455c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013f27bb +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x01362366 ___forwarding___ + 966
    4   CoreFoundation                      0x01361f22 _CF_forwarding_prep_0 + 50
    5   ShootEmUp                           0x0000c7cd -[ShipEntity addParts] + 349
    6   ShootEmUp                           0x0000c64d +[ShipEntity ship] + 141
    7   ShootEmUp                           0x000038ae -[GameScene initWithId:] + 1102
    8   ShootEmUp                           0x00003413 +[GameScene sceneWithId:] + 131
    9   ShootEmUp                           0x00010e9f -[Navigator LaunchLevel:] + 143
    10  CoreFoundation                      0x0136167d __invoking___ + 29
    11  CoreFoundation                      0x01361551 -[NSInvocation invoke] + 145
    12  ShootEmUp                           0x0004be75 -[CCMenuItem activate] + 85
    13  ShootEmUp                           0x00012d86 -[SlidingMenuGrid ccTouchEnded:withEvent:] + 454
    14  ShootEmUp                           0x000a40b1 -[CCTouchDispatcher touches:withEvent:withTouchType:] + 1633
    15  ShootEmUp                           0x000a4bff -[CCTouchDispatcher touchesEnded:withEvent:] + 111
    16  ShootEmUp                           0x000a6ab1 -[EAGLView touchesEnded:withEvent:] + 113
    17  UIKit                               0x008a30d1 -[UIWindow _sendTouchesForEvent:] + 567
    18  UIKit                               0x0088437a -[UIApplication sendEvent:] + 447
    19  UIKit                               0x00889732 _UIApplicationHandleEvent + 7576
    20  GraphicsServices                    0x02998a36 PurpleEventCallback + 1550
    21  CoreFoundation                      0x013d2064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    22  CoreFoundation                      0x013326f7 __CFRunLoopDoSource1 + 215
    23  CoreFoundation                      0x0132f983 __CFRunLoopRun + 979
    24  CoreFoundation                      0x0132f240 CFRunLoopRunSpecific + 208
    25  CoreFoundation                      0x0132f161 CFRunLoopRunInMode + 97
    26  GraphicsServices                    0x02997268 GSEventRunModal + 217
    27  GraphicsServices                    0x0299732d GSEventRun + 115
    28  UIKit                               0x0088d42e UIApplicationMain + 1160
    29  ShootEmUp                           0x00002844 main + 100

It seems related to not finding the file but I can't understand why in the example it does find it and why in my code doesn't .. I know is a bit silly and there must be an obvious reason that I am missing. Is also the first time I try to generate a plst and png file and hence I might have done some errors..

[EDIT NOTE: Difference in settings between my game-art files -left image- and the ShootthemUp ones - right image]

Answers


If you have a frame name, use the autorelease initializers; the "init" method you are using is somewhat unconventional technique for what you are trying to do:

head = [CCSprite spriteWithSpriteFrameName:@"head.png];

If you do not use autoreleased initializers, you need to be careful and employ a lot of memory management techniques and other techniques that Cocos is designed to prevent you from doing.

You should stick with the auto released methods as much as possible unless you have a really good reason not to.


Thanks for the answer, I haven't accepted it as the reason for which I was having problem was relatead to this other question post that I asked.. I did not realize that I needed both -hd and not -hd file in the App which was causing the confusing (I had added only one sprite atlas file in the resource folder and whenver I was trying to access a frame within it the App was complaining as it was set to run on retina display and wasn't finding both resource files). I wouldn't have imagined that without the answer I received in the post 1.

Thanks for your help!


Need Your Help

Ionic framework PdfViewer

pdf ionic-framework

I want to develop pdf ebook application for mobile. Is there pdf viewer component for ionic framework. I fond mozilla pdf.js . I need ionic project example.

Mongodb update document through ruby not working

ruby mongodb

I want to update documents in MongoDB via Ruby code. I have document ids of documents I want to update only a specific field. I tried the following code.