Skip to content

Commit 8e19335

Browse files
author
Thayer J Andrews
committed
CCEffectStack - Match convenience constructor flavors to those in CCActionSequence
There is now an NSArray* version and a nil-terminated list version. This also introduces a convenience va_list initializer but that's internal only.
1 parent 0ac3d9d commit 8e19335

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

cocos2d-ui-tests/tests/CCEffectsTest.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ -(void)setupStackTest
527527
sprite.position = ccp(0.5f, 0.5f);
528528
sprite.scale = 0.5f;
529529

530-
sprite.effect = [CCEffectStack effectStackWithEffects:@[effects[8], effects[9]]];
531-
// sprite.effect = [CCEffectStack effectStackWithEffects:@[effects[7]]];
530+
sprite.effect = [CCEffectStack effects:effects[8], effects[9], nil];
532531

533532
sprite.normalMapSpriteFrame = [CCSpriteFrame frameWithImageNamed:@"Images/ShinyBallNormals.png"];
534533
sprite.colorRGBA = [CCColor colorWithRed:0.75f green:0.75f blue:0.75f alpha:0.75f];
@@ -662,7 +661,7 @@ - (CCNode *)effectNodeWithEffects:(NSArray *)effects appliedToSpriteWithImage:(N
662661
}
663662
else if (effects.count > 1)
664663
{
665-
CCEffectStack *stack = [CCEffectStack effectStackWithEffects:effects];
664+
CCEffectStack *stack = [CCEffectStack effectWithArray:effects];
666665
effectNode.effect = stack;
667666
}
668667

@@ -683,7 +682,7 @@ - (CCSprite *)spriteWithEffects:(NSArray *)effects image:(NSString *)spriteImage
683682
}
684683
else if (effects.count > 1)
685684
{
686-
CCEffectStack *stack = [CCEffectStack effectStackWithEffects:effects];
685+
CCEffectStack *stack = [CCEffectStack effectWithArray:effects];
687686
sprite.effect = stack;
688687
}
689688

cocos2d/CCEffectStack.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@
4040
/**
4141
* Initializes an effect stack object with the specified array of effects.
4242
*
43-
* @param effects The array of effects to add to the stack.
43+
* @param arrayOfEffects The array of effects to add to the stack.
4444
*
4545
* @return The CCEffectStack object.
4646
*/
47-
- (id)initWithEffects:(NSArray *)effects;
47+
- (id)initWithArray:(NSArray *)arrayOfEffects;
48+
49+
/**
50+
* Initializes an effect stack object with the specified effects.
51+
*
52+
* @param effect1 First effect to add to the stack.
53+
* @param ... Nil terminated list of effects to stack.
54+
*
55+
* @return The CCEffectStack object.
56+
*/
57+
- (id)initWithEffects:(CCEffect*)effect1, ... NS_REQUIRES_NIL_TERMINATION;
4858

4959

5060
/// -----------------------------------------------------------------------
@@ -54,11 +64,21 @@
5464
/**
5565
* Creates an effect stack object with the specified array of effects.
5666
*
57-
* @param effects The array of effects to add to the stack.
67+
* @param arrayOfEffects The array of effects to add to the stack.
68+
*
69+
* @return The CCEffectStack object.
70+
*/
71+
+ (id)effectWithArray:(NSArray*)arrayOfEffects;
72+
73+
/**
74+
* Creates an effect stack object with the specified effects.
75+
*
76+
* @param effect1 First effect to add to the stack.
77+
* @param ... Nil terminated list of effects to stack.
5878
*
5979
* @return The CCEffectStack object.
6080
*/
61-
+ (id)effectStackWithEffects:(NSArray *)effects;
81+
+ (id)effects:(CCEffect*)effect1, ... NS_REQUIRES_NIL_TERMINATION;
6282

6383

6484
/// -----------------------------------------------------------------------

cocos2d/CCEffectStack.m

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ @implementation CCEffectStack
1717

1818
- (id)init
1919
{
20-
return [self initWithEffects:nil];
20+
return [self initWithArray:nil];
2121
}
2222

23-
- (id)initWithEffects:(NSArray *)effects
23+
- (id)initWithArray:(NSArray *)effects
2424
{
2525
if ((self = [super init]))
2626
{
@@ -46,9 +46,48 @@ - (id)initWithEffects:(NSArray *)effects
4646
return self;
4747
}
4848

49-
+ (id)effectStackWithEffects:(NSArray *)effects
49+
- (id)initWithEffect:(CCEffect*)effect1 vaList:(va_list)args
5050
{
51-
return [[self alloc] initWithEffects:effects];
51+
NSMutableArray *effects = [[NSMutableArray alloc] init];
52+
53+
CCEffect *effect = effect1;
54+
while(effect)
55+
{
56+
[effects addObject:effect];
57+
effect = va_arg(args, CCEffect*);
58+
}
59+
60+
return [self initWithArray:effects];
61+
}
62+
63+
- (id)initWithEffects:(CCEffect*)effect1, ...
64+
{
65+
va_list args;
66+
va_start(args, effect1);
67+
68+
id ret = [self initWithEffect:effect1 vaList:args];
69+
70+
va_end(args);
71+
72+
return ret;
73+
}
74+
75+
76+
+ (id)effectWithArray:(NSArray *)arrayOfEffects
77+
{
78+
return [[self alloc] initWithArray:arrayOfEffects];
79+
}
80+
81+
+ (id)effects:(CCEffect*)effect1, ...
82+
{
83+
va_list args;
84+
va_start(args, effect1);
85+
86+
id ret = [[self alloc] initWithEffect:effect1 vaList:args];
87+
88+
va_end(args);
89+
90+
return ret;
5291
}
5392

5493
- (NSUInteger)effectCount
@@ -58,10 +97,18 @@ - (NSUInteger)effectCount
5897

5998
- (CCEffect *)effectAtIndex:(NSUInteger)effectIndex
6099
{
61-
NSAssert(effectIndex < _effects.count,@"Pass index out of range.");
100+
NSAssert(effectIndex < _effects.count,@"Effect index out of range.");
62101
return _effects[effectIndex];
63102
}
64103

104+
- (void)dealloc
105+
{
106+
for (CCEffect *effect in _effects)
107+
{
108+
effect.owningStack = nil;
109+
}
110+
}
111+
65112
#pragma mark - CCEffect overrides
66113

67114
- (CCEffectPrepareStatus)prepareForRendering

0 commit comments

Comments
 (0)