Skip to content

Instantly share code, notes, and snippets.

@bontoJR
Created November 17, 2011 11:52
Show Gist options
  • Save bontoJR/1372988 to your computer and use it in GitHub Desktop.
Save bontoJR/1372988 to your computer and use it in GitHub Desktop.
Realistic card turn with Cocos2d
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface MCCard : NSObject
@property (readwrite, getter = isFaceUp) BOOL faceUp;
@property (readwrite, assign) CCSprite *cardSprite;
@property (readwrite, assign) CCSprite *frontCardSprite;
@property (nonatomic, retain) NSString *cardname;
@property (nonatomic, retain) NSString *backImageName;
+ (id)cardWithParentNode:(CCNode*)parentNode;
- (id)initWithParentNode:(CCNode*)parentNode;
- (void)turnCard;
- (void)flipReveal;
- (void)flipHide;
@end
- (void)turnCard
{
if ([self isFaceUp]) {
[self flipHide];
self.faceUp = NO;
} else {
self.faceUp = YES;
[self flipReveal];
}
}
- (void)flipHide
{
id scale = [CCScaleTo actionWithDuration:0.25 scaleX:-1.2 scaleY:1.2];
CCEaseExponentialIn* flipHalf = [CCEaseExponentialIn actionWithAction:[CCActionTween actionWithDuration:0.25 key:@"scaleX" from:-1.2 to:0.0]];
CCCallFuncN* removeLetter = [CCCallFuncN actionWithTarget:self selector:@selector(hideFront:)];
CCEaseExponentialOut* flipRemainingHalf = [CCEaseExponentialOut actionWithAction:[CCActionTween actionWithDuration:0.25 key:@"scaleX" from:0.0 to:1.20]];
id reverseScale = [CCScaleTo actionWithDuration:0.25 scaleX:1.0 scaleY:1.0];; // something like this, don't have mac at hand
CCSequence* seq = [CCSequence actions:scale,flipHalf,removeLetter,flipRemainingHalf, reverseScale, nil];
[self.cardSprite runAction:seq];
}
- (void)flipReveal
{
id scale = [CCScaleTo actionWithDuration:0.25 scaleX:1.2 scaleY:1.2];
CCEaseExponentialIn* flipHalf = [CCEaseExponentialIn actionWithAction:[CCActionTween actionWithDuration:0.25 key:@"scaleX" from:1.2 to:0.0]];
CCCallFuncN* showLetter = [CCCallFuncN actionWithTarget:self selector:@selector(showFront:)];
CCEaseExponentialOut* flipRemainingHalf = [CCEaseExponentialOut actionWithAction:[CCActionTween actionWithDuration:0.25 key:@"scaleX" from:0.0 to:-1.2]];
id reverseScale = [CCScaleTo actionWithDuration:0.25 scaleX:-1.0 scaleY:1.0];; // something like this, don't have mac at hand
CCSequence* seq = [CCSequence actions:scale,flipHalf,showLetter,flipRemainingHalf, reverseScale, nil];
[self.cardSprite runAction:seq];
}
- (void)hideFront:(id)node
{
[frontCardSprite setVisible:NO];
}
- (void)showFront:(id)node
{
[frontCardSprite setVisible:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment