forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCCNode.js
2577 lines (2351 loc) · 96.2 KB
/
CCNode.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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
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.
****************************************************************************/
/**
* Default Node tag
* @constant
* @type Number
*/
cc.NODE_TAG_INVALID = -1;
/**
* XXX: Yes, nodes might have a sort problem once every 15 days if the game runs at 60 FPS and each frame sprites are reordered.
*/
cc.s_globalOrderOfArrival = 1;
/**
* <p>cc.Node is the root class of all node. Anything that gets drawn or contains things that get drawn is a cc.Node.<br/>
* The most popular cc.Nodes are: cc.Scene, cc.Layer, cc.Sprite, cc.Menu.</p>
*
* <p>The main features of a cc.Node are: <br/>
* - They can contain other cc.Node nodes (addChild, getChildByTag, removeChild, etc) <br/>
* - They can schedule periodic callback (schedule, unschedule, etc) <br/>
* - They can execute actions (runAction, stopAction, etc) <br/></p>
*
* <p>Some cc.Node nodes provide extra functionality for them or their children.</p>
*
* <p>Subclassing a cc.Node usually means (one/all) of: <br/>
* - overriding constructor function "ctor" to initialize resources and schedule callbacks<br/>
* - create callbacks to handle the advancement of time<br/></p>
*
* <p>Features of cc.Node: <br/>
* - position <br/>
* - scale (x, y) <br/>
* - rotation (in degrees, clockwise)<br/>
* - anchor point<br/>
* - size <br/>
* - color <br/>
* - opacity <br/>
* - visible<br/>
* - z-order<br/>
* - WebGL z position<br/></P>
*
* <p> Default values: <br/>
* - rotation: 0 <br/>
* - position: (x=0,y=0) <br/>
* - scale: (x=1,y=1) <br/>
* - contentSize: (x=0,y=0)<br/>
* - anchorPoint: (x=0,y=0)<br/>
* - color: (r=255,g=255,b=255)<br/>
* - opacity: 255</p>
*
* <p> Limitations:<br/>
* - A cc.Node is a "void" object. It doesn't have a texture <br/></P>
*
* <p>Order in transformations with grid disabled <br/>
* -# The node will be translated (position) <br/>
* -# The node will be rotated (rotation)<br/>
* -# The node will be scaled (scale) <br/>
*
* <p>Order in transformations with grid enabled<br/>
* -# The node will be translated (position)<br/>
* -# The node will be rotated (rotation) <br/>
* -# The node will be scaled (scale) <br/>
* -# The grid will capture the screen <br/>
* -# The node will be moved according to the camera values (camera) <br/>
* -# The grid will render the captured screen <br/></P>
*
* @class
* @extends cc.Class
*
* @property {Number} x - x axis position of node
* @property {Number} y - y axis position of node
* @property {Number} width - Width of node
* @property {Number} height - Height of node
* @property {Number} anchorX - Anchor point's position on x axis
* @property {Number} anchorY - Anchor point's position on y axis
* @property {Boolean} ignoreAnchor - Indicate whether ignore the anchor point property for positioning
* @property {Number} skewX - Skew x
* @property {Number} skewY - Skew y
* @property {Number} zIndex - Z order in depth which stands for the drawing order
* @property {Number} vertexZ - WebGL Z vertex of this node, z order works OK if all the nodes uses the same openGL Z vertex
* @property {Number} rotation - Rotation of node
* @property {Number} rotationX - Rotation on x axis
* @property {Number} rotationY - Rotation on y axis
* @property {Number} scale - Scale of node
* @property {Number} scaleX - Scale on x axis
* @property {Number} scaleY - Scale on y axis
* @property {Boolean} visible - Indicate whether node is visible or not
* @property {cc.Color} color - Color of node, default value is white: (255, 255, 255)
* @property {Boolean} cascadeColor - Indicate whether node's color value affect its child nodes, default value is false
* @property {Number} opacity - Opacity of node, default value is 255
* @property {Boolean} opacityModifyRGB - Indicate whether opacity affect the color value, default value is false
* @property {Boolean} cascadeOpacity - Indicate whether node's opacity value affect its child nodes, default value is false
* @property {Array} children - <@readonly> All children nodes
* @property {Number} childrenCount - <@readonly> Number of children
* @property {cc.Node} parent - Parent node
* @property {Boolean} running - <@readonly> Indicate whether node is running or not
* @property {Number} tag - Tag of node
* @property {Object} userData - Custom user data
* @property {Object} userObject - User assigned CCObject, similar to userData, but instead of holding a void* it holds an id
* @property {Number} arrivalOrder - The arrival order, indicates which children is added previously
* @property {cc.ActionManager} actionManager - The CCActionManager object that is used by all actions.
* @property {cc.Scheduler} scheduler - cc.Scheduler used to schedule all "updates" and timers.
* @property {cc.GridBase} grid - grid object that is used when applying effects
* @property {cc.GLProgram} shaderProgram - The shader program currently used for this node
* @property {Number} glServerState - The state of OpenGL server side
*/
cc.Node = cc.Class.extend(/** @lends cc.Node# */{
_localZOrder: 0, ///< Local order (relative to its siblings) used to sort the node
_globalZOrder: 0, ///< Global order used to sort the node
_vertexZ: 0.0,
_customZ: NaN,
_rotationX: 0,
_rotationY: 0.0,
_scaleX: 1.0,
_scaleY: 1.0,
_position: null,
_normalizedPosition:null,
_usingNormalizedPosition: false,
_normalizedPositionDirty: false,
_skewX: 0.0,
_skewY: 0.0,
// children (lazy allocs),
_children: null,
// lazy alloc,
_visible: true,
_anchorPoint: null,
_contentSize: null,
_running: false,
_parent: null,
// "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true
_ignoreAnchorPointForPosition: false,
tag: cc.NODE_TAG_INVALID,
// userData is always initialized as nil
userData: null,
userObject: null,
//since 2.0 api
_reorderChildDirty: false,
_shaderProgram: null,
arrivalOrder: 0,
_actionManager: null,
_scheduler: null,
_eventDispatcher: null,
_additionalTransformDirty: false,
_additionalTransform: null,
_componentContainer: null,
_isTransitionFinished: false,
_className: "Node",
_showNode: false,
_name: "", ///<a string label, an user defined string to identify this node
_realOpacity: 255,
_realColor: null,
_cascadeColorEnabled: false,
_cascadeOpacityEnabled: false,
_renderCmd:null,
_camera: null,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @function
*/
ctor: function(){
this._initNode();
this._initRendererCmd();
},
_initNode: function () {
var _t = this;
_t._anchorPoint = cc.p(0, 0);
_t._contentSize = cc.size(0, 0);
_t._position = cc.p(0, 0);
_t._normalizedPosition = cc.p(0,0);
_t._children = [];
var director = cc.director;
_t._actionManager = director.getActionManager();
_t._scheduler = director.getScheduler();
_t._additionalTransform = cc.affineTransformMakeIdentity();
if (cc.ComponentContainer) {
_t._componentContainer = new cc.ComponentContainer(_t);
}
this._realOpacity = 255;
this._realColor = cc.color(255, 255, 255, 255);
this._cascadeColorEnabled = false;
this._cascadeOpacityEnabled = false;
},
/**
* Initializes the instance of cc.Node
* @function
* @returns {boolean} Whether the initialization was successful.
*/
init: function () {
//this._initNode(); //this has been called in ctor.
return true;
},
_arrayMakeObjectsPerformSelector: function (array, callbackType) {
if (!array || array.length === 0)
return;
var i, len = array.length, node;
var nodeCallbackType = cc.Node._stateCallbackType;
switch (callbackType) {
case nodeCallbackType.onEnter:
for (i = 0; i < len; i++) {
node = array[i];
if (node)
node.onEnter();
}
break;
case nodeCallbackType.onExit:
for (i = 0; i < len; i++) {
node = array[i];
if (node)
node.onExit();
}
break;
case nodeCallbackType.onEnterTransitionDidFinish:
for (i = 0; i < len; i++) {
node = array[i];
if (node)
node.onEnterTransitionDidFinish();
}
break;
case nodeCallbackType.cleanup:
for (i = 0; i < len; i++) {
node = array[i];
if (node)
node.cleanup();
}
break;
case nodeCallbackType.updateTransform:
for (i = 0; i < len; i++) {
node = array[i];
if (node)
node.updateTransform();
}
break;
case nodeCallbackType.onExitTransitionDidStart:
for (i = 0; i < len; i++) {
node = array[i];
if (node)
node.onExitTransitionDidStart();
}
break;
case nodeCallbackType.sortAllChildren:
for (i = 0; i < len; i++) {
node = array[i];
if (node)
node.sortAllChildren();
}
break;
default :
cc.assert(0, cc._LogInfos.Node__arrayMakeObjectsPerformSelector);
break;
}
},
/**
* <p>Properties configuration function </br>
* All properties in attrs will be set to the node, </br>
* when the setter of the node is available, </br>
* the property will be set via setter function.</br>
* </p>
* @function
* @param {Object} attrs Properties to be set to node
*/
attr: function (attrs) {
for (var key in attrs) {
this[key] = attrs[key];
}
},
/**
* <p>Returns the skew degrees in X </br>
* The X skew angle of the node in degrees. <br/>
* This angle describes the shear distortion in the X direction.<br/>
* Thus, it is the angle between the Y axis and the left edge of the shape </br>
* The default skewX angle is 0. Positive values distort the node in a CW direction.</br>
* </p>
* @function
* @return {Number} The X skew angle of the node in degrees.
*/
getSkewX: function () {
return this._skewX;
},
/**
* <p>
* Changes the X skew angle of the node in degrees. <br/>
* <br/>
* This angle describes the shear distortion in the X direction. <br/>
* Thus, it is the angle between the Y axis and the left edge of the shape <br/>
* The default skewX angle is 0. Positive values distort the node in a CW direction.
* </p>
* @function
* @param {Number} newSkewX The X skew angle of the node in degrees.
*/
setSkewX: function (newSkewX) {
this._skewX = newSkewX;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* <p>Returns the skew degrees in Y <br/>
* The Y skew angle of the node in degrees. <br/>
* This angle describes the shear distortion in the Y direction. <br/>
* Thus, it is the angle between the X axis and the bottom edge of the shape <br/>
* The default skewY angle is 0. Positive values distort the node in a CCW direction. <br/>
* </p>
* @function
* @return {Number} The Y skew angle of the node in degrees.
*/
getSkewY: function () {
return this._skewY;
},
/**
* <p>
* Changes the Y skew angle of the node in degrees. <br/>
* <br/>
* This angle describes the shear distortion in the Y direction. <br/>
* Thus, it is the angle between the X axis and the bottom edge of the shape <br/>
* The default skewY angle is 0. Positive values distort the node in a CCW direction. <br/>
* </p>
* @function
* @param {Number} newSkewY The Y skew angle of the node in degrees.
*/
setSkewY: function (newSkewY) {
this._skewY = newSkewY;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* <p> LocalZOrder is the 'key' used to sort the node relative to its siblings. <br/>
* <br/>
* The Node's parent will sort all its children based ont the LocalZOrder value. <br/>
* If two nodes have the same LocalZOrder, then the node that was added first to the children's array <br/>
* will be in front of the other node in the array. <br/>
* <br/>
* Also, the Scene Graph is traversed using the "In-Order" tree traversal algorithm ( http://en.wikipedia.org/wiki/Tree_traversal#In-order )
* <br/>
* And Nodes that have LocalZOder values < 0 are the "left" subtree <br/>
* While Nodes with LocalZOder >=0 are the "right" subtree. </p>
* @function
* @param {Number} localZOrder
*/
setLocalZOrder: function (localZOrder) {
this._localZOrder = localZOrder;
if (this._parent)
this._parent.reorderChild(this, localZOrder);
cc.eventManager._setDirtyForNode(this);
},
//Helper function used by `setLocalZOrder`. Don't use it unless you know what you are doing.
_setLocalZOrder: function (localZOrder) {
this._localZOrder = localZOrder;
},
/**
* Returns the local Z order of this node.
* @function
* @returns {Number} The local (relative to its siblings) Z order.
*/
getLocalZOrder: function () {
return this._localZOrder;
},
/**
* Returns z order of this node
* @function
* @return {Number}
* @deprecated since 3.0, please use getLocalZOrder instead
*/
getZOrder: function () {
cc.log(cc._LogInfos.Node_getZOrder);
return this.getLocalZOrder();
},
/**
* <p>
* Sets the Z order which stands for the drawing order, and reorder this node in its parent's children array. <br/>
* <br/>
* The Z order of node is relative to its "brothers": children of the same parent. <br/>
* It's nothing to do with OpenGL's z vertex. This one only affects the draw order of nodes in cocos2d. <br/>
* The larger number it is, the later this node will be drawn in each message loop. <br/>
* Please refer to setVertexZ(float) for the difference.
* </p>
* @function
* @param {Number} z Z order of this node.
* @deprecated since 3.0, please use setLocalZOrder instead
*/
setZOrder: function (z) {
cc.log(cc._LogInfos.Node_setZOrder);
this.setLocalZOrder(z);
},
/**
* <p>Defines the oder in which the nodes are renderer. <br/>
* Nodes that have a Global Z Order lower, are renderer first. <br/>
* <br/>
* In case two or more nodes have the same Global Z Order, the oder is not guaranteed. <br/>
* The only exception if the Nodes have a Global Z Order == 0. In that case, the Scene Graph order is used. <br/>
* <br/>
* By default, all nodes have a Global Z Order = 0. That means that by default, the Scene Graph order is used to render the nodes. <br/>
* <br/>
* Global Z Order is useful when you need to render nodes in an order different than the Scene Graph order. <br/>
* <br/>
* Limitations: Global Z Order can't be used used by Nodes that have SpriteBatchNode as one of their ancestors. <br/>
* And if ClippingNode is one of the ancestors, then "global Z order" will be relative to the ClippingNode. </p>
* @function
* @param {Number} globalZOrder
*/
setGlobalZOrder: function (globalZOrder) {
if (this._globalZOrder !== globalZOrder) {
this._globalZOrder = globalZOrder;
cc.eventManager._setDirtyForNode(this);
}
},
/**
* Return the Node's Global Z Order.
* @function
* @returns {number} The node's global Z order
*/
getGlobalZOrder: function () {
return this._globalZOrder;
},
/**
* Returns WebGL Z vertex of this node.
* @function
* @return {Number} WebGL Z vertex of this node
*/
getVertexZ: function () {
return this._vertexZ;
},
/**
* <p>
* Sets the real WebGL Z vertex. <br/>
* <br/>
* Differences between openGL Z vertex and cocos2d Z order: <br/>
* - WebGL Z modifies the Z vertex, and not the Z order in the relation between parent-children <br/>
* - WebGL Z might require to set 2D projection <br/>
* - cocos2d Z order works OK if all the nodes uses the same WebGL Z vertex. eg: vertexZ = 0 <br/>
* <br/>
* @warning Use it at your own risk since it might break the cocos2d parent-children z order
* </p>
* @function
* @param {Number} Var
*/
setVertexZ: function (Var) {
this._customZ = this._vertexZ = Var;
},
/**
* Returns the rotation (angle) of the node in degrees. 0 is the default rotation angle. Positive values rotate node clockwise.
* @function
* @return {Number} The rotation of the node in degrees.
*/
getRotation: function () {
if (this._rotationX !== this._rotationY)
cc.log(cc._LogInfos.Node_getRotation);
return this._rotationX;
},
/**
* <p>
* Sets the rotation (angle) of the node in degrees. <br/>
* <br/>
* 0 is the default rotation angle. <br/>
* Positive values rotate node clockwise, and negative values for anti-clockwise.
* </p>
* @function
* @param {Number} newRotation The rotation of the node in degrees.
*/
setRotation: function (newRotation) {
this._rotationX = this._rotationY = newRotation;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the X axis rotation (angle) which represent a horizontal rotational skew of the node in degrees. <br/>
* 0 is the default rotation angle. Positive values rotate node clockwise<br/>
* (support only in WebGL rendering mode)
* @function
* @return {Number} The X rotation in degrees.
*/
getRotationX: function () {
return this._rotationX;
},
/**
* <p>
* Sets the X rotation (angle) of the node in degrees which performs a horizontal rotational skew. <br/>
* (support only in WebGL rendering mode) <br/>
* 0 is the default rotation angle. <br/>
* Positive values rotate node clockwise, and negative values for anti-clockwise.
* </p>
* @param {Number} rotationX The X rotation in degrees which performs a horizontal rotational skew.
*/
setRotationX: function (rotationX) {
this._rotationX = rotationX;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the Y axis rotation (angle) which represent a vertical rotational skew of the node in degrees. <br/>
* 0 is the default rotation angle. Positive values rotate node clockwise<br/>
* (support only in WebGL rendering mode)
* @function
* @return {Number} The Y rotation in degrees.
*/
getRotationY: function () {
return this._rotationY;
},
/**
* <p>
* Sets the Y rotation (angle) of the node in degrees which performs a vertical rotational skew. <br/>
* (support only in WebGL rendering mode) <br/>
* 0 is the default rotation angle. <br/>
* Positive values rotate node clockwise, and negative values for anti-clockwise.
* </p>
* @param rotationY The Y rotation in degrees.
*/
setRotationY: function (rotationY) {
this._rotationY = rotationY;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the scale factor of the node.
* @warning: Assertion will fail when _scaleX != _scaleY.
* @function
* @return {Number} The scale factor
*/
getScale: function () {
if (this._scaleX !== this._scaleY)
cc.log(cc._LogInfos.Node_getScale);
return this._scaleX;
},
/**
* Sets the scale factor of the node. 1.0 is the default scale factor. This function can modify the X and Y scale at the same time.
* @function
* @param {Number} scale or scaleX value
* @param {Number} [scaleY=]
*/
setScale: function (scale, scaleY) {
this._scaleX = scale;
this._scaleY = (scaleY || scaleY === 0) ? scaleY : scale;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the scale factor on X axis of this node
* @function
* @return {Number} The scale factor on X axis.
*/
getScaleX: function () {
return this._scaleX;
},
/**
* <p>
* Changes the scale factor on X axis of this node <br/>
* The default value is 1.0 if you haven't changed it before
* </p>
* @function
* @param {Number} newScaleX The scale factor on X axis.
*/
setScaleX: function (newScaleX) {
this._scaleX = newScaleX;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the scale factor on Y axis of this node
* @function
* @return {Number} The scale factor on Y axis.
*/
getScaleY: function () {
return this._scaleY;
},
/**
* <p>
* Changes the scale factor on Y axis of this node <br/>
* The Default value is 1.0 if you haven't changed it before.
* </p>
* @function
* @param {Number} newScaleY The scale factor on Y axis.
*/
setScaleY: function (newScaleY) {
this._scaleY = newScaleY;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* <p>
* Changes the position (x,y) of the node in cocos2d coordinates.<br/>
* The original point (0,0) is at the left-bottom corner of screen.<br/>
* Usually we use cc.p(x,y) to compose CCPoint object.<br/>
* and Passing two numbers (x,y) is more efficient than passing CCPoint object.
* </p>
* @function
* @param {cc.Point|Number} newPosOrxValue The position (x,y) of the node in coordinates or the X coordinate for position
* @param {Number} [yValue] Y coordinate for position
* @example
* var size = cc.winSize;
* node.setPosition(size.width/2, size.height/2);
*/
setPosition: function (newPosOrxValue, yValue) {
var locPosition = this._position;
if (yValue === undefined) {
if(locPosition.x === newPosOrxValue.x && locPosition.y === newPosOrxValue.y)
return;
locPosition.x = newPosOrxValue.x;
locPosition.y = newPosOrxValue.y;
} else {
if(locPosition.x === newPosOrxValue && locPosition.y === yValue)
return;
locPosition.x = newPosOrxValue;
locPosition.y = yValue;
}
this._usingNormalizedPosition = false;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* <p>
* Sets the position (x,y) using values between 0 and 1. <br/>
* The positions in pixels is calculated like the following: <br/>
* _position = _normalizedPosition * parent.getContentSize()
* </p>
* @param {cc.Point|Number} posOrX
* @param {Number} [y]
*/
setNormalizedPosition: function(posOrX, y){
var locPosition = this._normalizedPosition;
if (y === undefined) {
locPosition.x = posOrX.x;
locPosition.y = posOrX.y;
} else {
locPosition.x = posOrX;
locPosition.y = y;
}
this._normalizedPositionDirty = this._usingNormalizedPosition = true;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* <p>Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.</p>
* @function
* @return {cc.Point} The position (x,y) of the node in OpenGL coordinates
*/
getPosition: function () {
return cc.p(this._position);
},
/**
* returns the normalized position
* @returns {cc.Point}
*/
getNormalizedPosition: function(){
return cc.p(this._normalizedPosition);
},
/**
* <p>Returns the x axis position of the node in cocos2d coordinates.</p>
* @function
* @return {Number}
*/
getPositionX: function () {
return this._position.x;
},
/**
* <p>Sets the x axis position of the node in cocos2d coordinates.</p>
* @function
* @param {Number} x The new position in x axis
*/
setPositionX: function (x) {
this._position.x = x;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* <p>Returns the y axis position of the node in cocos2d coordinates.</p>
* @function
* @return {Number}
*/
getPositionY: function () {
return this._position.y;
},
/**
* <p>Sets the y axis position of the node in cocos2d coordinates.</p>
* @function
* @param {Number} y The new position in y axis
*/
setPositionY: function (y) {
this._position.y = y;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
/**
* Returns the amount of children.
* @function
* @return {Number} The amount of children.
*/
getChildrenCount: function () {
return this._children.length;
},
/**
* Returns an array of all children <br/>
* Composing a "tree" structure is a very important feature of CCNode
* @function
* @return {Array} An array of children
* @example
* //This sample code traverses all children nodes, and set their position to (0,0)
* var allChildren = parent.getChildren();
* for(var i = 0; i< allChildren.length; i++) {
* allChildren[i].setPosition(0,0);
* }
*/
getChildren: function () {
return this._children;
},
/**
* Returns if the node is visible
* @function
* @see cc.Node#setVisible
* @return {Boolean} true if the node is visible, false if the node is hidden.
*/
isVisible: function () {
return this._visible;
},
/**
* Sets whether the node is visible <br/>
* The default value is true
* @function
* @param {Boolean} visible Pass true to make the node visible, false to hide the node.
*/
setVisible: function (visible) {
if(this._visible !== visible){
this._visible = visible;
//if(visible)
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
cc.renderer.childrenOrderDirty = true;
}
},
/**
* <p>Returns a copy of the anchor point.<br/>
* Anchor point is the point around which all transformations and positioning manipulations take place.<br/>
* It's like a pin in the node where it is "attached" to its parent. <br/>
* The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner. <br/>
* But you can use values higher than (1,1) and lower than (0,0) too. <br/>
* The default anchor point is (0.5,0.5), so it starts at the center of the node. <br/></p>
* @function
* @return {cc.Point} The anchor point of node.
*/
getAnchorPoint: function () {
return cc.p(this._anchorPoint);
},
/**
* <p>
* Sets the anchor point in percent. <br/>
* <br/>
* anchor point is the point around which all transformations and positioning manipulations take place. <br/>
* It's like a pin in the node where it is "attached" to its parent. <br/>
* The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner. <br/>
* But you can use values higher than (1,1) and lower than (0,0) too. <br/>
* The default anchor point is (0.5,0.5), so it starts at the center of the node.
* </p>
* @function
* @param {cc.Point|Number} point The anchor point of node or The x axis anchor of node.
* @param {Number} [y] The y axis anchor of node.
*/
setAnchorPoint: function (point, y) {
var locAnchorPoint = this._anchorPoint;
if (y === undefined) {
if ((point.x === locAnchorPoint.x) && (point.y === locAnchorPoint.y))
return;
locAnchorPoint.x = point.x;
locAnchorPoint.y = point.y;
} else {
if ((point === locAnchorPoint.x) && (y === locAnchorPoint.y))
return;
locAnchorPoint.x = point;
locAnchorPoint.y = y;
}
this._renderCmd._updateAnchorPointInPoint();
},
_getAnchorX: function () {
return this._anchorPoint.x;
},
_setAnchorX: function (x) {
if (this._anchorPoint.x === x) return;
this._anchorPoint.x = x;
this._renderCmd._updateAnchorPointInPoint();
},
_getAnchorY: function () {
return this._anchorPoint.y;
},
_setAnchorY: function (y) {
if (this._anchorPoint.y === y) return;
this._anchorPoint.y = y;
this._renderCmd._updateAnchorPointInPoint();
},
/**
* Returns a copy of the anchor point in absolute pixels. <br/>
* you can only read it. If you wish to modify it, use setAnchorPoint
* @see cc.Node#getAnchorPoint
* @function
* @return {cc.Point} The anchor point in absolute pixels.
*/
getAnchorPointInPoints: function () {
return this._renderCmd.getAnchorPointInPoints();
},
_getWidth: function () {
return this._contentSize.width;
},
_setWidth: function (width) {
this._contentSize.width = width;
this._renderCmd._updateAnchorPointInPoint();
},
_getHeight: function () {
return this._contentSize.height;
},
_setHeight: function (height) {
this._contentSize.height = height;
this._renderCmd._updateAnchorPointInPoint();
},
/**
* <p>Returns a copy the untransformed size of the node. <br/>
* The contentSize remains the same no matter the node is scaled or rotated.<br/>
* All nodes has a size. Layer and Scene has the same size of the screen by default. <br/></p>
* @function
* @return {cc.Size} The untransformed size of the node.
*/
getContentSize: function () {
return cc.size(this._contentSize);
},
/**
* <p>
* Sets the untransformed size of the node. <br/>
* <br/>
* The contentSize remains the same no matter the node is scaled or rotated. <br/>
* All nodes has a size. Layer and Scene has the same size of the screen.
* </p>
* @function
* @param {cc.Size|Number} size The untransformed size of the node or The untransformed size's width of the node.
* @param {Number} [height] The untransformed size's height of the node.
*/
setContentSize: function (size, height) {
var locContentSize = this._contentSize;
if (height === undefined) {
if ((size.width === locContentSize.width) && (size.height === locContentSize.height))
return;
locContentSize.width = size.width;
locContentSize.height = size.height;
} else {
if ((size === locContentSize.width) && (height === locContentSize.height))
return;
locContentSize.width = size;
locContentSize.height = height;
}
this._renderCmd._updateAnchorPointInPoint();
},
/**
* <p>
* Returns whether or not the node accepts event callbacks. <br/>
* Running means the node accept event callbacks like onEnter(), onExit(), update()
* </p>
* @function
* @return {Boolean} Whether or not the node is running.
*/
isRunning: function () {
return this._running;
},
/**
* Returns a reference to the parent node
* @function
* @return {cc.Node} A reference to the parent node
*/
getParent: function () {
return this._parent;
},
/**
* Sets the parent node
* @param {cc.Node} parent A reference to the parent node
*/
setParent: function (parent) {
this._parent = parent;
},
/**
* Returns whether the anchor point will be ignored when you position this node.<br/>
* When anchor point ignored, position will be calculated based on the origin point (0, 0) in parent's coordinates.
* @function
* @see cc.Node#ignoreAnchorPointForPosition
* @return {Boolean} true if the anchor point will be ignored when you position this node.
*/
isIgnoreAnchorPointForPosition: function () {
return this._ignoreAnchorPointForPosition;
},
/**
* <p>
* Sets whether the anchor point will be ignored when you position this node. <br/>
* When anchor point ignored, position will be calculated based on the origin point (0, 0) in parent's coordinates. <br/>
* This is an internal method, only used by CCLayer and CCScene. Don't call it outside framework. <br/>
* The default value is false, while in CCLayer and CCScene are true
* </p>
* @function
* @param {Boolean} newValue true if anchor point will be ignored when you position this node
*/
ignoreAnchorPointForPosition: function (newValue) {
if (newValue !== this._ignoreAnchorPointForPosition) {
this._ignoreAnchorPointForPosition = newValue;
this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
}
},
/**
* Returns a tag that is used to identify the node easily.
* @function
* @return {Number} An integer that identifies the node.
* @example
* //You can set tags to node then identify them easily.
* // set tags
* node1.setTag(TAG_PLAYER);
* node2.setTag(TAG_MONSTER);
* node3.setTag(TAG_BOSS);
* parent.addChild(node1);
* parent.addChild(node2);
* parent.addChild(node3);
* // identify by tags
* var allChildren = parent.getChildren();
* for(var i = 0; i < allChildren.length; i++){
* switch(node.getTag()) {
* case TAG_PLAYER:
* break;
* case TAG_MONSTER:
* break;
* case TAG_BOSS:
* break;