@@ -82,15 +82,19 @@ -(id) init
82
82
-(id )initWithSprite : (CCSprite*) sprite
83
83
{
84
84
if (( self = [super init ] )){
85
+ _type = CCProgressNodeTypeRadial;
86
+ _reverseDirection = NO ;
85
87
_percentage = 0 .f ;
86
88
_vertexData = NULL ;
87
89
_vertexDataCount = 0 ;
90
+
88
91
self.anchorPoint = ccp (0 .5f ,0 .5f );
89
- self.type = CCProgressNodeTypeRadial;
90
- self.reverseDirection = NO ;
91
- self.midpoint = ccp (.5f , .5f );
92
- self.barChangeRate = ccp (1 ,1 );
92
+ self.midpoint = ccp (0 .5f , 0 .5f );
93
+ self.barChangeRate = ccp (1 , 1 );
93
94
self.sprite = sprite;
95
+
96
+ _dirtyVertexData = NO ;
97
+ _needsUpdateProgress = YES ;
94
98
95
99
// shader program
96
100
self.shaderProgram = [[CCShaderCache sharedShaderCache ] programForKey: kCCShader_PositionTextureColor ];
@@ -105,11 +109,14 @@ -(void)dealloc
105
109
}
106
110
}
107
111
108
- -(void )setPercentage : (float ) percentage
112
+ -(void )setPercentage : (float )percentage
109
113
{
110
114
if (_percentage != percentage) {
111
- _percentage = clampf ( percentage, 0 , 100 );
112
- [self updateProgress ];
115
+ _percentage = clampf (percentage, 0 , 100 );
116
+
117
+ // only flag update progress here, let the progress type handle
118
+ // whether it needs to rebuild the vertex data
119
+ _needsUpdateProgress = YES ;
113
120
}
114
121
}
115
122
@@ -119,27 +126,18 @@ -(void)setSprite:(CCSprite *)newSprite
119
126
_sprite = newSprite;
120
127
self.contentSize = _sprite.contentSize ;
121
128
122
- // Everytime we set a new sprite, we free the current vertex data
123
- if (_vertexData){
124
- free (_vertexData);
125
- _vertexData = NULL ;
126
- _vertexDataCount = 0 ;
127
- }
129
+ _dirtyVertexData = YES ;
130
+ _needsUpdateProgress = YES ;
128
131
}
129
132
}
130
133
131
134
-(void )setType : (CCProgressNodeType)newType
132
135
{
133
136
if (newType != _type) {
134
-
135
- // release all previous information
136
- if (_vertexData){
137
- free (_vertexData);
138
- _vertexData = NULL ;
139
- _vertexDataCount = 0 ;
140
- }
141
137
_type = newType;
142
- [self updateProgress ];
138
+
139
+ _dirtyVertexData = YES ;
140
+ _needsUpdateProgress = YES ;
143
141
}
144
142
}
145
143
@@ -148,12 +146,8 @@ -(void)setReverseDirection:(BOOL)reverse
148
146
if ( _reverseDirection != reverse ) {
149
147
_reverseDirection = reverse;
150
148
151
- // release all previous information
152
- if (_vertexData){
153
- free (_vertexData);
154
- _vertexData = NULL ;
155
- _vertexDataCount = 0 ;
156
- }
149
+ _dirtyVertexData = YES ;
150
+ _needsUpdateProgress = YES ;
157
151
}
158
152
}
159
153
@@ -225,15 +219,25 @@ -(void)updateColor
225
219
226
220
-(void )updateProgress
227
221
{
222
+ if (_dirtyVertexData){
223
+ // remove the vertex data if the type, direction, or sprite have changed
224
+ if (_vertexData) {
225
+ free (_vertexData);
226
+ _vertexData = NULL ;
227
+ _vertexDataCount = 0 ;
228
+ }
229
+ _dirtyVertexData = NO ;
230
+ }
231
+
228
232
switch (_type) {
229
233
case CCProgressNodeTypeRadial:
230
234
[self updateRadial ];
231
- break ;
235
+ return ;
232
236
case CCProgressNodeTypeBar:
233
237
[self updateBar ];
234
- break ;
238
+ return ;
235
239
default :
236
- break ;
240
+ return ;
237
241
}
238
242
}
239
243
@@ -503,7 +507,12 @@ -(CGPoint)boundaryTexCoord:(char)index
503
507
504
508
-(void ) draw
505
509
{
506
- if ( ! _vertexData || ! _sprite)
510
+ if (_needsUpdateProgress) {
511
+ [self updateProgress ];
512
+ _needsUpdateProgress = NO ;
513
+ }
514
+
515
+ if (!_vertexData || !_sprite)
507
516
return ;
508
517
509
518
CC_NODE_DRAW_SETUP ();
0 commit comments