1
votes

So in my cocos2d-iphone game I have an action layer - where all the magic happens, and then I have classes for my enemy sprites and my main protagonist sprite.

In my action layer, I add the sprite frames on init, but this feels like it should be encapsulated into the class they belong to instead of the layer they are in, but I don't want to add them more than once.

How can I make it so that the frames are only added to the spriteFrameCache when the class is used? If I put the following code in my init method for the alien sprites, will it bog down my game?

    // Add Alien Hominid to sprite cache
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AlienHominid-ipadhd.plist"];
    alienSheet = [CCSpriteBatchNode batchNodeWithFile:@"AlienHominid-ipadhd.png"];
    [self addChild:alienSheet];
    alienFrames = [NSMutableArray array];
    for (int i=1; i <= 5; i++) {
        [alienFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"AlienHominid %d.png", i]]];
        if (i==5)
            [alienFrames addObject:
             [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"AlienHominid 1.png"]];
    }
    alienAnim = [CCAnimation animationWithSpriteFrames:alienFrames delay:0.1];
    flip = [[CCRepeatForever alloc ] initWithAction:[CCAnimate actionWithAnimation:alienAnim ]];

Right now this feels like semantic suicide, is there any way to encapsulate this behavior?

1

1 Answers

1
votes

You definitely should subclass CCSprite to do that.

lets call it an alienHominid class, in your header file the class should probably look like this:

@interface alienHominid : CCSprite {
    //helps you find which current frame is being displayed at any moment
    int currentFrame;
    CCAnimation *flip;
}

//Extra some extra params you might need ?
@property (readwrite) NSNumber *health;

//And your new init method
-(id)initWithSpriteSheet:(NSString*)aSpriteFrame andBatchNode:(NSString*)aBatchNode;

//Some other methods to animate and give it your required behaviours
-(void)startAnimations;
-(void)prepareAnimations;
-(void)stopAnimations;
-(void)removeFromLayer;
@end

Now let's take a look at your implementation file

@implementation alienHominid

//Synth. properties you created here , if any.
@synthesize health  = _health;

-(id)initWithSpriteSheet:(NSString*)aSpriteFrame andBatchNode:(NSString*)aBatchNode
{

    self = [super init];
    if(self)
    {
        //custom init code
        [self setHealth:[NSNumber numberWithInt:100]];
        //
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:aSpriteFrame];
        alienSheet = [CCSpriteBatchNode batchNodeWithFile:aBatchNode];
    }

    return self;
}

//Some other methods to animate and give it your required behaviours

-(void)prepareAnimations{
    alienFrames = [NSMutableArray array];
    for (int i=1; i <= 5; i++) {
        [alienFrames addObject:
        [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"AlienHominid %d.png", i]]];
        if (i==5)
            [alienFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"AlienHominid 1.png"]];
    }
    CCAnimation *alienAnim = [CCAnimation animationWithSpriteFrames:alienFrames delay:0.1];
    flip = [[CCRepeatForever alloc ] initWithAction:[CCAnimate actionWithAnimation:alienAnim]];
}
-(void)startAnimations{
    [flip start];
}
-(void)stopAnimations{
    //Stop animations here
    [flip stop];
}
-(void)removeFromLayer{
    //If alien dies, remove it here
}
@end

Now You can easily use it in any Scene by importing it's header

 #import "alienHominid.h"

and add it like this:

alienHominid *alien = [[alienHominid alloc] initWithSpriteSheet:@"AlienHominid-ipadhd.plist" andBatchNode:@"AlienHominid-ipadhd.png"];
[alien prepareAnimations];
[alien startAnimations];
[self addChild:alien];

Note This code isn't tested, just writing what comes to my mind into the browser, so expect it not to work, but it should give you a simple idea of what I'm trying to communicate :), treat it as pseudocode even if it works ;)

Cheers !