forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCCLayer.js
782 lines (707 loc) · 25.8 KB
/
CCLayer.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/** cc.Layer is a subclass of cc.Node that implements the TouchEventsDelegate protocol.<br/>
* All features from cc.Node are valid, plus the bake feature: Baked layer can cache a static layer to improve performance
* @class
* @extends cc.Node
*/
cc.Layer = cc.Node.extend(/** @lends cc.Layer# */{
_className: "Layer",
/**
* <p>Constructor of cc.Layer, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.</p>
*/
ctor: function () {
cc.Node.prototype.ctor.call(this);
this._ignoreAnchorPointForPosition = true;
this.setAnchorPoint(0.5, 0.5);
this.setContentSize(cc.winSize);
this._cascadeColorEnabled = false;
this._cascadeOpacityEnabled = false;
},
/**
* Sets the layer to cache all of children to a bake sprite, and draw itself by bake sprite. recommend using it in UI.<br/>
* This is useful only in html5 engine
* @function
* @see cc.Layer#unbake
*/
bake: function () {
this._renderCmd.bake();
},
/**
* Cancel the layer to cache all of children to a bake sprite.<br/>
* This is useful only in html5 engine
* @function
* @see cc.Layer#bake
*/
unbake: function () {
this._renderCmd.unbake();
},
/**
* Determines if the layer is baked.
* @function
* @returns {boolean}
* @see cc.Layer#bake and cc.Layer#unbake
*/
isBaked: function () {
return this._renderCmd._isBaked;
},
visit: function (parent) {
var cmd = this._renderCmd, parentCmd = parent ? parent._renderCmd : null;
// quick return if not visible
if (!this._visible) {
cmd._propagateFlagsDown(parentCmd);
return;
}
var renderer = cc.renderer;
cmd.visit(parentCmd);
if (cmd._isBaked) {
renderer.pushRenderCommand(cmd);
cmd._bakeSprite.visit(this);
}
else {
var i, children = this._children, len = children.length, child;
if (len > 0) {
if (this._reorderChildDirty) {
this.sortAllChildren();
}
// draw children zOrder < 0
for (i = 0; i < len; i++) {
child = children[i];
if (child._localZOrder < 0) {
child.visit(this);
}
else {
break;
}
}
renderer.pushRenderCommand(cmd);
for (; i < len; i++) {
children[i].visit(this);
}
} else {
renderer.pushRenderCommand(cmd);
}
}
cmd._dirtyFlag = 0;
},
addChild: function (child, localZOrder, tag) {
cc.Node.prototype.addChild.call(this, child, localZOrder, tag);
this._renderCmd._bakeForAddChild(child);
},
_createRenderCmd: function () {
if (cc._renderType === cc.game.RENDER_TYPE_CANVAS)
return new cc.Layer.CanvasRenderCmd(this);
else
return new cc.Layer.WebGLRenderCmd(this);
}
});
/**
* Creates a layer
* @deprecated since v3.0, please use the new construction instead
* @see cc.Layer
* @return {cc.Layer|Null}
*/
cc.Layer.create = function () {
return new cc.Layer();
};
/**
* <p>
* CCLayerColor is a subclass of CCLayer that implements the CCRGBAProtocol protocol. <br/>
* All features from CCLayer are valid, plus the following new features: <br/>
* - opacity <br/>
* - RGB colors </p>
* @class
* @extends cc.Layer
*
* @param {cc.Color} [color=] The color of the layer
* @param {Number} [width=] The width of the layer
* @param {Number} [height=] The height of the layer
*
* @example
* // Example
* //Create a yellow color layer as background
* var yellowBackground = new cc.LayerColor(cc.color(255,255,0,255));
* //If you didn't pass in width and height, it defaults to the same size as the canvas
*
* //create a yellow box, 200 by 200 in size
* var yellowBox = new cc.LayerColor(cc.color(255,255,0,255), 200, 200);
*/
cc.LayerColor = cc.Layer.extend(/** @lends cc.LayerColor# */{
_blendFunc: null,
_className: "LayerColor",
/**
* Returns the blend function
* @return {cc.BlendFunc}
*/
getBlendFunc: function () {
return this._blendFunc;
},
/**
* Changes width and height
* @deprecated since v3.0 please use setContentSize instead
* @see cc.Node#setContentSize
* @param {Number} w width
* @param {Number} h height
*/
changeWidthAndHeight: function (w, h) {
this.width = w;
this.height = h;
},
/**
* Changes width in Points
* @deprecated since v3.0 please use setContentSize instead
* @see cc.Node#setContentSize
* @param {Number} w width
*/
changeWidth: function (w) {
this.width = w;
},
/**
* change height in Points
* @deprecated since v3.0 please use setContentSize instead
* @see cc.Node#setContentSize
* @param {Number} h height
*/
changeHeight: function (h) {
this.height = h;
},
setOpacityModifyRGB: function (value) {
},
isOpacityModifyRGB: function () {
return false;
},
/**
* Constructor of cc.LayerColor
* @function
* @param {cc.Color} [color=]
* @param {Number} [width=]
* @param {Number} [height=]
*/
ctor: function (color, width, height) {
cc.Layer.prototype.ctor.call(this);
this._blendFunc = cc.BlendFunc._alphaNonPremultiplied();
cc.LayerColor.prototype.init.call(this, color, width, height);
},
/**
* Initialization of the layer, please do not call this function by yourself, you should pass the parameters to constructor to initialize a layer
* @param {cc.Color} [color=]
* @param {Number} [width=]
* @param {Number} [height=]
* @return {Boolean}
*/
init: function (color, width, height) {
var winSize = cc.director.getWinSize();
color = color || cc.color(0, 0, 0, 255);
width = width === undefined ? winSize.width : width;
height = height === undefined ? winSize.height : height;
var locRealColor = this._realColor;
locRealColor.r = color.r;
locRealColor.g = color.g;
locRealColor.b = color.b;
this._realOpacity = color.a;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.colorDirty | cc.Node._dirtyFlags.opacityDirty);
cc.LayerColor.prototype.setContentSize.call(this, width, height);
return true;
},
visit: function (parent) {
var cmd = this._renderCmd, parentCmd = parent ? parent._renderCmd : null;
// quick return if not visible
if (!this._visible) {
cmd._propagateFlagsDown(parentCmd);
return;
}
var renderer = cc.renderer;
cmd.visit(parentCmd);
if (cmd._isBaked) {
renderer.pushRenderCommand(cmd._bakeRenderCmd);
//the bakeSprite is drawing
cmd._bakeSprite._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
cmd._bakeSprite.visit(this);
}
else {
var i, children = this._children, len = children.length;
if (len > 0) {
if (this._reorderChildDirty) {
this.sortAllChildren();
}
// draw children zOrder < 0
for (i = 0; i < len; i++) {
child = children[i];
if (child._localZOrder < 0) {
child.visit(this);
}
else {
break;
}
}
renderer.pushRenderCommand(cmd);
for (; i < len; i++) {
children[i].visit(this);
}
} else {
renderer.pushRenderCommand(cmd);
}
}
cmd._dirtyFlag = 0;
},
/**
* Sets the blend func, you can pass either a cc.BlendFunc object or source and destination value separately
* @param {Number|cc.BlendFunc} src
* @param {Number} [dst]
*/
setBlendFunc: function (src, dst) {
var locBlendFunc = this._blendFunc;
if (dst === undefined) {
locBlendFunc.src = src.src;
locBlendFunc.dst = src.dst;
} else {
locBlendFunc.src = src;
locBlendFunc.dst = dst;
}
this._renderCmd.updateBlendFunc(locBlendFunc);
},
_createRenderCmd: function () {
if (cc._renderType === cc.game.RENDER_TYPE_CANVAS)
return new cc.LayerColor.CanvasRenderCmd(this);
else
return new cc.LayerColor.WebGLRenderCmd(this);
}
});
/**
* Creates a cc.Layer with color, width and height in Points
* @deprecated since v3.0 please use the new construction instead
* @see cc.LayerColor
* @param {cc.Color} color
* @param {Number|Null} [width=]
* @param {Number|Null} [height=]
* @return {cc.LayerColor}
*/
cc.LayerColor.create = function (color, width, height) {
return new cc.LayerColor(color, width, height);
};
//LayerColor - Getter Setter
(function () {
var proto = cc.LayerColor.prototype;
cc.defineGetterSetter(proto, "width", proto._getWidth, proto._setWidth);
cc.defineGetterSetter(proto, "height", proto._getHeight, proto._setHeight);
})();
/**
* <p>
* CCLayerGradient is a subclass of cc.LayerColor that draws gradients across the background.<br/>
*<br/>
* All features from cc.LayerColor are valid, plus the following new features:<br/>
* <ul><li>direction</li>
* <li>final color</li>
* <li>interpolation mode</li></ul>
* <br/>
* Color is interpolated between the startColor and endColor along the given<br/>
* vector (starting at the origin, ending at the terminus). If no vector is<br/>
* supplied, it defaults to (0, -1) -- a fade from top to bottom.<br/>
* <br/>
* If 'compressedInterpolation' is disabled, you will not see either the start or end color for<br/>
* non-cardinal vectors; a smooth gradient implying both end points will be still<br/>
* be drawn, however.<br/>
*<br/>
* If 'compressedInterpolation' is enabled (default mode) you will see both the start and end colors of the gradient.
* </p>
* @class
* @extends cc.LayerColor
*
* @param {cc.Color} start Starting color
* @param {cc.Color} end Ending color
* @param {cc.Point} [v=cc.p(0, -1)] A vector defines the gradient direction, default direction is from top to bottom
*
* @property {cc.Color} startColor - Start color of the color gradient
* @property {cc.Color} endColor - End color of the color gradient
* @property {Number} startOpacity - Start opacity of the color gradient
* @property {Number} endOpacity - End opacity of the color gradient
* @property {Number} vector - Direction vector of the color gradient
* @property {Number} compressedInterpolation - Indicate whether or not the interpolation will be compressed
*/
cc.LayerGradient = cc.LayerColor.extend(/** @lends cc.LayerGradient# */{
_endColor: null,
_startOpacity: 255,
_endOpacity: 255,
_alongVector: null,
_compressedInterpolation: false,
_className: "LayerGradient",
_colorStops: [],
/**
* Constructor of cc.LayerGradient
* @param {cc.Color} start
* @param {cc.Color} end
* @param {cc.Point} [v=cc.p(0, -1)]
* @param {Array|Null} stops
*
* @example Using ColorStops argument:
* //startColor & endColor are for default and backward compatibility
* var layerGradient = new cc.LayerGradient(cc.color.RED, new cc.Color(255,0,0,0), cc.p(0, -1),
* [{p:0, color: cc.color.RED},
* {p:.5, color: new cc.Color(0,0,0,0)},
* {p:1, color: cc.color.RED}]);
* //where p = A value between 0.0 and 1.0 that represents the position between start and end in a gradient
*
*/
ctor: function (start, end, v, stops) {
cc.LayerColor.prototype.ctor.call(this);
this._endColor = cc.color(0, 0, 0, 255);
this._alongVector = cc.p(0, -1);
this._startOpacity = 255;
this._endOpacity = 255;
if (stops && stops instanceof Array) {
this._colorStops = stops;
stops.splice(0, 0, {p: 0, color: start || cc.color.BLACK});
stops.push({p: 1, color: end || cc.color.BLACK});
} else
this._colorStops = [{p: 0, color: start || cc.color.BLACK}, {p: 1, color: end || cc.color.BLACK}];
cc.LayerGradient.prototype.init.call(this, start, end, v, stops);
},
/**
* Initialization of the layer, please do not call this function by yourself, you should pass the parameters to constructor to initialize a layer
* @param {cc.Color} start starting color
* @param {cc.Color} end
* @param {cc.Point|Null} v
* @param {Array|Null} stops
* @return {Boolean}
*/
init: function (start, end, v, stops) {
start = start || cc.color(0, 0, 0, 255);
end = end || cc.color(0, 0, 0, 255);
v = v || cc.p(0, -1);
var _t = this;
// Initializes the CCLayer with a gradient between start and end in the direction of v.
var locEndColor = _t._endColor;
_t._startOpacity = start.a;
locEndColor.r = end.r;
locEndColor.g = end.g;
locEndColor.b = end.b;
_t._endOpacity = end.a;
_t._alongVector = v;
_t._compressedInterpolation = true;
cc.LayerColor.prototype.init.call(_t, cc.color(start.r, start.g, start.b, 255));
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.colorDirty | cc.Node._dirtyFlags.opacityDirty | cc.Node._dirtyFlags.gradientDirty);
return true;
},
/**
* Sets the untransformed size of the LayerGradient.
* @param {cc.Size|Number} size The untransformed size of the LayerGradient or The untransformed size's width of the LayerGradient.
* @param {Number} [height] The untransformed size's height of the LayerGradient.
*/
setContentSize: function (size, height) {
cc.LayerColor.prototype.setContentSize.call(this, size, height);
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.gradientDirty);
},
_setWidth: function (width) {
cc.LayerColor.prototype._setWidth.call(this, width);
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.gradientDirty);
},
_setHeight: function (height) {
cc.LayerColor.prototype._setHeight.call(this, height);
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.gradientDirty);
},
/**
* Returns the starting color
* @return {cc.Color}
*/
getStartColor: function () {
return cc.color(this._realColor);
},
/**
* Sets the starting color
* @param {cc.Color} color
* @example
* // Example
* myGradientLayer.setStartColor(cc.color(255,0,0));
* //set the starting gradient to red
*/
setStartColor: function (color) {
this.color = color;
//update the color stops
var stops = this._colorStops;
if (stops && stops.length > 0) {
var selColor = stops[0].color;
selColor.r = color.r;
selColor.g = color.g;
selColor.b = color.b;
}
},
/**
* Sets the end gradient color
* @param {cc.Color} color
* @example
* // Example
* myGradientLayer.setEndColor(cc.color(255,0,0));
* //set the ending gradient to red
*/
setEndColor: function (color) {
var locColor = this._endColor;
locColor.r = color.r;
locColor.g = color.g;
locColor.b = color.b;
//update the color stops
var stops = this._colorStops;
if (stops && stops.length > 0) {
var selColor = stops[stops.length - 1].color;
selColor.r = color.r;
selColor.g = color.g;
selColor.b = color.b;
}
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.colorDirty);
},
/**
* Returns the end color
* @return {cc.Color}
*/
getEndColor: function () {
return cc.color(this._endColor);
},
/**
* Sets starting gradient opacity
* @param {Number} o from 0 to 255, 0 is transparent
*/
setStartOpacity: function (o) {
this._startOpacity = o;
//update the color stops
var stops = this._colorStops;
if (stops && stops.length > 0)
stops[0].color.a = o;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.opacityDirty);
},
/**
* Returns the starting gradient opacity
* @return {Number}
*/
getStartOpacity: function () {
return this._startOpacity;
},
/**
* Sets the end gradient opacity
* @param {Number} o
*/
setEndOpacity: function (o) {
this._endOpacity = o;
var stops = this._colorStops;
if (stops && stops.length > 0)
stops[stops.length - 1].color.a = o;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.opacityDirty);
},
/**
* Returns the end gradient opacity
* @return {Number}
*/
getEndOpacity: function () {
return this._endOpacity;
},
/**
* Sets the direction vector of the gradient
* @param {cc.Point} Var
*/
setVector: function (Var) {
this._alongVector.x = Var.x;
this._alongVector.y = Var.y;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.gradientDirty);
},
/**
* Returns the direction vector of the gradient
* @return {cc.Point}
*/
getVector: function () {
return cc.p(this._alongVector.x, this._alongVector.y);
},
/**
* Returns whether compressed interpolation is enabled
* @return {Boolean}
*/
isCompressedInterpolation: function () {
return this._compressedInterpolation;
},
/**
* Sets whether compressed interpolation is enabled
* @param {Boolean} compress
*/
setCompressedInterpolation: function (compress) {
this._compressedInterpolation = compress;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.gradientDirty);
},
/**
* Return an array of Object representing a colorStop for the gradient, if no stops was specified
* start & endColor will be provided as default values
* @example
* [{p: 0, color: cc.color.RED},{p: 1, color: cc.color.RED},...]
* @returns {Array}
*/
getColorStops: function () {
return this._colorStops;
},
/**
* Set the colorStops to create the gradient using multiple point & color
*
* @param colorStops
*
* @example
* //startColor & endColor are for default and backward compatibility
* var layerGradient = new cc.LayerGradient(cc.color.RED, new cc.Color(255,0,0,0), cc.p(0, -1));
* layerGradient.setColorStops([{p:0, color: cc.color.RED},
* {p:.5, color: new cc.Color(0,0,0,0)},
* {p:1, color: cc.color.RED}]);
* //where p = A value between 0.0 and 1.0 that represents the position between start and end in a gradient
*
*/
setColorStops: function (colorStops) {
this._colorStops = colorStops;
//todo need update the start color and end color
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.colorDirty | cc.Node._dirtyFlags.opacityDirty | cc.Node._dirtyFlags.gradientDirty);
},
_createRenderCmd: function () {
if (cc._renderType === cc.game.RENDER_TYPE_CANVAS)
return new cc.LayerGradient.CanvasRenderCmd(this);
else
return new cc.LayerGradient.WebGLRenderCmd(this);
}
});
/**
* Creates a gradient layer
* @deprecated since v3.0, please use the new construction instead
* @see cc.layerGradient
* @param {cc.Color} start starting color
* @param {cc.Color} end ending color
* @param {cc.Point|Null} v
* @param {Array|NULL} stops
* @return {cc.LayerGradient}
*/
cc.LayerGradient.create = function (start, end, v, stops) {
return new cc.LayerGradient(start, end, v, stops);
};
//LayerGradient - Getter Setter
(function () {
var proto = cc.LayerGradient.prototype;
// Extended properties
/** @expose */
proto.startColor;
cc.defineGetterSetter(proto, "startColor", proto.getStartColor, proto.setStartColor);
/** @expose */
proto.endColor;
cc.defineGetterSetter(proto, "endColor", proto.getEndColor, proto.setEndColor);
/** @expose */
proto.startOpacity;
cc.defineGetterSetter(proto, "startOpacity", proto.getStartOpacity, proto.setStartOpacity);
/** @expose */
proto.endOpacity;
cc.defineGetterSetter(proto, "endOpacity", proto.getEndOpacity, proto.setEndOpacity);
/** @expose */
proto.vector;
cc.defineGetterSetter(proto, "vector", proto.getVector, proto.setVector);
/** @expose */
proto.colorStops;
cc.defineGetterSetter(proto, "colorStops", proto.getColorStops, proto.setColorStops);
})();
/**
* CCMultipleLayer is a CCLayer with the ability to multiplex it's children.<br/>
* Features:<br/>
* <ul><li>- It supports one or more children</li>
* <li>- Only one children will be active a time</li></ul>
* @class
* @extends cc.Layer
* @param {Array} layers an array of cc.Layer
* @example
* // Example
* var multiLayer = new cc.LayerMultiple(layer1, layer2, layer3);//any number of layers
*/
cc.LayerMultiplex = cc.Layer.extend(/** @lends cc.LayerMultiplex# */{
_enabledLayer: 0,
_layers: null,
_className: "LayerMultiplex",
/**
* Constructor of cc.LayerMultiplex
* @param {Array} layers an array of cc.Layer
*/
ctor: function (layers) {
cc.Layer.prototype.ctor.call(this);
if (layers instanceof Array)
cc.LayerMultiplex.prototype.initWithLayers.call(this, layers);
else
cc.LayerMultiplex.prototype.initWithLayers.call(this, Array.prototype.slice.call(arguments));
},
/**
* Initialization of the layer multiplex, please do not call this function by yourself, you should pass the parameters to constructor to initialize a layer multiplex
* @param {Array} layers an array of cc.Layer
* @return {Boolean}
*/
initWithLayers: function (layers) {
if ((layers.length > 0) && (layers[layers.length - 1] == null))
cc.log(cc._LogInfos.LayerMultiplex_initWithLayers);
this._layers = layers;
this._enabledLayer = 0;
this.addChild(this._layers[this._enabledLayer]);
return true;
},
/**
* Switches to a certain layer indexed by n.<br/>
* The current (old) layer will be removed from it's parent with 'cleanup:YES'.
* @param {Number} n the layer index to switch to
*/
switchTo: function (n) {
if (n >= this._layers.length) {
cc.log(cc._LogInfos.LayerMultiplex_switchTo);
return;
}
this.removeChild(this._layers[this._enabledLayer], true);
this._enabledLayer = n;
this.addChild(this._layers[n]);
},
/**
* Release the current layer and switches to another layer indexed by n.<br/>
* The current (old) layer will be removed from it's parent with 'cleanup:YES'.
* @param {Number} n the layer index to switch to
*/
switchToAndReleaseMe: function (n) {
if (n >= this._layers.length) {
cc.log(cc._LogInfos.LayerMultiplex_switchToAndReleaseMe);
return;
}
this.removeChild(this._layers[this._enabledLayer], true);
//[layers replaceObjectAtIndex:_enabledLayer withObject:[NSNull null]];
this._layers[this._enabledLayer] = null;
this._enabledLayer = n;
this.addChild(this._layers[n]);
},
/**
* Add a layer to the multiplex layers list
* @param {cc.Layer} layer
*/
addLayer: function (layer) {
if (!layer) {
cc.log(cc._LogInfos.LayerMultiplex_addLayer);
return;
}
this._layers.push(layer);
}
});
/**
* Creates a cc.LayerMultiplex with one or more layers using a variable argument list.
* @deprecated since v3.0, please use new construction instead
* @see cc.LayerMultiplex
* @return {cc.LayerMultiplex|Null}
*/
cc.LayerMultiplex.create = function (/*Multiple Arguments*/) {
return new cc.LayerMultiplex(Array.prototype.slice.call(arguments));
};