Create Sprite Frame Cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"role.plist"];
Create Sprite Sheet
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"role.png"];
[self addChild:spriteSheet];
Create Animation Frames
NSMutableArray *danceAnimationFrames = [NSMutableArray array];
for(int i=1; i <= 14; i++) {
[danceAnimationFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"role%02d.png", i]]];
}
Create Animation
CCAnimation *danceAnimation = [CCAnimation animationWithSpriteFrames:danceAnimationFrames delay:0.1f];
Add Animation to Animation Cache
[[CCAnimationCache sharedAnimationCache] addAnimation:danceAnimation name:@"danceAnimation"];
Get Animation from Animation Cache
danceAnimation = [[CCAnimationCache sharedAnimationCache] animationByName:@"danceAnimation"];
Create Sprite
self.role = [mySprite spriteWithSpriteFrameName:@"role01.png"];
Create Action
self.danceAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:danceAnimation]];
Sample Code for AniEgg.h
#import "cocos2d.h"
#import "mySprite.h"
@interface AniEgg : CCLayer {
mySprite *_role;
CCAction *_danceAction;
}
+(CCScene *)scene;
@property (nonatomic, retain) mySprite *role;
@property (nonatomic, retain) CCAction *danceAction;
@end
Sample Code for AniEgg.m
#import "AniEgg.h"
BOOL paused;
int pausedCount;
@implementation AniEgg
@synthesize role = _role;
@synthesize danceAction = _danceAction;
+(CCScene *) scene {
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
AniEgg *layer = [AniEgg node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init {
if( (self=[super init]) ) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"role.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"role.png"];
[self addChild:spriteSheet];
NSMutableArray *danceAnimationFrames = [NSMutableArray array];
for(int i=1; i <= 14; i++) {
[danceAnimationFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"role%02d.png", i]]];
}
CCAnimation *danceAnimation = [CCAnimation animationWithSpriteFrames:danceAnimationFrames delay:0.1f];
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.role = [mySprite spriteWithSpriteFrameName:@"role01.png"];
_role.position = ccp(winSize.width / 2, winSize.height / 2);
self.danceAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:danceAnimation]];
[spriteSheet addChild:_role];
[_role runAction:_danceAction];
paused = NO;
[self schedule:@selector(nextFrame:) interval:0.1f];
self.isTouchEnabled = YES;
}
return self;
}
- (void) nextFrame:(ccTime)dt {
if ([_role isTouched]) {
if (paused) {
paused = NO;
[[[CCDirector sharedDirector] actionManager] resumeTarget:_role];
pausedCount = 0;
} else {
paused = YES;
[[[CCDirector sharedDirector] actionManager] pauseTarget:_role];
pausedCount = 0;
}
[_role setUnTouched];
}
if (paused) {
pausedCount++;
if (pausedCount >= 20) {
paused = NO;
[[[CCDirector sharedDirector] actionManager] resumeTarget:_role];
pausedCount = 0;
}
}
}
- (void) dealloc
{
self.role = nil;
self.danceAction = nil;
}
@end
沒有留言:
張貼留言