-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCCAnimationManager+FrameAnimation.m
162 lines (117 loc) · 5.32 KB
/
CCAnimationManager+FrameAnimation.m
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
162
//
// CCBAnimationManager+FrameAnimation.m
// cocos2d-ios
//
// Created by Martin Walsh on 14/04/2014.
//
//
#import "CCAnimationManager+FrameAnimation.h"
#import "CCAnimationManager.h"
@implementation CCAnimationManager (FrameAnimation)
- (void)animationWithSpriteFrames:animFrames delay:(float)delay name:(NSString*)name node:(CCNode*)node loop:(BOOL)loop{
float nextTime = 0.0f;
NSMutableArray *keyFrames = [[NSMutableArray alloc] init];
for(NSString* frame in animFrames) {
// Create Frame(s)
NSDictionary* frameDict = [NSDictionary dictionaryWithObjectsAndKeys:
frame, @"value",
[NSNumber numberWithFloat:nextTime], @"time",
nil];
[keyFrames addObject:frameDict];
nextTime+=delay;
}
// Return to first frame
NSDictionary* frameDict = [NSDictionary dictionaryWithObjectsAndKeys:
[animFrames firstObject], @"value",
[NSNumber numberWithFloat:(nextTime)], @"time",
nil];
[keyFrames addObject:frameDict];
// Add Animation Sequence
[self addKeyFramesForSequenceNamed:name propertyType:CCBSequencePropertyTypeSpriteFrame frameArray:keyFrames node:node loop:loop];
}
#pragma mark Legacy Animation Support
- (void)parseVersion1:(NSDictionary*)animations node:(CCNode*)node {
NSArray* animationNames = [animations allKeys];
NSMutableArray* animFrames = [[NSMutableArray alloc] init];
for( NSString *name in animationNames ) {
[animFrames removeAllObjects];
NSDictionary* animationDict = [animations objectForKey:name];
NSArray *frameNames = [animationDict objectForKey:@"frames"];
float delay = [[animationDict objectForKey:@"delay"] floatValue];
if ( frameNames == nil ) {
CCLOG(@"Animation '%@' found in dictionary without any frames - Skipping", name);
continue;
}
for( NSString *frameName in frameNames ) {
CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
if ( !spriteFrame ) {
CCLOG(@"Animation '%@' refers to frame '%@' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation - Skipping", name, frameName);
continue;
}
[animFrames addObject:frameName];
}
[self animationWithSpriteFrames:animFrames delay:delay name:name node:node loop:YES];
}
}
- (void)parseVersion2:(NSDictionary*)animations node:(CCNode*)node {
NSArray* animationNames = [animations allKeys];
NSMutableArray* animFrames = [[NSMutableArray alloc] init];
for( NSString *name in animationNames ) {
[animFrames removeAllObjects];
NSDictionary* animationDict = [animations objectForKey:name];
//int loops = [[animationDict objectForKey:@"loops"] intValue];
//BOOL restoreOriginalFrame = [[animationDict objectForKey:@"restoreOriginalFrame"] boolValue];
NSArray *frameArray = [animationDict objectForKey:@"frames"];
if ( frameArray == nil ) {
CCLOG(@"Animation '%@' found in dictionary without any frames - Skipping", name);
continue;
}
for( NSDictionary *entry in frameArray ) {
NSString *spriteFrameName = [entry objectForKey:@"spriteframe"];
CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:spriteFrameName];
if ( !spriteFrame ) {
CCLOG(@"Animation '%@' refers to frame '%@' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation - Skipping", name, spriteFrameName);
continue;
}
[animFrames addObject:spriteFrameName];
//float delayUnits = [[entry objectForKey:@"delayUnits"] floatValue];
//NSDictionary *userInfo = [entry objectForKey:@"notification"];
}
float delayPerUnit = [[animationDict objectForKey:@"delayPerUnit"] floatValue];
[self animationWithSpriteFrames:animFrames delay:delayPerUnit name:name node:node loop:YES];
}
}
- (void)addAnimationsWithDictionary:(NSDictionary *)dictionary node:(CCNode*)node {
NSDictionary *animations = [dictionary objectForKey:@"animations"];
if ( animations == nil ) {
CCLOG(@"No animations were found in dictionary.");
return;
}
NSUInteger version = 1;
NSDictionary *properties = [dictionary objectForKey:@"properties"];
if( properties ) {
version = [[properties objectForKey:@"format"] intValue];
}
NSArray *spritesheets = [properties objectForKey:@"spritesheets"];
// Ensure Sheets Loaded
for( NSString *name in spritesheets ) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:name];
}
switch (version) {
case 1:
[self parseVersion1:animations node:node];
break;
case 2:
[self parseVersion2:animations node:node];
break;
default:
NSAssert(NO, @"Invalid animation format.");
}
}
- (void)addAnimationsWithFile:(NSString *)plist node:(CCNode*)node {
NSString *path = [[CCFileUtils sharedFileUtils] fullPathForFilename:plist];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSAssert1( dict, @"Animation file could not be found: %@", plist);
[self addAnimationsWithDictionary:dict node:node];
}
@end