@@ -11,6 +11,7 @@ var RandomZone = require('./zones/RandomZone');
1111var Rectangle = require ( '../../geom/rectangle/Rectangle' ) ;
1212var StableSort = require ( '../../utils/array/StableSort' ) ;
1313var Vector2 = require ( '../../math/Vector2' ) ;
14+ var Wrap = require ( '../../math/Wrap' ) ;
1415
1516var ParticleEmitter = new Class ( {
1617
@@ -38,6 +39,14 @@ var ParticleEmitter = new Class({
3839
3940 this . defaultFrame = manager . defaultFrame ;
4041
42+ this . randomFrame = true ;
43+
44+ this . frameQuantity = 1 ;
45+
46+ this . currentFrame = 0 ;
47+
48+ this . _frameCounter = 0 ;
49+
4150 this . x = new EmitterOp ( config , 'x' , 0 ) ;
4251 this . y = new EmitterOp ( config , 'y' , 0 ) ;
4352
@@ -169,11 +178,11 @@ var ParticleEmitter = new Class({
169178 this . followOffset = new Vector2 ( GetFastValue ( config , 'followOffset' , 0 ) ) ;
170179 this . trackVisible = GetFastValue ( config , 'trackVisible' , false ) ;
171180
172- var frame = GetFastValue ( config , 'frame' , null ) ;
181+ var frameConfig = GetFastValue ( config , 'frame' , null ) ;
173182
174- if ( frame )
183+ if ( frameConfig )
175184 {
176- this . setFrame ( frame ) ;
185+ this . setFrame ( frameConfig ) ;
177186 }
178187 } ,
179188
@@ -215,16 +224,72 @@ var ParticleEmitter = new Class({
215224 {
216225 return this . defaultFrame ;
217226 }
218- else
227+ else if ( this . randomFrame )
219228 {
220229 return GetRandomElement ( this . frames ) ;
221230 }
231+ else
232+ {
233+ var frame = this . frames [ this . currentFrame ] ;
234+
235+ this . _frameCounter ++ ;
236+
237+ if ( this . _frameCounter === this . frameQuantity )
238+ {
239+ this . _frameCounter = 0 ;
240+ this . currentFrame = Wrap ( this . currentFrame + 1 , 0 , this . _frameLength ) ;
241+ }
242+
243+ return frame ;
244+ }
222245 } ,
223246
224- // Either a single frame (numeric / string based), or an array of frames to randomly pick from
225- setFrame : function ( frames )
247+ // frame: 0
248+ // frame: 'red'
249+ // frame: [ 0, 1, 2, 3 ]
250+ // frame: [ 'red', 'green', 'blue', 'pink', 'white' ]
251+ // frame: { frames: [ 'red', 'green', 'blue', 'pink', 'white' ], [cycle: bool], [quantity: int] }
252+
253+ setFrame : function ( frames , pickRandom , quantity )
226254 {
227- this . manager . setEmitterFrames ( frames , this ) ;
255+ if ( pickRandom === undefined ) { pickRandom = true ; }
256+ if ( quantity === undefined ) { quantity = 1 ; }
257+
258+ this . randomFrame = pickRandom ;
259+ this . frameQuantity = quantity ;
260+ this . currentFrame = 0 ;
261+ this . _frameCounter = 0 ;
262+
263+ var t = typeof ( frames ) ;
264+
265+ if ( Array . isArray ( frames ) || t === 'string' || t === 'number' )
266+ {
267+ this . manager . setEmitterFrames ( frames , this ) ;
268+ }
269+ else if ( t === 'object' )
270+ {
271+ var frameConfig = frames ;
272+ var frames = GetFastValue ( frameConfig , 'frames' , null ) ;
273+
274+ if ( frames )
275+ {
276+ this . manager . setEmitterFrames ( frames , this ) ;
277+ }
278+
279+ var isCycle = GetFastValue ( frameConfig , 'cycle' , false ) ;
280+
281+ this . randomFrame = ( isCycle ) ? false : true ;
282+
283+ this . frameQuantity = GetFastValue ( frameConfig , 'quantity' , quantity ) ;
284+ }
285+
286+ this . _frameLength = this . frames . length ;
287+
288+ if ( this . _frameLength === 1 )
289+ {
290+ this . frameQuantity = 1 ;
291+ this . randomFrame = false ;
292+ }
228293
229294 return this ;
230295 } ,
0 commit comments