-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnimatedSprite.m
More file actions
executable file
·161 lines (141 loc) · 5.61 KB
/
AnimatedSprite.m
File metadata and controls
executable file
·161 lines (141 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// CCAnimatedSprite.m
//
// Created by Benoit Freslon on 23/02/12.
// Copyright 2013 Benoit Freslon. All rights reserved.
// http://www.benoitfreslon.com
//
#import "AnimatedSprite.h"
@implementation AnimatedSprite
@synthesize currentAnimation = _currentAnimation;
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect rotated:(BOOL)rotated {
//CCLOG(@"*** AnimatedSprite: initWithTexture");
if ((self = [super initWithTexture:texture rect:rect rotated:rotated]) ) {
self.currentAnimation = @"";
animations = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)addLoopingAnimation:(NSString *)pName frame:(NSString *)pFrame delay:(float)pDelay {
//CCLOG(@"**** AnimatedSprite: addLoopingAnimation: %@", pName);
NSMutableArray *animFrames = [NSMutableArray array];
for(int i=0;;i++) {
NSString *str = [NSString stringWithFormat:pFrame, i];
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:str];
if(frame == nil) {
//CCLOGWARN(@"*** AnimatedSprite No frames found: %@", str);
break;
} else {
[animFrames addObject:frame];
}
}
if ([animFrames count] == 0) {
//CCLOGWARN(@"*** AnimatedSprite: ERROR NO IMAGES IN FRAMES %@",self);
return;
}
id animation = [CCAnimation animationWithSpriteFrames:animFrames delay:pDelay];
id animationLoop = [CCAnimate actionWithAnimation:animation];
CCRepeatForever *repeatAnimation = [CCRepeatForever actionWithAction:animationLoop];
[animations setValue:repeatAnimation forKey:pName];
}
- (void)addAnimation:(NSString *)pName frame:(NSString *)pFrame delay:(float)pDelay {
[self addAnimation:pName frame:pFrame delay:pDelay target:nil callback:nil];
}
- (void)addAnimation:(NSString *)pName frame:(NSString *)pFrame delay:(float)pDelay target:(id)pTarget callback:(SEL)pCallBack {
//CCLOG(@"*** AnimatedSprite: addAnimation: %@", pName);
NSMutableArray *animFrames = [NSMutableArray array];
for(int i=0;;i++) {
NSString *str = [NSString stringWithFormat:pFrame, i];
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:str];
if(frame == nil) {
//CCLOGWARN(@"*** AnimatedSprite: No frames found: %@", str);
break;
} else {
[animFrames addObject:frame];
}
}
if ([animFrames count] == 0) {
//CCLOGWARN(@"*** AnimatedSprite: ERROR NO IMAGE IN FRAMES: %@ %@",pFrame, self );
return;
}
CCAnimation *anim = [CCAnimation animationWithSpriteFrames:animFrames delay:pDelay];
CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCCallFunc *callback = [CCCallFunc actionWithTarget:pTarget selector:pCallBack];
if (pTarget == nil && pCallBack == nil) {
callback = [CCCallFunc actionWithTarget:self selector:@selector(dummyMethod)];
}
CCSequence *seq = [CCSequence actions:animate, callback , nil];
[animations setValue:seq forKey:pName];
}
- (void) dummyMethod {
}
- (void)startAnimation:(NSString *)pName {
//CCLOG(@"*** AnimatedSprite: startAnimation %@ %i %@",pName, self.tag, currentAnimation);
/*
if (![pName isEqualToString:currentAnimation]) {
[self stopCurrentAnimation];
}
*/
[self resumeCurrentAnimation];
[self stopCurrentAnimation];
self.currentAnimation = pName;
currentAnimAction = (CCAction *)[animations objectForKey:pName];
//CCLOG(@"currentaciton: %@", currentAnimAction);
if (currentAnimAction == nil) {
CCLOGWARN(@"*** AnimatedSprite: ERROR CAN'T FIND ANIMATION: %@ %@", pName, self);
return;
}
[self runAction:currentAnimAction];
}
- (void)startAnimation:(NSString *)pName restart:(BOOL)pRestart {
//CCLOG(@"*** AnimatedSprite: startAnimation %@", pName);
if ([self.currentAnimation isEqualToString:pName] && pRestart == YES) {
[self startAnimation:pName];
} else if (![self.currentAnimation isEqualToString:pName]) {
[self startAnimation:pName];
}
[self resumeCurrentAnimation];
}
- (void)stopCurrentAnimation {
//CCLOG(@"AnimatedSprite: stopCurrentAnimation %i, %@", self.tag, self);
if (currentAnimAction != nil) {
if ([currentAnimAction isDone] == NO) {
[self stopAction:currentAnimAction];
[currentAnimAction stop];
}
self.currentAnimation = @"";
}
}
- (void)stopAnimation:(NSString *)pName {
//CCLOG(@"*** AnimatedSprite: stopAnimation %@ %i %@",pName, self.tag, self);
if ([pName isEqualToString:self.currentAnimation]) {
if ([currentAnimAction isDone] == NO) {
[self stopAction:currentAnimAction];
}
self.currentAnimation = @"";
}
}
- (void)pauseCurrentAnimation {
//CCLOG(@"AnimatedSprite: pauseCurrentAnimation %@", currentAnimAction);
[self pauseSchedulerAndActions];
[[[CCDirector sharedDirector] actionManager] pauseTarget:self];
}
- (void)resumeCurrentAnimation {
[self resumeSchedulerAndActions];
[[[CCDirector sharedDirector] actionManager] resumeTarget:self];
}
- (void)dealloc {
//CCLOG(@"AnimatedSprite: dealloc %@",self);
[self stopCurrentAnimation];
[self stopAllActions];
[self unscheduleAllSelectors];
for (NSString* key in [animations allKeys]) {
CCAction *action = [animations objectForKey:key];
//CCLOG(@"action %@", action);
[self stopAction:action];
[action stop];
}
[animations removeAllObjects];
//[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
}
@end