IronMike was asking me about this in a private message, but I thought it should be out here on the open thread where others can comment if they like. He asked:
I need help understanding what this does:
[ImageSwapAction actionWithCard: self],
in:
[self runAction: [CCSequence actions:
firstAction,
[ImageSwapAction actionWithCard: self],
secondAction,
nil]];
What is ImageSwapAction?
I have many nested CCSequences that all work great but I need to change the image of my sprite in the middle of the Sequence. I appreciate your help on the forum but I needed more explanation of this part.
Thanks for your help.
To which I replied:
You have to create your own subclass of CCAction and then bury it in the middle of your sequence. Here's my card flip action - all it really does is change the rectangle for the sprite in the update method, updating the image (I think I went to a sprite sheet after posting in that thread):
// ImageSwapAction.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@class Card;
@interface ImageSwapAction : CCFiniteTimeAction
{
Card* card;
}
+ (id) actionWithCard: (Card*) whichCard;
- (id) initWithCard: (Card*) whichCard;
@property (nonatomic, retain) Card* card;
@end
// ImageSwapAction.mm
#import "ImageSwapAction.h"
#import "Card.h"
@implementation ImageSwapAction
@synthesize card;
+ (id) actionWithCard: (Card*) whichCard
{
return [[[self alloc] initWithCard: whichCard] autorelease];
}
- (float) duration
{
return 0;
}
- (id) initWithCard: (Card*) whichCard
{
if( (self=[super init]) )
{
card = whichCard;
return self;
}
return nil;
}
-(void) update: (ccTime) dt
{
if (card.isFaceUp == YES)
{
card.textureRect = card.faceRect;
}
else
{
card.textureRect = card.backRect;
}
}
-(id) copyWithZone: (NSZone*) zone
{
return [[[self class] allocWithZone: zone] initWithCard: card];
}
@end
And here's a sequence call that invokes it:
[self.card runAction: [CCSequence actions:
[CCOrbitCamera actionWithDuration:d/2 radius:1 deltaRadius:0 angleZ:0 deltaAngleZ:90 angleX:0 deltaAngleX:0],
[ImageSwapAction actionWithCard: self.card],
[CCOrbitCamera actionWithDuration:d/2 radius:1 deltaRadius:0 angleZ:270 deltaAngleZ:90 angleX:0 deltaAngleX:0],
nil]];
Then he replied:
I was thinking about using a sprite sheet.
Which would you recommend at this point?
So in response to that question, I would say "it depends".
Depends on how many sprites you have, how much animation you're doing, etc. Using Zwoptex I've found it's pretty bone simple to use sprite sheets instead of individual sprites.