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?