0

in the project i'm working on I have multiple characters you can play as (in this case I will refer to them as drones). In my gamescene I have all the physics setup through _drone. A .CCB file created with spritebuilder. Previously I had this file animated through spritebuilder, but now with the addition of multiple drones to choose from I need to set it to display and cycle through the appropriate frames. I've been looking all day for things related to this and most answers I see are for cocos2d v2.0 or when I get something that has no errors showing in Xcode, it doesn't apply it to my _drone class. What I'm looking to do is something like this: [_drone setSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"DefectiveDroneSpriteSheet/Drone1.png"]]; As this sets the frame for _drone to what I want. However I don't know how to make this cycle between the frames.

I found an answer on here recently that showed how to do it:

NSMutableArray *animationFrames = [NSMutableArray array];

for(int i = 1; i <= FRAMES; ++i)
{
    CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"animationFrame%d.png", i]]; //
}

//Create an animation from the set of frames you created earlier
CCAnimation *animation = [CCAnimation animationWithSpriteFrames: animationFrames delay:delay];

//Create an action with the animation that can then be assigned to a sprite
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:animation];

CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
[self runAction:repeatingAnimation];

Unfortunately this didn't work for me, but maybe I did something wrong. I seemed to have the most issue with the for loop/ a warning saying variable spriteFrame is never used, or something like that. The problem with this code is that after hours of messing with it and trying to find updated documentation I couldn't figure out how to do what my first example did, apply it directly to _drone. So with all that said... what should I do about solving this? Is there another easier way that i'm overlooking?

Thanks for your time, much appreciated!

edit for Guru:

Hello, thanks for responding. This is what my for loop looks like currently that is throwing me 4 warnings.

for(int i = 1; i <= 2; ++i)
{
    CCSpriteFrame *frame1 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"DefectiveDroneSpriteSheet/Drone1.png", i]]; //
    CCSpriteFrame *frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"DefectiveDroneSpriteSheet/Drone1.png", i]]; //
}

I get a warning "unused variable for frame 1 and frame 2" and "data argument not used by format string" for the 'i'. Also doing this, how do I make this animation apply to my _drone object? As the object is already placed in the scene through Spritebuilder?


Second Edit:

- (void)didLoadFromCCB {

    NSMutableArray *animationFrames = [NSMutableArray array];

    for(int i = 1; i <= 2; ++i)
    {
        CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"DroneSpriteSheets%d.png", i]]; //
        [animationFrames addObject: spriteFrame];
    }

    //Create an animation from the set of frames you created earlier
    CCAnimation *animation = [CCAnimation animationWithSpriteFrames: animationFrames delay:1.0f];

    //Create an action with the animation that can then be assigned to a sprite
    CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:animation];

    CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
    [_drone runAction:repeatingAnimation];

    //-----------------------------------------------------------

Thats the relevant part of my code, i get this error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

James Webster
  • 31,873
  • 11
  • 70
  • 114
BasedRebel
  • 11
  • 5

1 Answers1

0

You are not adding frames in array:

for(int i = 1; i <= FRAMES; ++i)
{
    CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"animationFrame%d.png", i]]; //
    [animationFrames addObject: spriteFrame];

}
Guru
  • 21,652
  • 10
  • 63
  • 102
  • Two mistakes, one wrong sprite frame and not added that to array. Check your spritesheet for proper sprite names – Guru Feb 21 '14 at 09:15
  • Not sure why I missed that, I fixed that and for my spritesheet name it is actually called "DroneSpriteSheets.png" I checked it in Xcode, the two frames are Drone1 and Drone2 .png. I have no errors in xcode, but In the second edit you can see both my full animation code, aswell as the error that gets thrown. I'm unsure what could be causing it. – BasedRebel Feb 21 '14 at 20:11