CCNode.js 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * Default Node tag
  24. * @constant
  25. * @type Number
  26. */
  27. cc.NODE_TAG_INVALID = -1;
  28. /**
  29. * 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.
  30. */
  31. cc.s_globalOrderOfArrival = 1;
  32. /**
  33. * <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/>
  34. * The most popular cc.Nodes are: cc.Scene, cc.Layer, cc.Sprite, cc.Menu.</p>
  35. *
  36. * <p>The main features of a cc.Node are: <br/>
  37. * - They can contain other cc.Node nodes (addChild, getChildByTag, removeChild, etc) <br/>
  38. * - They can schedule periodic callback (schedule, unschedule, etc) <br/>
  39. * - They can execute actions (runAction, stopAction, etc) <br/></p>
  40. *
  41. * <p>Some cc.Node nodes provide extra functionality for them or their children.</p>
  42. *
  43. * <p>Subclassing a cc.Node usually means (one/all) of: <br/>
  44. * - overriding constructor function "ctor" to initialize resources and schedule callbacks<br/>
  45. * - create callbacks to handle the advancement of time<br/></p>
  46. *
  47. * <p>Features of cc.Node: <br/>
  48. * - position <br/>
  49. * - scale (x, y) <br/>
  50. * - rotation (in degrees, clockwise)<br/>
  51. * - anchor point<br/>
  52. * - size <br/>
  53. * - color <br/>
  54. * - opacity <br/>
  55. * - visible<br/>
  56. * - z-order<br/>
  57. * - WebGL z position<br/></P>
  58. *
  59. * <p> Default values: <br/>
  60. * - rotation: 0 <br/>
  61. * - position: (x=0,y=0) <br/>
  62. * - scale: (x=1,y=1) <br/>
  63. * - contentSize: (x=0,y=0)<br/>
  64. * - anchorPoint: (x=0,y=0)<br/>
  65. * - color: (r=255,g=255,b=255)<br/>
  66. * - opacity: 255</p>
  67. *
  68. * <p> Limitations:<br/>
  69. * - A cc.Node is a "void" object. It doesn't have a texture <br/></P>
  70. *
  71. * <p>Order in transformations with grid disabled <br/>
  72. * -# The node will be translated (position) <br/>
  73. * -# The node will be rotated (rotation)<br/>
  74. * -# The node will be scaled (scale) <br/>
  75. *
  76. * <p>Order in transformations with grid enabled<br/>
  77. * -# The node will be translated (position)<br/>
  78. * -# The node will be rotated (rotation) <br/>
  79. * -# The node will be scaled (scale) <br/>
  80. * -# The grid will capture the screen <br/>
  81. * -# The node will be moved according to the camera values (camera) <br/>
  82. * -# The grid will render the captured screen <br/></P>
  83. *
  84. * @class
  85. * @extends cc.Class
  86. *
  87. * @property {Number} x - x axis position of node
  88. * @property {Number} y - y axis position of node
  89. * @property {Number} width - Width of node
  90. * @property {Number} height - Height of node
  91. * @property {Number} anchorX - Anchor point's position on x axis
  92. * @property {Number} anchorY - Anchor point's position on y axis
  93. * @property {Boolean} ignoreAnchor - Indicate whether ignore the anchor point property for positioning
  94. * @property {Number} skewX - Skew x
  95. * @property {Number} skewY - Skew y
  96. * @property {Number} zIndex - Z order in depth which stands for the drawing order
  97. * @property {Number} vertexZ - WebGL Z vertex of this node, z order works OK if all the nodes uses the same openGL Z vertex
  98. * @property {Number} rotation - Rotation of node
  99. * @property {Number} rotationX - Rotation on x axis
  100. * @property {Number} rotationY - Rotation on y axis
  101. * @property {Number} scale - Scale of node
  102. * @property {Number} scaleX - Scale on x axis
  103. * @property {Number} scaleY - Scale on y axis
  104. * @property {Boolean} visible - Indicate whether node is visible or not
  105. * @property {cc.Color} color - Color of node, default value is white: (255, 255, 255)
  106. * @property {Boolean} cascadeColor - Indicate whether node's color value affect its child nodes, default value is false
  107. * @property {Number} opacity - Opacity of node, default value is 255
  108. * @property {Boolean} opacityModifyRGB - Indicate whether opacity affect the color value, default value is false
  109. * @property {Boolean} cascadeOpacity - Indicate whether node's opacity value affect its child nodes, default value is false
  110. * @property {Array} children - <@readonly> All children nodes
  111. * @property {Number} childrenCount - <@readonly> Number of children
  112. * @property {cc.Node} parent - Parent node
  113. * @property {Boolean} running - <@readonly> Indicate whether node is running or not
  114. * @property {Number} tag - Tag of node
  115. * @property {Object} userData - Custom user data
  116. * @property {Object} userObject - User assigned CCObject, similar to userData, but instead of holding a void* it holds an id
  117. * @property {Number} arrivalOrder - The arrival order, indicates which children is added previously
  118. * @property {cc.ActionManager} actionManager - The CCActionManager object that is used by all actions.
  119. * @property {cc.Scheduler} scheduler - cc.Scheduler used to schedule all "updates" and timers.
  120. * @property {cc.GridBase} grid - grid object that is used when applying effects
  121. * @property {cc.GLProgram} shaderProgram - The shader program currently used for this node
  122. * @property {Number} glServerState - The state of OpenGL server side
  123. */
  124. cc.Node = cc.Class.extend(/** @lends cc.Node# */{
  125. _localZOrder: 0, ///< Local order (relative to its siblings) used to sort the node
  126. _globalZOrder: 0, ///< Global order used to sort the node
  127. _vertexZ: 0.0,
  128. _rotationX: 0,
  129. _rotationY: 0.0,
  130. _scaleX: 1.0,
  131. _scaleY: 1.0,
  132. _position: null,
  133. _skewX: 0.0,
  134. _skewY: 0.0,
  135. // children (lazy allocs),
  136. _children: null,
  137. // lazy alloc,
  138. _visible: true,
  139. _anchorPoint: null,
  140. _anchorPointInPoints: null,
  141. _contentSize: null,
  142. _running: false,
  143. _parent: null,
  144. // "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true
  145. _ignoreAnchorPointForPosition: false,
  146. tag: cc.NODE_TAG_INVALID,
  147. // userData is always inited as nil
  148. userData: null,
  149. userObject: null,
  150. _transformDirty: true,
  151. _inverseDirty: true,
  152. _cacheDirty: true,
  153. // Cached parent serves to construct the cached parent chain
  154. _cachedParent: null,
  155. _transformGLDirty: null,
  156. _transform: null,
  157. _inverse: null,
  158. //since 2.0 api
  159. _reorderChildDirty: false,
  160. _shaderProgram: null,
  161. arrivalOrder: 0,
  162. _actionManager: null,
  163. _scheduler: null,
  164. _eventDispatcher: null,
  165. _initializedNode: false,
  166. _additionalTransformDirty: false,
  167. _additionalTransform: null,
  168. _componentContainer: null,
  169. _isTransitionFinished: false,
  170. _rotationRadiansX: 0,
  171. _rotationRadiansY: 0,
  172. _className: "Node",
  173. _showNode: false,
  174. _name: "", ///<a string label, an user defined string to identify this node
  175. _displayedOpacity: 255,
  176. _realOpacity: 255,
  177. _displayedColor: null,
  178. _realColor: null,
  179. _cascadeColorEnabled: false,
  180. _cascadeOpacityEnabled: false,
  181. _usingNormalizedPosition: false,
  182. _hashOfName: 0,
  183. _initNode: function () {
  184. var _t = this;
  185. _t._anchorPoint = cc.p(0, 0);
  186. _t._anchorPointInPoints = cc.p(0, 0);
  187. _t._contentSize = cc.size(0, 0);
  188. _t._position = cc.p(0, 0);
  189. _t._children = [];
  190. _t._transform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0};
  191. var director = cc.director;
  192. _t._actionManager = director.getActionManager();
  193. _t._scheduler = director.getScheduler();
  194. _t._initializedNode = true;
  195. _t._additionalTransform = cc.affineTransformMakeIdentity();
  196. if (cc.ComponentContainer) {
  197. _t._componentContainer = new cc.ComponentContainer(_t);
  198. }
  199. this._displayedOpacity = 255;
  200. this._realOpacity = 255;
  201. this._displayedColor = cc.color(255, 255, 255, 255);
  202. this._realColor = cc.color(255, 255, 255, 255);
  203. this._cascadeColorEnabled = false;
  204. this._cascadeOpacityEnabled = false;
  205. },
  206. /**
  207. * Initializes the instance of cc.Node
  208. * @function
  209. * @returns {boolean} Whether the initialization was successful.
  210. */
  211. init: function () {
  212. if (this._initializedNode === false)
  213. this._initNode();
  214. return true;
  215. },
  216. _arrayMakeObjectsPerformSelector: function (array, callbackType) {
  217. if (!array || array.length === 0)
  218. return;
  219. var i, len = array.length, node;
  220. var nodeCallbackType = cc.Node._StateCallbackType;
  221. switch (callbackType) {
  222. case nodeCallbackType.onEnter:
  223. for (i = 0; i < len; i++) {
  224. node = array[i];
  225. if (node)
  226. node.onEnter();
  227. }
  228. break;
  229. case nodeCallbackType.onExit:
  230. for (i = 0; i < len; i++) {
  231. node = array[i];
  232. if (node)
  233. node.onExit();
  234. }
  235. break;
  236. case nodeCallbackType.onEnterTransitionDidFinish:
  237. for (i = 0; i < len; i++) {
  238. node = array[i];
  239. if (node)
  240. node.onEnterTransitionDidFinish();
  241. }
  242. break;
  243. case nodeCallbackType.cleanup:
  244. for (i = 0; i < len; i++) {
  245. node = array[i];
  246. if (node)
  247. node.cleanup();
  248. }
  249. break;
  250. case nodeCallbackType.updateTransform:
  251. for (i = 0; i < len; i++) {
  252. node = array[i];
  253. if (node)
  254. node.updateTransform();
  255. }
  256. break;
  257. case nodeCallbackType.onExitTransitionDidStart:
  258. for (i = 0; i < len; i++) {
  259. node = array[i];
  260. if (node)
  261. node.onExitTransitionDidStart();
  262. }
  263. break;
  264. case nodeCallbackType.sortAllChildren:
  265. for (i = 0; i < len; i++) {
  266. node = array[i];
  267. if (node)
  268. node.sortAllChildren();
  269. }
  270. break;
  271. default :
  272. cc.assert(0, cc._LogInfos.Node__arrayMakeObjectsPerformSelector);
  273. break;
  274. }
  275. },
  276. /**
  277. * Sets node's dirty flag to true so that it can be updated in visit function of the next frame
  278. * @function
  279. */
  280. setNodeDirty: null,
  281. /**
  282. * <p>Properties configuration function </br>
  283. * All properties in attrs will be set to the node, </br>
  284. * when the setter of the node is available, </br>
  285. * the property will be set via setter function.</br>
  286. * </p>
  287. * @function
  288. * @param {Object} attrs Properties to be set to node
  289. */
  290. attr: function (attrs) {
  291. for (var key in attrs) {
  292. this[key] = attrs[key];
  293. }
  294. },
  295. /**
  296. * <p>Returns the skew degrees in X </br>
  297. * The X skew angle of the node in degrees. <br/>
  298. * This angle describes the shear distortion in the X direction.<br/>
  299. * Thus, it is the angle between the Y axis and the left edge of the shape </br>
  300. * The default skewX angle is 0. Positive values distort the node in a CW direction.</br>
  301. * </p>
  302. * @function
  303. * @return {Number} The X skew angle of the node in degrees.
  304. */
  305. getSkewX: function () {
  306. return this._skewX;
  307. },
  308. /**
  309. * <p>
  310. * Changes the X skew angle of the node in degrees. <br/>
  311. * <br/>
  312. * This angle describes the shear distortion in the X direction. <br/>
  313. * Thus, it is the angle between the Y axis and the left edge of the shape <br/>
  314. * The default skewX angle is 0. Positive values distort the node in a CW direction.
  315. * </p>
  316. * @function
  317. * @param {Number} newSkewX The X skew angle of the node in degrees.
  318. */
  319. setSkewX: function (newSkewX) {
  320. this._skewX = newSkewX;
  321. this.setNodeDirty();
  322. },
  323. /**
  324. * <p>Returns the skew degrees in Y <br/>
  325. * The Y skew angle of the node in degrees. <br/>
  326. * This angle describes the shear distortion in the Y direction. <br/>
  327. * Thus, it is the angle between the X axis and the bottom edge of the shape <br/>
  328. * The default skewY angle is 0. Positive values distort the node in a CCW direction. <br/>
  329. * </p>
  330. * @function
  331. * @return {Number} The Y skew angle of the node in degrees.
  332. */
  333. getSkewY: function () {
  334. return this._skewY;
  335. },
  336. /**
  337. * <p>
  338. * Changes the Y skew angle of the node in degrees. <br/>
  339. * <br/>
  340. * This angle describes the shear distortion in the Y direction. <br/>
  341. * Thus, it is the angle between the X axis and the bottom edge of the shape <br/>
  342. * The default skewY angle is 0. Positive values distort the node in a CCW direction. <br/>
  343. * </p>
  344. * @function
  345. * @param {Number} newSkewY The Y skew angle of the node in degrees.
  346. */
  347. setSkewY: function (newSkewY) {
  348. this._skewY = newSkewY;
  349. this.setNodeDirty();
  350. },
  351. /**
  352. * <p> LocalZOrder is the 'key' used to sort the node relative to its siblings. <br/>
  353. * <br/>
  354. * The Node's parent will sort all its children based ont the LocalZOrder value. <br/>
  355. * If two nodes have the same LocalZOrder, then the node that was added first to the children's array <br/>
  356. * will be in front of the other node in the array. <br/>
  357. * <br/>
  358. * Also, the Scene Graph is traversed using the "In-Order" tree traversal algorithm ( http://en.wikipedia.org/wiki/Tree_traversal#In-order )
  359. * <br/>
  360. * And Nodes that have LocalZOder values < 0 are the "left" subtree <br/>
  361. * While Nodes with LocalZOder >=0 are the "right" subtree. </p>
  362. * @function
  363. * @param {Number} localZOrder
  364. */
  365. setLocalZOrder: function (localZOrder) {
  366. this._localZOrder = localZOrder;
  367. if (this._parent)
  368. this._parent.reorderChild(this, localZOrder);
  369. cc.eventManager._setDirtyForNode(this);
  370. },
  371. //Helper function used by `setLocalZOrder`. Don't use it unless you know what you are doing.
  372. _setLocalZOrder: function (localZOrder) {
  373. this._localZOrder = localZOrder;
  374. },
  375. /**
  376. * Returns the local Z order of this node.
  377. * @function
  378. * @returns {Number} The local (relative to its siblings) Z order.
  379. */
  380. getLocalZOrder: function () {
  381. return this._localZOrder;
  382. },
  383. /**
  384. * Returns z order of this node
  385. * @function
  386. * @return {Number}
  387. * @deprecated since 3.0, please use getLocalZOrder instead
  388. */
  389. getZOrder: function () {
  390. cc.log(cc._LogInfos.Node_getZOrder);
  391. return this.getLocalZOrder();
  392. },
  393. /**
  394. * <p>
  395. * Sets the Z order which stands for the drawing order, and reorder this node in its parent's children array. <br/>
  396. * <br/>
  397. * The Z order of node is relative to its "brothers": children of the same parent. <br/>
  398. * It's nothing to do with OpenGL's z vertex. This one only affects the draw order of nodes in cocos2d. <br/>
  399. * The larger number it is, the later this node will be drawn in each message loop. <br/>
  400. * Please refer to setVertexZ(float) for the difference.
  401. * </p>
  402. * @function
  403. * @param {Number} z Z order of this node.
  404. * @deprecated since 3.0, please use setLocalZOrder instead
  405. */
  406. setZOrder: function (z) {
  407. cc.log(cc._LogInfos.Node_setZOrder);
  408. this.setLocalZOrder(z);
  409. },
  410. /**
  411. * <p>Defines the oder in which the nodes are renderer. <br/>
  412. * Nodes that have a Global Z Order lower, are renderer first. <br/>
  413. * <br/>
  414. * In case two or more nodes have the same Global Z Order, the oder is not guaranteed. <br/>
  415. * The only exception if the Nodes have a Global Z Order == 0. In that case, the Scene Graph order is used. <br/>
  416. * <br/>
  417. * 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/>
  418. * <br/>
  419. * Global Z Order is useful when you need to render nodes in an order different than the Scene Graph order. <br/>
  420. * <br/>
  421. * Limitations: Global Z Order can't be used used by Nodes that have SpriteBatchNode as one of their ancestors. <br/>
  422. * And if ClippingNode is one of the ancestors, then "global Z order" will be relative to the ClippingNode. </p>
  423. * @function
  424. * @param {Number} globalZOrder
  425. */
  426. setGlobalZOrder: function (globalZOrder) {
  427. if (this._globalZOrder != globalZOrder) {
  428. this._globalZOrder = globalZOrder;
  429. cc.eventManager._setDirtyForNode(this);
  430. }
  431. },
  432. /**
  433. * Return the Node's Global Z Order.
  434. * @function
  435. * @returns {number} The node's global Z order
  436. */
  437. getGlobalZOrder: function () {
  438. return this._globalZOrder;
  439. },
  440. /**
  441. * Returns WebGL Z vertex of this node.
  442. * @function
  443. * @return {Number} WebGL Z vertex of this node
  444. */
  445. getVertexZ: function () {
  446. return this._vertexZ;
  447. },
  448. /**
  449. * <p>
  450. * Sets the real WebGL Z vertex. <br/>
  451. * <br/>
  452. * Differences between openGL Z vertex and cocos2d Z order: <br/>
  453. * - WebGL Z modifies the Z vertex, and not the Z order in the relation between parent-children <br/>
  454. * - WebGL Z might require to set 2D projection <br/>
  455. * - cocos2d Z order works OK if all the nodes uses the same WebGL Z vertex. eg: vertexZ = 0 <br/>
  456. * <br/>
  457. * @warning Use it at your own risk since it might break the cocos2d parent-children z order
  458. * </p>
  459. * @function
  460. * @param {Number} Var
  461. */
  462. setVertexZ: function (Var) {
  463. this._vertexZ = Var;
  464. },
  465. /**
  466. * Returns the rotation (angle) of the node in degrees. 0 is the default rotation angle. Positive values rotate node clockwise.
  467. * @function
  468. * @return {Number} The rotation of the node in degrees.
  469. */
  470. getRotation: function () {
  471. if (this._rotationX !== this._rotationY)
  472. cc.log(cc._LogInfos.Node_getRotation);
  473. return this._rotationX;
  474. },
  475. /**
  476. * <p>
  477. * Sets the rotation (angle) of the node in degrees. <br/>
  478. * <br/>
  479. * 0 is the default rotation angle. <br/>
  480. * Positive values rotate node clockwise, and negative values for anti-clockwise.
  481. * </p>
  482. * @function
  483. * @param {Number} newRotation The rotation of the node in degrees.
  484. */
  485. setRotation: function (newRotation) {
  486. this._rotationX = this._rotationY = newRotation;
  487. this._rotationRadiansX = this._rotationX * 0.017453292519943295; //(Math.PI / 180);
  488. this._rotationRadiansY = this._rotationY * 0.017453292519943295; //(Math.PI / 180);
  489. this.setNodeDirty();
  490. },
  491. /**
  492. * Returns the X axis rotation (angle) which represent a horizontal rotational skew of the node in degrees. <br/>
  493. * 0 is the default rotation angle. Positive values rotate node clockwise<br/>
  494. * (support only in WebGL rendering mode)
  495. * @function
  496. * @return {Number} The X rotation in degrees.
  497. */
  498. getRotationX: function () {
  499. return this._rotationX;
  500. },
  501. /**
  502. * <p>
  503. * Sets the X rotation (angle) of the node in degrees which performs a horizontal rotational skew. <br/>
  504. * (support only in WebGL rendering mode) <br/>
  505. * 0 is the default rotation angle. <br/>
  506. * Positive values rotate node clockwise, and negative values for anti-clockwise.
  507. * </p>
  508. * @param {Number} rotationX The X rotation in degrees which performs a horizontal rotational skew.
  509. */
  510. setRotationX: function (rotationX) {
  511. this._rotationX = rotationX;
  512. this._rotationRadiansX = this._rotationX * 0.017453292519943295; //(Math.PI / 180);
  513. this.setNodeDirty();
  514. },
  515. /**
  516. * Returns the Y axis rotation (angle) which represent a vertical rotational skew of the node in degrees. <br/>
  517. * 0 is the default rotation angle. Positive values rotate node clockwise<br/>
  518. * (support only in WebGL rendering mode)
  519. * @function
  520. * @return {Number} The Y rotation in degrees.
  521. */
  522. getRotationY: function () {
  523. return this._rotationY;
  524. },
  525. /**
  526. * <p>
  527. * Sets the Y rotation (angle) of the node in degrees which performs a vertical rotational skew. <br/>
  528. * (support only in WebGL rendering mode) <br/>
  529. * 0 is the default rotation angle. <br/>
  530. * Positive values rotate node clockwise, and negative values for anti-clockwise.
  531. * </p>
  532. * @param rotationY The Y rotation in degrees.
  533. */
  534. setRotationY: function (rotationY) {
  535. this._rotationY = rotationY;
  536. this._rotationRadiansY = this._rotationY * 0.017453292519943295; //(Math.PI / 180);
  537. this.setNodeDirty();
  538. },
  539. /**
  540. * Returns the scale factor of the node.
  541. * @warning: Assertion will fail when _scaleX != _scaleY.
  542. * @function
  543. * @return {Number} The scale factor
  544. */
  545. getScale: function () {
  546. if (this._scaleX !== this._scaleY)
  547. cc.log(cc._LogInfos.Node_getScale);
  548. return this._scaleX;
  549. },
  550. /**
  551. * 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.
  552. * @function
  553. * @param {Number} scale or scaleX value
  554. * @param {Number} [scaleY=]
  555. */
  556. setScale: function (scale, scaleY) {
  557. this._scaleX = scale;
  558. this._scaleY = (scaleY || scaleY === 0) ? scaleY : scale;
  559. this.setNodeDirty();
  560. },
  561. /**
  562. * Returns the scale factor on X axis of this node
  563. * @function
  564. * @return {Number} The scale factor on X axis.
  565. */
  566. getScaleX: function () {
  567. return this._scaleX;
  568. },
  569. /**
  570. * <p>
  571. * Changes the scale factor on X axis of this node <br/>
  572. * The deafult value is 1.0 if you haven't changed it before
  573. * </p>
  574. * @function
  575. * @param {Number} newScaleX The scale factor on X axis.
  576. */
  577. setScaleX: function (newScaleX) {
  578. this._scaleX = newScaleX;
  579. this.setNodeDirty();
  580. },
  581. /**
  582. * Returns the scale factor on Y axis of this node
  583. * @function
  584. * @return {Number} The scale factor on Y axis.
  585. */
  586. getScaleY: function () {
  587. return this._scaleY;
  588. },
  589. /**
  590. * <p>
  591. * Changes the scale factor on Y axis of this node <br/>
  592. * The Default value is 1.0 if you haven't changed it before.
  593. * </p>
  594. * @function
  595. * @param {Number} newScaleY The scale factor on Y axis.
  596. */
  597. setScaleY: function (newScaleY) {
  598. this._scaleY = newScaleY;
  599. this.setNodeDirty();
  600. },
  601. /**
  602. * <p>
  603. * Changes the position (x,y) of the node in cocos2d coordinates.<br/>
  604. * The original point (0,0) is at the left-bottom corner of screen.<br/>
  605. * Usually we use cc.p(x,y) to compose CCPoint object.<br/>
  606. * and Passing two numbers (x,y) is more efficient than passing CCPoint object.
  607. * </p>
  608. * @function
  609. * @param {cc.Point|Number} newPosOrxValue The position (x,y) of the node in coordinates or the X coordinate for position
  610. * @param {Number} [yValue] Y coordinate for position
  611. * @example
  612. * var size = cc.winSize;
  613. * node.setPosition(size.width/2, size.height/2);
  614. */
  615. setPosition: function (newPosOrxValue, yValue) {
  616. var locPosition = this._position;
  617. if (yValue === undefined) {
  618. locPosition.x = newPosOrxValue.x;
  619. locPosition.y = newPosOrxValue.y;
  620. } else {
  621. locPosition.x = newPosOrxValue;
  622. locPosition.y = yValue;
  623. }
  624. this.setNodeDirty();
  625. },
  626. /**
  627. * <p>Returns a copy of the position (x,y) of the node in cocos2d coordinates. (0,0) is the left-bottom corner.</p>
  628. * @function
  629. * @return {cc.Point} The position (x,y) of the node in OpenGL coordinates
  630. */
  631. getPosition: function () {
  632. return cc.p(this._position);
  633. },
  634. /**
  635. * <p>Returns the x axis position of the node in cocos2d coordinates.</p>
  636. * @function
  637. * @return {Number}
  638. */
  639. getPositionX: function () {
  640. return this._position.x;
  641. },
  642. /**
  643. * <p>Sets the x axis position of the node in cocos2d coordinates.</p>
  644. * @function
  645. * @param {Number} x The new position in x axis
  646. */
  647. setPositionX: function (x) {
  648. this._position.x = x;
  649. this.setNodeDirty();
  650. },
  651. /**
  652. * <p>Returns the y axis position of the node in cocos2d coordinates.</p>
  653. * @function
  654. * @return {Number}
  655. */
  656. getPositionY: function () {
  657. return this._position.y;
  658. },
  659. /**
  660. * <p>Sets the y axis position of the node in cocos2d coordinates.</p>
  661. * @function
  662. * @param {Number} y The new position in y axis
  663. */
  664. setPositionY: function (y) {
  665. this._position.y = y;
  666. this.setNodeDirty();
  667. },
  668. /**
  669. * Returns the amount of children.
  670. * @function
  671. * @return {Number} The amount of children.
  672. */
  673. getChildrenCount: function () {
  674. return this._children.length;
  675. },
  676. /**
  677. * Returns an array of all children <br/>
  678. * Composing a "tree" structure is a very important feature of CCNode
  679. * @function
  680. * @return {Array} An array of children
  681. * @example
  682. * //This sample code traverses all children nodes, and set their position to (0,0)
  683. * var allChildren = parent.getChildren();
  684. * for(var i = 0; i< allChildren.length; i++) {
  685. * allChildren[i].setPosition(0,0);
  686. * }
  687. */
  688. getChildren: function () {
  689. return this._children;
  690. },
  691. /**
  692. * Returns if the node is visible
  693. * @function
  694. * @see cc.Node#setVisible
  695. * @return {Boolean} true if the node is visible, false if the node is hidden.
  696. */
  697. isVisible: function () {
  698. return this._visible;
  699. },
  700. /**
  701. * Sets whether the node is visible <br/>
  702. * The default value is true
  703. * @function
  704. * @param {Boolean} visible Pass true to make the node visible, false to hide the node.
  705. */
  706. setVisible: function (visible) {
  707. if(this._visible != visible){
  708. this._visible = visible;
  709. if(visible) this.setNodeDirty();
  710. }
  711. },
  712. /**
  713. * <p>Returns a copy of the anchor point.<br/>
  714. * Anchor point is the point around which all transformations and positioning manipulations take place.<br/>
  715. * It's like a pin in the node where it is "attached" to its parent. <br/>
  716. * The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner. <br/>
  717. * But you can use values higher than (1,1) and lower than (0,0) too. <br/>
  718. * The default anchor point is (0.5,0.5), so it starts at the center of the node. <br/></p>
  719. * @function
  720. * @return {cc.Point} The anchor point of node.
  721. */
  722. getAnchorPoint: function () {
  723. return cc.p(this._anchorPoint);
  724. },
  725. /**
  726. * <p>
  727. * Sets the anchor point in percent. <br/>
  728. * <br/>
  729. * anchor point is the point around which all transformations and positioning manipulations take place. <br/>
  730. * It's like a pin in the node where it is "attached" to its parent. <br/>
  731. * The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner. <br/>
  732. * But you can use values higher than (1,1) and lower than (0,0) too. <br/>
  733. * The default anchor point is (0.5,0.5), so it starts at the center of the node.
  734. * </p>
  735. * @function
  736. * @param {cc.Point|Number} point The anchor point of node or The x axis anchor of node.
  737. * @param {Number} [y] The y axis anchor of node.
  738. */
  739. setAnchorPoint: function (point, y) {
  740. var locAnchorPoint = this._anchorPoint;
  741. if (y === undefined) {
  742. if ((point.x === locAnchorPoint.x) && (point.y === locAnchorPoint.y))
  743. return;
  744. locAnchorPoint.x = point.x;
  745. locAnchorPoint.y = point.y;
  746. } else {
  747. if ((point === locAnchorPoint.x) && (y === locAnchorPoint.y))
  748. return;
  749. locAnchorPoint.x = point;
  750. locAnchorPoint.y = y;
  751. }
  752. var locAPP = this._anchorPointInPoints, locSize = this._contentSize;
  753. locAPP.x = locSize.width * locAnchorPoint.x;
  754. locAPP.y = locSize.height * locAnchorPoint.y;
  755. this.setNodeDirty();
  756. },
  757. _getAnchor: function () {
  758. return this._anchorPoint;
  759. },
  760. _setAnchor: function (p) {
  761. var x = p.x, y = p.y;
  762. if (this._anchorPoint.x !== x) {
  763. this._anchorPoint.x = x;
  764. this._anchorPointInPoints.x = this._contentSize.width * x;
  765. }
  766. if (this._anchorPoint.y !== y) {
  767. this._anchorPoint.y = y;
  768. this._anchorPointInPoints.y = this._contentSize.height * y;
  769. }
  770. this.setNodeDirty();
  771. },
  772. _getAnchorX: function () {
  773. return this._anchorPoint.x;
  774. },
  775. _setAnchorX: function (x) {
  776. if (this._anchorPoint.x === x) return;
  777. this._anchorPoint.x = x;
  778. this._anchorPointInPoints.x = this._contentSize.width * x;
  779. this.setNodeDirty();
  780. },
  781. _getAnchorY: function () {
  782. return this._anchorPoint.y;
  783. },
  784. _setAnchorY: function (y) {
  785. if (this._anchorPoint.y === y) return;
  786. this._anchorPoint.y = y;
  787. this._anchorPointInPoints.y = this._contentSize.height * y;
  788. this.setNodeDirty();
  789. },
  790. /**
  791. * Returns a copy of the anchor point in absolute pixels. <br/>
  792. * you can only read it. If you wish to modify it, use setAnchorPoint
  793. * @see cc.Node#getAnchorPoint
  794. * @function
  795. * @return {cc.Point} The anchor point in absolute pixels.
  796. */
  797. getAnchorPointInPoints: function () {
  798. return cc.p(this._anchorPointInPoints);
  799. },
  800. _getWidth: function () {
  801. return this._contentSize.width;
  802. },
  803. _setWidth: function (width) {
  804. this._contentSize.width = width;
  805. this._anchorPointInPoints.x = width * this._anchorPoint.x;
  806. this.setNodeDirty();
  807. },
  808. _getHeight: function () {
  809. return this._contentSize.height;
  810. },
  811. _setHeight: function (height) {
  812. this._contentSize.height = height;
  813. this._anchorPointInPoints.y = height * this._anchorPoint.y;
  814. this.setNodeDirty();
  815. },
  816. /**
  817. * <p>Returns a copy the untransformed size of the node. <br/>
  818. * The contentSize remains the same no matter the node is scaled or rotated.<br/>
  819. * All nodes has a size. Layer and Scene has the same size of the screen by default. <br/></p>
  820. * @function
  821. * @return {cc.Size} The untransformed size of the node.
  822. */
  823. getContentSize: function () {
  824. return cc.size(this._contentSize);
  825. },
  826. /**
  827. * <p>
  828. * Sets the untransformed size of the node. <br/>
  829. * <br/>
  830. * The contentSize remains the same no matter the node is scaled or rotated. <br/>
  831. * All nodes has a size. Layer and Scene has the same size of the screen.
  832. * </p>
  833. * @function
  834. * @param {cc.Size|Number} size The untransformed size of the node or The untransformed size's width of the node.
  835. * @param {Number} [height] The untransformed size's height of the node.
  836. */
  837. setContentSize: function (size, height) {
  838. var locContentSize = this._contentSize;
  839. if (height === undefined) {
  840. if ((size.width === locContentSize.width) && (size.height === locContentSize.height))
  841. return;
  842. locContentSize.width = size.width;
  843. locContentSize.height = size.height;
  844. } else {
  845. if ((size === locContentSize.width) && (height === locContentSize.height))
  846. return;
  847. locContentSize.width = size;
  848. locContentSize.height = height;
  849. }
  850. var locAPP = this._anchorPointInPoints, locAnchorPoint = this._anchorPoint;
  851. locAPP.x = locContentSize.width * locAnchorPoint.x;
  852. locAPP.y = locContentSize.height * locAnchorPoint.y;
  853. this.setNodeDirty();
  854. },
  855. /**
  856. * <p>
  857. * Returns whether or not the node accepts event callbacks. <br/>
  858. * Running means the node accept event callbacks like onEnter(), onExit(), update()
  859. * </p>
  860. * @function
  861. * @return {Boolean} Whether or not the node is running.
  862. */
  863. isRunning: function () {
  864. return this._running;
  865. },
  866. /**
  867. * Returns a reference to the parent node
  868. * @function
  869. * @return {cc.Node} A reference to the parent node
  870. */
  871. getParent: function () {
  872. return this._parent;
  873. },
  874. /**
  875. * Sets the parent node
  876. * @param {cc.Node} parent A reference to the parent node
  877. */
  878. setParent: function (parent) {
  879. this._parent = parent;
  880. },
  881. /**
  882. * Returns whether the anchor point will be ignored when you position this node.<br/>
  883. * When anchor point ignored, position will be calculated based on the origin point (0, 0) in parent's coordinates.
  884. * @function
  885. * @see cc.Node#ignoreAnchorPointForPosition
  886. * @return {Boolean} true if the anchor point will be ignored when you position this node.
  887. */
  888. isIgnoreAnchorPointForPosition: function () {
  889. return this._ignoreAnchorPointForPosition;
  890. },
  891. /**
  892. * <p>
  893. * Sets whether the anchor point will be ignored when you position this node. <br/>
  894. * When anchor point ignored, position will be calculated based on the origin point (0, 0) in parent's coordinates. <br/>
  895. * This is an internal method, only used by CCLayer and CCScene. Don't call it outside framework. <br/>
  896. * The default value is false, while in CCLayer and CCScene are true
  897. * </p>
  898. * @function
  899. * @param {Boolean} newValue true if anchor point will be ignored when you position this node
  900. */
  901. ignoreAnchorPointForPosition: function (newValue) {
  902. if (newValue != this._ignoreAnchorPointForPosition) {
  903. this._ignoreAnchorPointForPosition = newValue;
  904. this.setNodeDirty();
  905. }
  906. },
  907. /**
  908. * Returns a tag that is used to identify the node easily.
  909. * @function
  910. * @return {Number} An integer that identifies the node.
  911. * @example
  912. * //You can set tags to node then identify them easily.
  913. * // set tags
  914. * node1.setTag(TAG_PLAYER);
  915. * node2.setTag(TAG_MONSTER);
  916. * node3.setTag(TAG_BOSS);
  917. * parent.addChild(node1);
  918. * parent.addChild(node2);
  919. * parent.addChild(node3);
  920. * // identify by tags
  921. * var allChildren = parent.getChildren();
  922. * for(var i = 0; i < allChildren.length; i++){
  923. * switch(node.getTag()) {
  924. * case TAG_PLAYER:
  925. * break;
  926. * case TAG_MONSTER:
  927. * break;
  928. * case TAG_BOSS:
  929. * break;
  930. * }
  931. * }
  932. */
  933. getTag: function () {
  934. return this.tag;
  935. },
  936. /**
  937. * Changes the tag that is used to identify the node easily. <br/>
  938. * Please refer to getTag for the sample code.
  939. * @function
  940. * @see cc.Node#getTag
  941. * @param {Number} tag A integer that identifies the node.
  942. */
  943. setTag: function (tag) {
  944. this.tag = tag;
  945. },
  946. /**
  947. * Changes the name that is used to identify the node easily.
  948. * @function
  949. * @param {String} name
  950. */
  951. setName: function(name){
  952. this._name = name;
  953. },
  954. /**
  955. * Returns a string that is used to identify the node.
  956. * @function
  957. * @returns {string} A string that identifies the node.
  958. */
  959. getName: function(){
  960. return this._name;
  961. },
  962. /**
  963. * <p>
  964. * Returns a custom user data pointer <br/>
  965. * You can set everything in UserData pointer, a data block, a structure or an object.
  966. * </p>
  967. * @function
  968. * @return {object} A custom user data pointer
  969. */
  970. getUserData: function () {
  971. return this.userData;
  972. },
  973. /**
  974. * <p>
  975. * Sets a custom user data reference <br/>
  976. * You can set everything in UserData reference, a data block, a structure or an object, etc.
  977. * </p>
  978. * @function
  979. * @warning Don't forget to release the memory manually in JSB, especially before you change this data pointer, and before this node is autoreleased.
  980. * @param {object} Var A custom user data
  981. */
  982. setUserData: function (Var) {
  983. this.userData = Var;
  984. },
  985. /**
  986. * Returns a user assigned cocos2d object. <br/>
  987. * Similar to userData, but instead of holding all kinds of data it can only hold a cocos2d object
  988. * @function
  989. * @return {object} A user assigned CCObject
  990. */
  991. getUserObject: function () {
  992. return this.userObject;
  993. },
  994. /**
  995. * <p>
  996. * Sets a user assigned cocos2d object <br/>
  997. * Similar to UserData, but instead of holding all kinds of data it can only hold a cocos2d object <br/>
  998. * In JSB, the UserObject will be retained once in this method, and the previous UserObject (if existed) will be release. <br/>
  999. * The UserObject will be released in CCNode's destruction.
  1000. * </p>
  1001. * @param {object} newValue A user cocos2d object
  1002. */
  1003. setUserObject: function (newValue) {
  1004. if (this.userObject != newValue) {
  1005. this.userObject = newValue;
  1006. }
  1007. },
  1008. /**
  1009. * Returns the arrival order, indicates which children should be added previously.
  1010. * @function
  1011. * @return {Number} The arrival order.
  1012. */
  1013. getOrderOfArrival: function () {
  1014. return this.arrivalOrder;
  1015. },
  1016. /**
  1017. * <p>
  1018. * Sets the arrival order when this node has a same ZOrder with other children. <br/>
  1019. * <br/>
  1020. * A node which called addChild subsequently will take a larger arrival order, <br/>
  1021. * If two children have the same Z order, the child with larger arrival order will be drawn later.
  1022. * </p>
  1023. * @function
  1024. * @warning This method is used internally for zOrder sorting, don't change this manually
  1025. * @param {Number} Var The arrival order.
  1026. */
  1027. setOrderOfArrival: function (Var) {
  1028. if(this.arrivalOrder == NaN)
  1029. debugger;
  1030. this.arrivalOrder = Var;
  1031. },
  1032. /**
  1033. * <p>Returns the CCActionManager object that is used by all actions.<br/>
  1034. * (IMPORTANT: If you set a new cc.ActionManager, then previously created actions are going to be removed.)</p>
  1035. * @function
  1036. * @see cc.Node#setActionManager
  1037. * @return {cc.ActionManager} A CCActionManager object.
  1038. */
  1039. getActionManager: function () {
  1040. if (!this._actionManager) {
  1041. this._actionManager = cc.director.getActionManager();
  1042. }
  1043. return this._actionManager;
  1044. },
  1045. /**
  1046. * <p>Sets the cc.ActionManager object that is used by all actions. </p>
  1047. * @function
  1048. * @warning If you set a new CCActionManager, then previously created actions will be removed.
  1049. * @param {cc.ActionManager} actionManager A CCActionManager object that is used by all actions.
  1050. */
  1051. setActionManager: function (actionManager) {
  1052. if (this._actionManager != actionManager) {
  1053. this.stopAllActions();
  1054. this._actionManager = actionManager;
  1055. }
  1056. },
  1057. /**
  1058. * <p>
  1059. * Returns the cc.Scheduler object used to schedule all "updates" and timers.
  1060. * </p>
  1061. * @function
  1062. * @return {cc.Scheduler} A CCScheduler object.
  1063. */
  1064. getScheduler: function () {
  1065. if (!this._scheduler) {
  1066. this._scheduler = cc.director.getScheduler();
  1067. }
  1068. return this._scheduler;
  1069. },
  1070. /**
  1071. * <p>
  1072. * Sets a CCScheduler object that is used to schedule all "updates" and timers. <br/>
  1073. * IMPORTANT: If you set a new cc.Scheduler, then previously created timers/update are going to be removed.
  1074. * </p>
  1075. * @function
  1076. * @warning If you set a new CCScheduler, then previously created timers/update are going to be removed.
  1077. * @param scheduler A cc.Scheduler object that is used to schedule all "update" and timers.
  1078. */
  1079. setScheduler: function (scheduler) {
  1080. if (this._scheduler != scheduler) {
  1081. this.unscheduleAllCallbacks();
  1082. this._scheduler = scheduler;
  1083. }
  1084. },
  1085. /**
  1086. * Returns a "local" axis aligned bounding box of the node. <br/>
  1087. * @deprecated since v3.0, please use getBoundingBox instead
  1088. * @return {cc.Rect}
  1089. */
  1090. boundingBox: function(){
  1091. cc.log(cc._LogInfos.Node_boundingBox);
  1092. return this.getBoundingBox();
  1093. },
  1094. /**
  1095. * Returns a "local" axis aligned bounding box of the node. <br/>
  1096. * The returned box is relative only to its parent.
  1097. * @function
  1098. * @return {cc.Rect} The calculated bounding box of the node
  1099. */
  1100. getBoundingBox: function () {
  1101. var rect = cc.rect(0, 0, this._contentSize.width, this._contentSize.height);
  1102. return cc._rectApplyAffineTransformIn(rect, this.getNodeToParentTransform());
  1103. },
  1104. /**
  1105. * Stops all running actions and schedulers
  1106. * @function
  1107. */
  1108. cleanup: function () {
  1109. // actions
  1110. this.stopAllActions();
  1111. this.unscheduleAllCallbacks();
  1112. // event
  1113. cc.eventManager.removeListeners(this);
  1114. // timers
  1115. this._arrayMakeObjectsPerformSelector(this._children, cc.Node._StateCallbackType.cleanup);
  1116. },
  1117. // composition: GET
  1118. /**
  1119. * Returns a child from the container given its tag
  1120. * @function
  1121. * @param {Number} aTag An identifier to find the child node.
  1122. * @return {cc.Node} a CCNode object whose tag equals to the input parameter
  1123. */
  1124. getChildByTag: function (aTag) {
  1125. var __children = this._children;
  1126. if (__children != null) {
  1127. for (var i = 0; i < __children.length; i++) {
  1128. var node = __children[i];
  1129. if (node && node.tag == aTag)
  1130. return node;
  1131. }
  1132. }
  1133. return null;
  1134. },
  1135. /**
  1136. * Returns a child from the container given its name
  1137. * @function
  1138. * @param {Number} name An identifier to find the child node.
  1139. * @return {cc.Node} a CCNode object whose name equals to the input parameter
  1140. */
  1141. getChildByName: function(name){
  1142. if(!name){
  1143. cc.log("Invalid name");
  1144. return null;
  1145. }
  1146. var locChildren = this._children;
  1147. for(var i = 0, len = locChildren.length; i < len; i++){
  1148. if(locChildren[i]._name == name)
  1149. return locChildren[i];
  1150. }
  1151. return null;
  1152. },
  1153. // composition: ADD
  1154. /** <p>"add" logic MUST only be in this method <br/> </p>
  1155. *
  1156. * <p>If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.</p>
  1157. * @function
  1158. * @param {cc.Node} child A child node
  1159. * @param {Number} [localZOrder=] Z order for drawing priority. Please refer to setZOrder(int)
  1160. * @param {Number} [tag=] A integer to identify the node easily. Please refer to setTag(int)
  1161. */
  1162. addChild: function (child, localZOrder, tag) {
  1163. var child = child;
  1164. var localZOrder = localZOrder === undefined ? child._localZOrder : localZOrder;
  1165. var tag, name, setTag = false;
  1166. switch(typeof tag){
  1167. case 'undefined':
  1168. tag = undefined;
  1169. name = child._name;
  1170. break;
  1171. case 'string':
  1172. name = tag;
  1173. tag = undefined;
  1174. break;
  1175. case 'number':
  1176. setTag = true;
  1177. name = "";
  1178. break;
  1179. }
  1180. cc.assert(child, cc._LogInfos.Node_addChild_3);
  1181. cc.assert(child._parent === null, "child already added. It can't be added again");
  1182. this._addChildHelper(child, localZOrder, tag, name, setTag);
  1183. },
  1184. _addChildHelper: function(child, localZOrder, tag, name, setTag){
  1185. if(!this._children)
  1186. this._children = [];
  1187. this._insertChild(child, localZOrder);
  1188. if(setTag)
  1189. child.setTag(tag);
  1190. else
  1191. child.setName(name);
  1192. child.setParent(this);
  1193. child.setOrderOfArrival(cc.s_globalOrderOfArrival++);
  1194. if( this._running ){
  1195. child.onEnter();
  1196. // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
  1197. if (this._isTransitionFinished)
  1198. child.onEnterTransitionDidFinish();
  1199. }
  1200. if (this._cascadeColorEnabled)
  1201. this._enableCascadeColor();
  1202. if (this._cascadeOpacityEnabled)
  1203. this._enableCascadeOpacity();
  1204. },
  1205. // composition: REMOVE
  1206. /**
  1207. * Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks. <br/>
  1208. * If the cleanup parameter is not passed, it will force a cleanup. <br/>
  1209. * If the node orphan, then nothing happens.
  1210. * @function
  1211. * @param {Boolean} cleanup true if all actions and callbacks on this node should be removed, false otherwise.
  1212. * @see cc.Node#removeFromParentAndCleanup
  1213. */
  1214. removeFromParent: function (cleanup) {
  1215. if (this._parent) {
  1216. if (cleanup == null)
  1217. cleanup = true;
  1218. this._parent.removeChild(this, cleanup);
  1219. }
  1220. },
  1221. /**
  1222. * Removes this node itself from its parent node. <br/>
  1223. * If the node orphan, then nothing happens.
  1224. * @deprecated since v3.0, please use removeFromParent() instead
  1225. * @param {Boolean} cleanup true if all actions and callbacks on this node should be removed, false otherwise.
  1226. */
  1227. removeFromParentAndCleanup: function (cleanup) {
  1228. cc.log(cc._LogInfos.Node_removeFromParentAndCleanup);
  1229. this.removeFromParent(cleanup);
  1230. },
  1231. /** <p>Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter. </p>
  1232. * If the cleanup parameter is not passed, it will force a cleanup. <br/>
  1233. * <p> "remove" logic MUST only be on this method <br/>
  1234. * If a class wants to extend the 'removeChild' behavior it only needs <br/>
  1235. * to override this method </p>
  1236. * @function
  1237. * @param {cc.Node} child The child node which will be removed.
  1238. * @param {Boolean|null} [cleanup=null] true if all running actions and callbacks on the child node will be cleanup, false otherwise.
  1239. */
  1240. removeChild: function (child, cleanup) {
  1241. // explicit nil handling
  1242. if (this._children.length === 0)
  1243. return;
  1244. if (cleanup == null)
  1245. cleanup = true;
  1246. if (this._children.indexOf(child) > -1)
  1247. this._detachChild(child, cleanup);
  1248. this.setNodeDirty();
  1249. },
  1250. /**
  1251. * Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter.
  1252. * If the cleanup parameter is not passed, it will force a cleanup. <br/>
  1253. * @function
  1254. * @param {Number} tag An integer number that identifies a child node
  1255. * @param {Boolean} cleanup true if all running actions and callbacks on the child node will be cleanup, false otherwise.
  1256. * @see cc.Node#removeChildByTag
  1257. */
  1258. removeChildByTag: function (tag, cleanup) {
  1259. if (tag === cc.NODE_TAG_INVALID)
  1260. cc.log(cc._LogInfos.Node_removeChildByTag);
  1261. var child = this.getChildByTag(tag);
  1262. if (child == null)
  1263. cc.log(cc._LogInfos.Node_removeChildByTag_2, tag);
  1264. else
  1265. this.removeChild(child, cleanup);
  1266. },
  1267. /**
  1268. * Removes all children from the container and do a cleanup all running actions depending on the cleanup parameter.
  1269. * @deprecated since v3.0, please use removeAllChildren() instead
  1270. * @param {Boolean | null } cleanup
  1271. */
  1272. removeAllChildrenWithCleanup: function (cleanup) {
  1273. cc.log(cc._LogInfos.Node_removeAllChildrenWithCleanup);
  1274. this.removeAllChildren(cleanup);
  1275. },
  1276. /**
  1277. * Removes all children from the container and do a cleanup all running actions depending on the cleanup parameter. <br/>
  1278. * If the cleanup parameter is not passed, it will force a cleanup. <br/>
  1279. * @function
  1280. * @param {Boolean | null } cleanup true if all running actions on all children nodes should be cleanup, false otherwise.
  1281. */
  1282. removeAllChildren: function (cleanup) {
  1283. // not using detachChild improves speed here
  1284. var __children = this._children;
  1285. if (__children != null) {
  1286. if (cleanup == null)
  1287. cleanup = true;
  1288. for (var i = 0; i < __children.length; i++) {
  1289. var node = __children[i];
  1290. if (node) {
  1291. // IMPORTANT:
  1292. // -1st do onExit
  1293. // -2nd cleanup
  1294. if (this._running) {
  1295. node.onExitTransitionDidStart();
  1296. node.onExit();
  1297. }
  1298. if (cleanup)
  1299. node.cleanup();
  1300. // set parent nil at the end
  1301. node.parent = null;
  1302. }
  1303. }
  1304. this._children.length = 0;
  1305. }
  1306. },
  1307. _detachChild: function (child, doCleanup) {
  1308. // IMPORTANT:
  1309. // -1st do onExit
  1310. // -2nd cleanup
  1311. if (this._running) {
  1312. child.onExitTransitionDidStart();
  1313. child.onExit();
  1314. }
  1315. // If you don't do cleanup, the child's actions will not get removed and the
  1316. // its scheduledSelectors_ dict will not get released!
  1317. if (doCleanup)
  1318. child.cleanup();
  1319. // set parent nil at the end
  1320. child.parent = null;
  1321. cc.arrayRemoveObject(this._children, child);
  1322. },
  1323. _insertChild: function (child, z) {
  1324. this._reorderChildDirty = true;
  1325. this._children.push(child);
  1326. child._setLocalZOrder(z);
  1327. },
  1328. /** Reorders a child according to a new z value. <br/>
  1329. * The child MUST be already added.
  1330. * @function
  1331. * @param {cc.Node} child An already added child node. It MUST be already added.
  1332. * @param {Number} zOrder Z order for drawing priority. Please refer to setZOrder(int)
  1333. */
  1334. reorderChild: function (child, zOrder) {
  1335. cc.assert(child, cc._LogInfos.Node_reorderChild)
  1336. this._reorderChildDirty = true;
  1337. child.arrivalOrder = cc.s_globalOrderOfArrival;
  1338. cc.s_globalOrderOfArrival++;
  1339. child._setLocalZOrder(zOrder);
  1340. this.setNodeDirty();
  1341. },
  1342. /**
  1343. * <p>
  1344. * Sorts the children array once before drawing, instead of every time when a child is added or reordered. <br/>
  1345. * This approach can improves the performance massively.
  1346. * </p>
  1347. * @function
  1348. * @note Don't call this manually unless a child added needs to be removed in the same frame
  1349. */
  1350. sortAllChildren: function () {
  1351. if (this._reorderChildDirty) {
  1352. var _children = this._children;
  1353. // insertion sort
  1354. var len = _children.length, i, j, tmp;
  1355. for(i=1; i<len; i++){
  1356. tmp = _children[i];
  1357. j = i - 1;
  1358. //continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
  1359. while(j >= 0){
  1360. if(tmp._localZOrder < _children[j]._localZOrder){
  1361. _children[j+1] = _children[j];
  1362. }else if(tmp._localZOrder === _children[j]._localZOrder && tmp.arrivalOrder < _children[j].arrivalOrder){
  1363. _children[j+1] = _children[j];
  1364. }else{
  1365. break;
  1366. }
  1367. j--;
  1368. }
  1369. _children[j+1] = tmp;
  1370. }
  1371. //don't need to check children recursively, that's done in visit of each child
  1372. this._reorderChildDirty = false;
  1373. }
  1374. },
  1375. /**
  1376. * Render function using the canvas 2d context or WebGL context, internal usage only, please do not call this function
  1377. * @function
  1378. * @param {CanvasRenderingContext2D | WebGLRenderingContext} ctx The render context
  1379. */
  1380. draw: function (ctx) {
  1381. // override me
  1382. // Only use- this function to draw your staff.
  1383. // DON'T draw your stuff outside this method
  1384. },
  1385. // Internal use only, do not call it by yourself,
  1386. transformAncestors: function () {
  1387. if (this._parent != null) {
  1388. this._parent.transformAncestors();
  1389. this._parent.transform();
  1390. }
  1391. },
  1392. //scene managment
  1393. /**
  1394. * <p>
  1395. * Event callback that is invoked every time when CCNode enters the 'stage'. <br/>
  1396. * If the CCNode enters the 'stage' with a transition, this event is called when the transition starts. <br/>
  1397. * During onEnter you can't access a "sister/brother" node. <br/>
  1398. * If you override onEnter, you must call its parent's onEnter function with this._super().
  1399. * </p>
  1400. * @function
  1401. */
  1402. onEnter: function () {
  1403. this._isTransitionFinished = false;
  1404. this._running = true;//should be running before resumeSchedule
  1405. this._arrayMakeObjectsPerformSelector(this._children, cc.Node._StateCallbackType.onEnter);
  1406. this.resume();
  1407. },
  1408. /**
  1409. * <p>
  1410. * Event callback that is invoked when the CCNode enters in the 'stage'. <br/>
  1411. * If the CCNode enters the 'stage' with a transition, this event is called when the transition finishes. <br/>
  1412. * If you override onEnterTransitionDidFinish, you shall call its parent's onEnterTransitionDidFinish with this._super()
  1413. * </p>
  1414. * @function
  1415. */
  1416. onEnterTransitionDidFinish: function () {
  1417. this._isTransitionFinished = true;
  1418. this._arrayMakeObjectsPerformSelector(this._children, cc.Node._StateCallbackType.onEnterTransitionDidFinish);
  1419. },
  1420. /**
  1421. * <p>callback that is called every time the cc.Node leaves the 'stage'. <br/>
  1422. * If the cc.Node leaves the 'stage' with a transition, this callback is called when the transition starts. <br/>
  1423. * If you override onExitTransitionDidStart, you shall call its parent's onExitTransitionDidStart with this._super()</p>
  1424. * @function
  1425. */
  1426. onExitTransitionDidStart: function () {
  1427. this._arrayMakeObjectsPerformSelector(this._children, cc.Node._StateCallbackType.onExitTransitionDidStart);
  1428. },
  1429. /**
  1430. * <p>
  1431. * callback that is called every time the cc.Node leaves the 'stage'. <br/>
  1432. * If the cc.Node leaves the 'stage' with a transition, this callback is called when the transition finishes. <br/>
  1433. * During onExit you can't access a sibling node. <br/>
  1434. * If you override onExit, you shall call its parent's onExit with this._super().
  1435. * </p>
  1436. * @function
  1437. */
  1438. onExit: function () {
  1439. this._running = false;
  1440. this.pause();
  1441. this._arrayMakeObjectsPerformSelector(this._children, cc.Node._StateCallbackType.onExit);
  1442. },
  1443. // actions
  1444. /**
  1445. * Executes an action, and returns the action that is executed.<br/>
  1446. * The node becomes the action's target. Refer to cc.Action's getTarget()
  1447. * @function
  1448. * @warning Starting from v0.8 actions don't retain their target anymore.
  1449. * @param {cc.Action} action
  1450. * @return {cc.Action} An Action pointer
  1451. */
  1452. runAction: function (action) {
  1453. cc.assert(action, cc._LogInfos.Node_runAction);
  1454. this.actionManager.addAction(action, this, !this._running);
  1455. return action;
  1456. },
  1457. /**
  1458. * Stops and removes all actions from the running action list .
  1459. * @function
  1460. */
  1461. stopAllActions: function () {
  1462. this.actionManager && this.actionManager.removeAllActionsFromTarget(this);
  1463. },
  1464. /**
  1465. * Stops and removes an action from the running action list.
  1466. * @function
  1467. * @param {cc.Action} action An action object to be removed.
  1468. */
  1469. stopAction: function (action) {
  1470. this.actionManager.removeAction(action);
  1471. },
  1472. /**
  1473. * Removes an action from the running action list by its tag.
  1474. * @function
  1475. * @param {Number} tag A tag that indicates the action to be removed.
  1476. */
  1477. stopActionByTag: function (tag) {
  1478. if (tag === cc.ACTION_TAG_INVALID) {
  1479. cc.log(cc._LogInfos.Node_stopActionByTag);
  1480. return;
  1481. }
  1482. this.actionManager.removeActionByTag(tag, this);
  1483. },
  1484. /**
  1485. * Returns an action from the running action list by its tag.
  1486. * @function
  1487. * @see cc.Node#getTag and cc.Node#setTag
  1488. * @param {Number} tag
  1489. * @return {cc.Action} The action object with the given tag.
  1490. */
  1491. getActionByTag: function (tag) {
  1492. if (tag === cc.ACTION_TAG_INVALID) {
  1493. cc.log(cc._LogInfos.Node_getActionByTag);
  1494. return null;
  1495. }
  1496. return this.actionManager.getActionByTag(tag, this);
  1497. },
  1498. /** <p>Returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionsToAdd and actions arrays).<br/>
  1499. * Composable actions are counted as 1 action. Example:<br/>
  1500. * If you are running 1 Sequence of 7 actions, it will return 1. <br/>
  1501. * If you are running 7 Sequences of 2 actions, it will return 7.</p>
  1502. * @function
  1503. * @return {Number} The number of actions that are running plus the ones that are schedule to run
  1504. */
  1505. getNumberOfRunningActions: function () {
  1506. return this.actionManager.numberOfRunningActionsInTarget(this);
  1507. },
  1508. // cc.Node - Callbacks
  1509. // timers
  1510. /**
  1511. * <p>schedules the "update" method. <br/>
  1512. * It will use the order number 0. This method will be called every frame. <br/>
  1513. * Scheduled methods with a lower order value will be called before the ones that have a higher order value.<br/>
  1514. * Only one "update" method could be scheduled per node.</p>
  1515. * @function
  1516. */
  1517. scheduleUpdate: function () {
  1518. this.scheduleUpdateWithPriority(0);
  1519. },
  1520. /**
  1521. * <p>
  1522. * schedules the "update" callback function with a custom priority.
  1523. * This callback function will be called every frame.<br/>
  1524. * Scheduled callback functions with a lower priority will be called before the ones that have a higher value.<br/>
  1525. * Only one "update" callback function could be scheduled per node (You can't have 2 'update' callback functions).<br/>
  1526. * </p>
  1527. * @function
  1528. * @param {Number} priority
  1529. */
  1530. scheduleUpdateWithPriority: function (priority) {
  1531. this.scheduler.scheduleUpdateForTarget(this, priority, !this._running);
  1532. },
  1533. /**
  1534. * Unschedules the "update" method.
  1535. * @function
  1536. * @see cc.Node#scheduleUpdate
  1537. */
  1538. unscheduleUpdate: function () {
  1539. this.scheduler.unscheduleUpdateForTarget(this);
  1540. },
  1541. /**
  1542. * <p>Schedules a custom selector. <br/>
  1543. * If the selector is already scheduled, then the interval parameter will be updated without scheduling it again.</p>
  1544. * @function
  1545. * @param {function} callback_fn A function wrapped as a selector
  1546. * @param {Number} interval Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
  1547. * @param {Number} repeat The selector will be executed (repeat + 1) times, you can use kCCRepeatForever for tick infinitely.
  1548. * @param {Number} delay The amount of time that the first tick will wait before execution.
  1549. */
  1550. schedule: function (callback_fn, interval, repeat, delay) {
  1551. interval = interval || 0;
  1552. cc.assert(callback_fn, cc._LogInfos.Node_schedule);
  1553. cc.assert(interval >= 0, cc._LogInfos.Node_schedule_2);
  1554. repeat = (repeat == null) ? cc.REPEAT_FOREVER : repeat;
  1555. delay = delay || 0;
  1556. this.scheduler.scheduleCallbackForTarget(this, callback_fn, interval, repeat, delay, !this._running);
  1557. },
  1558. /**
  1559. * Schedules a callback function that runs only once, with a delay of 0 or larger
  1560. * @function
  1561. * @see cc.Node#schedule
  1562. * @param {function} callback_fn A function wrapped as a selector
  1563. * @param {Number} delay The amount of time that the first tick will wait before execution.
  1564. */
  1565. scheduleOnce: function (callback_fn, delay) {
  1566. this.schedule(callback_fn, 0.0, 0, delay);
  1567. },
  1568. /**
  1569. * unschedules a custom callback function.
  1570. * @function
  1571. * @see cc.Node#schedule
  1572. * @param {function} callback_fn A function wrapped as a selector
  1573. */
  1574. unschedule: function (callback_fn) {
  1575. // explicit nil handling
  1576. if (!callback_fn)
  1577. return;
  1578. this.scheduler.unscheduleCallbackForTarget(this, callback_fn);
  1579. },
  1580. /**
  1581. * <p>unschedule all scheduled callback functions: custom callback functions, and the 'update' callback function.<br/>
  1582. * Actions are not affected by this method.</p>
  1583. * @function
  1584. */
  1585. unscheduleAllCallbacks: function () {
  1586. this.scheduler.unscheduleAllCallbacksForTarget(this);
  1587. },
  1588. /**
  1589. * Resumes all scheduled selectors and actions.<br/>
  1590. * This method is called internally by onEnter
  1591. * @function
  1592. * @deprecated since v3.0, please use resume() instead
  1593. */
  1594. resumeSchedulerAndActions: function () {
  1595. cc.log(cc._LogInfos.Node_resumeSchedulerAndActions);
  1596. this.resume();
  1597. },
  1598. /**
  1599. * <p>Resumes all scheduled selectors and actions.<br/>
  1600. * This method is called internally by onEnter</p>
  1601. */
  1602. resume: function () {
  1603. this.scheduler.resumeTarget(this);
  1604. this.actionManager && this.actionManager.resumeTarget(this);
  1605. cc.eventManager.resumeTarget(this);
  1606. },
  1607. /**
  1608. * <p>Pauses all scheduled selectors and actions.<br/>
  1609. * This method is called internally by onExit</p>
  1610. * @deprecated since v3.0, please use pause instead
  1611. * @function
  1612. */
  1613. pauseSchedulerAndActions: function () {
  1614. cc.log(cc._LogInfos.Node_pauseSchedulerAndActions);
  1615. this.pause();
  1616. },
  1617. /**
  1618. * <p>Pauses all scheduled selectors and actions.<br/>
  1619. * This method is called internally by onExit</p>
  1620. * @function
  1621. */
  1622. pause: function () {
  1623. this.scheduler.pauseTarget(this);
  1624. this.actionManager && this.actionManager.pauseTarget(this);
  1625. cc.eventManager.pauseTarget(this);
  1626. },
  1627. /**
  1628. *<p>Sets the additional transform.<br/>
  1629. * The additional transform will be concatenated at the end of getNodeToParentTransform.<br/>
  1630. * It could be used to simulate `parent-child` relationship between two nodes (e.g. one is in BatchNode, another isn't).<br/>
  1631. * </p>
  1632. * @function
  1633. * @param {cc.AffineTransform} additionalTransform The additional transform
  1634. * @example
  1635. * // create a batchNode
  1636. * var batch= cc.SpriteBatchNode.create("Icon-114.png");
  1637. * this.addChild(batch);
  1638. *
  1639. * // create two sprites, spriteA will be added to batchNode, they are using different textures.
  1640. * var spriteA = cc.Sprite.create(batch->getTexture());
  1641. * var spriteB = cc.Sprite.create("Icon-72.png");
  1642. *
  1643. * batch.addChild(spriteA);
  1644. *
  1645. * // We can't make spriteB as spriteA's child since they use different textures. So just add it to layer.
  1646. * // But we want to simulate `parent-child` relationship for these two node.
  1647. * this.addChild(spriteB);
  1648. *
  1649. * //position
  1650. * spriteA.setPosition(ccp(200, 200));
  1651. *
  1652. * // Gets the spriteA's transform.
  1653. * var t = spriteA.getNodeToParentTransform();
  1654. *
  1655. * // Sets the additional transform to spriteB, spriteB's position will based on its pseudo parent i.e. spriteA.
  1656. * spriteB.setAdditionalTransform(t);
  1657. *
  1658. * //scale
  1659. * spriteA.setScale(2);
  1660. *
  1661. * // Gets the spriteA's transform.
  1662. * t = spriteA.getNodeToParentTransform();
  1663. *
  1664. * // Sets the additional transform to spriteB, spriteB's scale will based on its pseudo parent i.e. spriteA.
  1665. * spriteB.setAdditionalTransform(t);
  1666. *
  1667. * //rotation
  1668. * spriteA.setRotation(20);
  1669. *
  1670. * // Gets the spriteA's transform.
  1671. * t = spriteA.getNodeToParentTransform();
  1672. *
  1673. * // Sets the additional transform to spriteB, spriteB's rotation will based on its pseudo parent i.e. spriteA.
  1674. * spriteB.setAdditionalTransform(t);
  1675. */
  1676. setAdditionalTransform: function (additionalTransform) {
  1677. this._additionalTransform = additionalTransform;
  1678. this._transformDirty = true;
  1679. this._additionalTransformDirty = true;
  1680. },
  1681. /**
  1682. * Returns the matrix that transform parent's space coordinates to the node's (local) space coordinates.<br/>
  1683. * The matrix is in Pixels.
  1684. * @function
  1685. * @return {cc.AffineTransform}
  1686. */
  1687. getParentToNodeTransform: function () {
  1688. if (this._inverseDirty) {
  1689. this._inverse = cc.affineTransformInvert(this.getNodeToParentTransform());
  1690. this._inverseDirty = false;
  1691. }
  1692. return this._inverse;
  1693. },
  1694. /**
  1695. * @function
  1696. * @deprecated since v3.0, please use getParentToNodeTransform instead
  1697. */
  1698. parentToNodeTransform: function () {
  1699. return this.getParentToNodeTransform();
  1700. },
  1701. /**
  1702. * Returns the world affine transform matrix. The matrix is in Pixels.
  1703. * @function
  1704. * @return {cc.AffineTransform}
  1705. */
  1706. getNodeToWorldTransform: function () {
  1707. var t = this.getNodeToParentTransform();
  1708. for (var p = this._parent; p != null; p = p.parent)
  1709. t = cc.affineTransformConcat(t, p.getNodeToParentTransform());
  1710. return t;
  1711. },
  1712. /**
  1713. * @function
  1714. * @deprecated since v3.0, please use getNodeToWorldTransform instead
  1715. */
  1716. nodeToWorldTransform: function(){
  1717. return this.getNodeToWorldTransform();
  1718. },
  1719. /**
  1720. * Returns the inverse world affine transform matrix. The matrix is in Pixels.
  1721. * @function
  1722. * @return {cc.AffineTransform}
  1723. */
  1724. getWorldToNodeTransform: function () {
  1725. return cc.affineTransformInvert(this.getNodeToWorldTransform());
  1726. },
  1727. /**
  1728. * @function
  1729. * @deprecated since v3.0, please use getWorldToNodeTransform instead
  1730. */
  1731. worldToNodeTransform: function () {
  1732. return this.getWorldToNodeTransform();
  1733. },
  1734. /**
  1735. * Converts a Point to node (local) space coordinates. The result is in Points.
  1736. * @function
  1737. * @param {cc.Point} worldPoint
  1738. * @return {cc.Point}
  1739. */
  1740. convertToNodeSpace: function (worldPoint) {
  1741. return cc.pointApplyAffineTransform(worldPoint, this.getWorldToNodeTransform());
  1742. },
  1743. /**
  1744. * Converts a Point to world space coordinates. The result is in Points.
  1745. * @function
  1746. * @param {cc.Point} nodePoint
  1747. * @return {cc.Point}
  1748. */
  1749. convertToWorldSpace: function (nodePoint) {
  1750. nodePoint = nodePoint || cc.p(0,0);
  1751. return cc.pointApplyAffineTransform(nodePoint, this.getNodeToWorldTransform());
  1752. },
  1753. /**
  1754. * Converts a Point to node (local) space coordinates. The result is in Points.<br/>
  1755. * treating the returned/received node point as anchor relative.
  1756. * @function
  1757. * @param {cc.Point} worldPoint
  1758. * @return {cc.Point}
  1759. */
  1760. convertToNodeSpaceAR: function (worldPoint) {
  1761. return cc.pSub(this.convertToNodeSpace(worldPoint), this._anchorPointInPoints);
  1762. },
  1763. /**
  1764. * Converts a local Point to world space coordinates.The result is in Points.<br/>
  1765. * treating the returned/received node point as anchor relative.
  1766. * @function
  1767. * @param {cc.Point} nodePoint
  1768. * @return {cc.Point}
  1769. */
  1770. convertToWorldSpaceAR: function (nodePoint) {
  1771. nodePoint = nodePoint || cc.p(0,0);
  1772. var pt = cc.pAdd(nodePoint, this._anchorPointInPoints);
  1773. return this.convertToWorldSpace(pt);
  1774. },
  1775. _convertToWindowSpace: function (nodePoint) {
  1776. var worldPoint = this.convertToWorldSpace(nodePoint);
  1777. return cc.director.convertToUI(worldPoint);
  1778. },
  1779. /** convenience methods which take a cc.Touch instead of cc.Point
  1780. * @function
  1781. * @param {cc.Touch} touch The touch object
  1782. * @return {cc.Point}
  1783. */
  1784. convertTouchToNodeSpace: function (touch) {
  1785. var point = touch.getLocation();
  1786. //TODO This point needn't convert to GL in HTML5
  1787. //point = cc.director.convertToGL(point);
  1788. return this.convertToNodeSpace(point);
  1789. },
  1790. /**
  1791. * converts a cc.Touch (world coordinates) into a local coordiante. This method is AR (Anchor Relative).
  1792. * @function
  1793. * @param {cc.Touch} touch The touch object
  1794. * @return {cc.Point}
  1795. */
  1796. convertTouchToNodeSpaceAR: function (touch) {
  1797. var point = touch.getLocation();
  1798. point = cc.director.convertToGL(point);
  1799. return this.convertToNodeSpaceAR(point);
  1800. },
  1801. /**
  1802. * Update will be called automatically every frame if "scheduleUpdate" is called when the node is "live".<br/>
  1803. * The default behavior is to invoke the visit function of node's componentContainer.<br/>
  1804. * Override me to implement your own update logic.
  1805. * @function
  1806. * @param {Number} dt Delta time since last update
  1807. */
  1808. update: function (dt) {
  1809. if (this._componentContainer && !this._componentContainer.isEmpty())
  1810. this._componentContainer.visit(dt);
  1811. },
  1812. /**
  1813. * <p>
  1814. * Calls children's updateTransform() method recursively. <br/>
  1815. * <br/>
  1816. * This method is moved from CCSprite, so it's no longer specific to CCSprite. <br/>
  1817. * As the result, you apply CCSpriteBatchNode's optimization on your customed CCNode. <br/>
  1818. * e.g., batchNode->addChild(myCustomNode), while you can only addChild(sprite) before.
  1819. * </p>
  1820. * @function
  1821. */
  1822. updateTransform: function () {
  1823. // Recursively iterate over children
  1824. this._arrayMakeObjectsPerformSelector(this._children, cc.Node._StateCallbackType.updateTransform);
  1825. },
  1826. /**
  1827. * <p>Currently JavaScript Bindings (JSB), in some cases, needs to use retain and release. This is a bug in JSB,
  1828. * and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB.
  1829. * This is a hack, and should be removed once JSB fixes the retain/release bug<br/>
  1830. * You will need to retain an object if you created an engine object and haven't added it into the scene graph during the same frame.<br/>
  1831. * Otherwise, JSB's native autorelease pool will consider this object a useless one and release it directly,<br/>
  1832. * when you want to use it later, a "Invalid Native Object" error will be raised.<br/>
  1833. * The retain function can increase a reference count for the native object to avoid it being released,<br/>
  1834. * you need to manually invoke release function when you think this object is no longer needed, otherwise, there will be memory learks.<br/>
  1835. * retain and release function call should be paired in developer's game code.</p>
  1836. * @function
  1837. * @see cc.Node#release
  1838. */
  1839. retain: function () {
  1840. },
  1841. /**
  1842. * <p>Currently JavaScript Bindings (JSB), in some cases, needs to use retain and release. This is a bug in JSB,
  1843. * and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB.
  1844. * This is a hack, and should be removed once JSB fixes the retain/release bug<br/>
  1845. * You will need to retain an object if you created an engine object and haven't added it into the scene graph during the same frame.<br/>
  1846. * Otherwise, JSB's native autorelease pool will consider this object a useless one and release it directly,<br/>
  1847. * when you want to use it later, a "Invalid Native Object" error will be raised.<br/>
  1848. * The retain function can increase a reference count for the native object to avoid it being released,<br/>
  1849. * you need to manually invoke release function when you think this object is no longer needed, otherwise, there will be memory learks.<br/>
  1850. * retain and release function call should be paired in developer's game code.</p>
  1851. * @function
  1852. * @see cc.Node#retain
  1853. */
  1854. release: function () {
  1855. },
  1856. /**
  1857. * Returns a component identified by the name given.
  1858. * @function
  1859. * @param {String} name The name to search for
  1860. * @return {cc.Component} The component found
  1861. */
  1862. getComponent: function (name) {
  1863. return this._componentContainer.getComponent(name);
  1864. },
  1865. /**
  1866. * Adds a component to the node's component container.
  1867. * @function
  1868. * @param {cc.Component} component
  1869. */
  1870. addComponent: function (component) {
  1871. this._componentContainer.add(component);
  1872. },
  1873. /**
  1874. * Removes a component identified by the given name or removes the component object given
  1875. * @function
  1876. * @param {String|cc.Component} component
  1877. */
  1878. removeComponent: function (component) {
  1879. return this._componentContainer.remove(component);
  1880. },
  1881. /**
  1882. * Removes all components
  1883. * @function
  1884. */
  1885. removeAllComponents: function () {
  1886. this._componentContainer.removeAll();
  1887. },
  1888. grid: null,
  1889. /**
  1890. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  1891. * @function
  1892. */
  1893. ctor: null,
  1894. /**
  1895. * Recursive method that visit its children and draw them
  1896. * @function
  1897. * @param {CanvasRenderingContext2D|WebGLRenderingContext} ctx
  1898. */
  1899. visit: null,
  1900. /**
  1901. * Performs view-matrix transformation based on position, scale, rotation and other attributes.
  1902. * @function
  1903. * @param {CanvasRenderingContext2D|WebGLRenderingContext} ctx Render context
  1904. */
  1905. transform: null,
  1906. /**
  1907. * <p>Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.<br/>
  1908. * The matrix is in Pixels.</p>
  1909. * @function
  1910. * @return {cc.AffineTransform}
  1911. * @deprecated since v3.0, please use getNodeToParentTransform instead
  1912. */
  1913. nodeToParentTransform: function(){
  1914. return this.getNodeToParentTransform();
  1915. },
  1916. /**
  1917. * Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.<br/>
  1918. * The matrix is in Pixels.
  1919. * @function
  1920. * @return {cc.AffineTransform} The affine transform object
  1921. */
  1922. getNodeToParentTransform: null,
  1923. _setNodeDirtyForCache: function () {
  1924. if (this._cacheDirty === false) {
  1925. this._cacheDirty = true;
  1926. var cachedP = this._cachedParent;
  1927. //var cachedP = this._parent;
  1928. cachedP && cachedP != this && cachedP._setNodeDirtyForCache();
  1929. }
  1930. },
  1931. _setCachedParent: function(cachedParent){
  1932. if(this._cachedParent == cachedParent)
  1933. return;
  1934. this._cachedParent = cachedParent;
  1935. var children = this._children;
  1936. for(var i = 0, len = children.length; i < len; i++)
  1937. children[i]._setCachedParent(cachedParent);
  1938. },
  1939. /**
  1940. * Returns a camera object that lets you move the node using a gluLookAt
  1941. * @function
  1942. * @return {cc.Camera} A CCCamera object that lets you move the node using a gluLookAt
  1943. * @deprecated since v3.0, no alternative function
  1944. * @example
  1945. * var camera = node.getCamera();
  1946. * camera.setEye(0, 0, 415/2);
  1947. * camera.setCenter(0, 0, 0);
  1948. */
  1949. getCamera: function () {
  1950. if (!this._camera) {
  1951. this._camera = new cc.Camera();
  1952. }
  1953. return this._camera;
  1954. },
  1955. /**
  1956. * <p>Returns a grid object that is used when applying effects.<br/>
  1957. * This function have been deprecated, please use cc.NodeGrid to run grid actions</p>
  1958. * @function
  1959. * @return {cc.GridBase} A CCGrid object that is used when applying effects
  1960. * @deprecated since v3.0, no alternative function
  1961. */
  1962. getGrid: function () {
  1963. return this.grid;
  1964. },
  1965. /**
  1966. * <p>Changes a grid object that is used when applying effects<br/>
  1967. * This function have been deprecated, please use cc.NodeGrid to run grid actions</p>
  1968. * @function
  1969. * @param {cc.GridBase} grid A CCGrid object that is used when applying effects
  1970. * @deprecated since v3.0, no alternative function
  1971. */
  1972. setGrid: function (grid) {
  1973. this.grid = grid;
  1974. },
  1975. /**
  1976. * Return the shader program currently used for this node
  1977. * @function
  1978. * @return {cc.GLProgram} The shader program currently used for this node
  1979. */
  1980. getShaderProgram: function () {
  1981. return this._shaderProgram;
  1982. },
  1983. /**
  1984. * <p>
  1985. * Sets the shader program for this node
  1986. *
  1987. * Since v2.0, each rendering node must set its shader program.
  1988. * It should be set in initialize phase.
  1989. * </p>
  1990. * @function
  1991. * @param {cc.GLProgram} newShaderProgram The shader program which fetchs from CCShaderCache.
  1992. * @example
  1993. * node.setGLProgram(cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURECOLOR));
  1994. */
  1995. setShaderProgram: function (newShaderProgram) {
  1996. this._shaderProgram = newShaderProgram;
  1997. },
  1998. /**
  1999. * Returns the state of OpenGL server side.
  2000. * @function
  2001. * @return {Number} The state of OpenGL server side.
  2002. * @deprecated since v3.0, no need anymore
  2003. */
  2004. getGLServerState: function () {
  2005. return this._glServerState;
  2006. },
  2007. /**
  2008. * Sets the state of OpenGL server side.
  2009. * @function
  2010. * @param {Number} state The state of OpenGL server side.
  2011. * @deprecated since v3.0, no need anymore
  2012. */
  2013. setGLServerState: function (state) {
  2014. this._glServerState = state;
  2015. },
  2016. /**
  2017. * Returns a "world" axis aligned bounding box of the node.
  2018. * @function
  2019. * @return {cc.Rect}
  2020. */
  2021. getBoundingBoxToWorld: function () {
  2022. var rect = cc.rect(0, 0, this._contentSize.width, this._contentSize.height);
  2023. var trans = this.getNodeToWorldTransform();
  2024. rect = cc.rectApplyAffineTransform(rect, this.getNodeToWorldTransform());
  2025. //query child's BoundingBox
  2026. if (!this._children)
  2027. return rect;
  2028. var locChildren = this._children;
  2029. for (var i = 0; i < locChildren.length; i++) {
  2030. var child = locChildren[i];
  2031. if (child && child._visible) {
  2032. var childRect = child._getBoundingBoxToCurrentNode(trans);
  2033. if (childRect)
  2034. rect = cc.rectUnion(rect, childRect);
  2035. }
  2036. }
  2037. return rect;
  2038. },
  2039. _getBoundingBoxToCurrentNode: function (parentTransform) {
  2040. var rect = cc.rect(0, 0, this._contentSize.width, this._contentSize.height);
  2041. var trans = (parentTransform == null) ? this.getNodeToParentTransform() : cc.affineTransformConcat(this.getNodeToParentTransform(), parentTransform);
  2042. rect = cc.rectApplyAffineTransform(rect, trans);
  2043. //query child's BoundingBox
  2044. if (!this._children)
  2045. return rect;
  2046. var locChildren = this._children;
  2047. for (var i = 0; i < locChildren.length; i++) {
  2048. var child = locChildren[i];
  2049. if (child && child._visible) {
  2050. var childRect = child._getBoundingBoxToCurrentNode(trans);
  2051. if (childRect)
  2052. rect = cc.rectUnion(rect, childRect);
  2053. }
  2054. }
  2055. return rect;
  2056. },
  2057. _getNodeToParentTransformForWebGL: function () {
  2058. var _t = this;
  2059. if (_t._transformDirty) {
  2060. // Translate values
  2061. var x = _t._position.x;
  2062. var y = _t._position.y;
  2063. var apx = _t._anchorPointInPoints.x, napx = -apx;
  2064. var apy = _t._anchorPointInPoints.y, napy = -apy;
  2065. var scx = _t._scaleX, scy = _t._scaleY;
  2066. if (_t._ignoreAnchorPointForPosition) {
  2067. x += apx;
  2068. y += apy;
  2069. }
  2070. // Rotation values
  2071. // Change rotation code to handle X and Y
  2072. // If we skew with the exact same value for both x and y then we're simply just rotating
  2073. var cx = 1, sx = 0, cy = 1, sy = 0;
  2074. if (_t._rotationX !== 0 || _t._rotationY !== 0) {
  2075. cx = Math.cos(-_t._rotationRadiansX);
  2076. sx = Math.sin(-_t._rotationRadiansX);
  2077. cy = Math.cos(-_t._rotationRadiansY);
  2078. sy = Math.sin(-_t._rotationRadiansY);
  2079. }
  2080. var needsSkewMatrix = ( _t._skewX || _t._skewY );
  2081. // optimization:
  2082. // inline anchor point calculation if skew is not needed
  2083. // Adjusted transform calculation for rotational skew
  2084. if (!needsSkewMatrix && (apx !== 0 || apy !== 0)) {
  2085. x += cy * napx * scx + -sx * napy * scy;
  2086. y += sy * napx * scx + cx * napy * scy;
  2087. }
  2088. // Build Transform Matrix
  2089. // Adjusted transform calculation for rotational skew
  2090. var t = _t._transform;
  2091. t.a = cy * scx;
  2092. t.b = sy * scx;
  2093. t.c = -sx * scy;
  2094. t.d = cx * scy;
  2095. t.tx = x;
  2096. t.ty = y;
  2097. // XXX: Try to inline skew
  2098. // If skew is needed, apply skew and then anchor point
  2099. if (needsSkewMatrix) {
  2100. t = cc.affineTransformConcat({a: 1.0, b: Math.tan(cc.degreesToRadians(_t._skewY)),
  2101. c: Math.tan(cc.degreesToRadians(_t._skewX)), d: 1.0, tx: 0.0, ty: 0.0}, t);
  2102. // adjust anchor point
  2103. if (apx !== 0 || apy !== 0)
  2104. t = cc.affineTransformTranslate(t, napx, napy);
  2105. }
  2106. if (_t._additionalTransformDirty) {
  2107. t = cc.affineTransformConcat(t, _t._additionalTransform);
  2108. _t._additionalTransformDirty = false;
  2109. }
  2110. _t._transform = t;
  2111. _t._transformDirty = false;
  2112. }
  2113. return _t._transform;
  2114. },
  2115. _updateColor: function(){
  2116. //TODO
  2117. },
  2118. /**
  2119. * Returns the opacity of Node
  2120. * @function
  2121. * @returns {number} opacity
  2122. */
  2123. getOpacity: function () {
  2124. return this._realOpacity;
  2125. },
  2126. /**
  2127. * Returns the displayed opacity of Node,
  2128. * the difference between displayed opacity and opacity is that displayed opacity is calculated based on opacity and parent node's opacity when cascade opacity enabled.
  2129. * @function
  2130. * @returns {number} displayed opacity
  2131. */
  2132. getDisplayedOpacity: function () {
  2133. return this._displayedOpacity;
  2134. },
  2135. /**
  2136. * Sets the opacity of Node
  2137. * @function
  2138. * @param {Number} opacity
  2139. */
  2140. setOpacity: function (opacity) {
  2141. this._displayedOpacity = this._realOpacity = opacity;
  2142. var parentOpacity = 255, locParent = this._parent;
  2143. if (locParent && locParent.cascadeOpacity)
  2144. parentOpacity = locParent.getDisplayedOpacity();
  2145. this.updateDisplayedOpacity(parentOpacity);
  2146. this._displayedColor.a = this._realColor.a = opacity;
  2147. },
  2148. /**
  2149. * Update displayed opacity
  2150. * @function
  2151. * @param {Number} parentOpacity
  2152. */
  2153. updateDisplayedOpacity: function (parentOpacity) {
  2154. this._displayedOpacity = this._realOpacity * parentOpacity / 255.0;
  2155. if (this._cascadeOpacityEnabled) {
  2156. var selChildren = this._children;
  2157. for (var i = 0; i < selChildren.length; i++) {
  2158. var item = selChildren[i];
  2159. if (item)
  2160. item.updateDisplayedOpacity(this._displayedOpacity);
  2161. }
  2162. }
  2163. },
  2164. /**
  2165. * Returns whether node's opacity value affect its child nodes.
  2166. * @function
  2167. * @returns {boolean}
  2168. */
  2169. isCascadeOpacityEnabled: function () {
  2170. return this._cascadeOpacityEnabled;
  2171. },
  2172. /**
  2173. * Enable or disable cascade opacity, if cascade enabled, child nodes' opacity will be the multiplication of parent opacity and its own opacity.
  2174. * @function
  2175. * @param {boolean} cascadeOpacityEnabled
  2176. */
  2177. setCascadeOpacityEnabled: function (cascadeOpacityEnabled) {
  2178. if (this._cascadeOpacityEnabled === cascadeOpacityEnabled)
  2179. return;
  2180. this._cascadeOpacityEnabled = cascadeOpacityEnabled;
  2181. if (cascadeOpacityEnabled)
  2182. this._enableCascadeOpacity();
  2183. else
  2184. this._disableCascadeOpacity();
  2185. },
  2186. _enableCascadeOpacity: function () {
  2187. var parentOpacity = 255, locParent = this._parent;
  2188. if (locParent && locParent.cascadeOpacity)
  2189. parentOpacity = locParent.getDisplayedOpacity();
  2190. this.updateDisplayedOpacity(parentOpacity);
  2191. },
  2192. _disableCascadeOpacity: function () {
  2193. this._displayedOpacity = this._realOpacity;
  2194. var selChildren = this._children;
  2195. for (var i = 0; i < selChildren.length; i++) {
  2196. var item = selChildren[i];
  2197. if (item)
  2198. item.updateDisplayedOpacity(255);
  2199. }
  2200. },
  2201. /**
  2202. * Returns the color of Node
  2203. * @function
  2204. * @returns {cc.Color}
  2205. */
  2206. getColor: function () {
  2207. var locRealColor = this._realColor;
  2208. return cc.color(locRealColor.r, locRealColor.g, locRealColor.b, locRealColor.a);
  2209. },
  2210. /**
  2211. * Returns the displayed color of Node,
  2212. * the difference between displayed color and color is that displayed color is calculated based on color and parent node's color when cascade color enabled.
  2213. * @function
  2214. * @returns {cc.Color}
  2215. */
  2216. getDisplayedColor: function () {
  2217. var tmpColor = this._displayedColor;
  2218. return cc.color(tmpColor.r, tmpColor.g, tmpColor.b, tmpColor.a);
  2219. },
  2220. /**
  2221. * <p>Sets the color of Node.<br/>
  2222. * When color doesn't include opacity value like cc.color(128,128,128), this function only change the color. <br/>
  2223. * When color include opacity like cc.color(128,128,128,100), then this function will change the color and the opacity.</p>
  2224. * @function
  2225. * @param {cc.Color} color The new color given
  2226. */
  2227. setColor: function (color) {
  2228. var locDisplayedColor = this._displayedColor, locRealColor = this._realColor;
  2229. locDisplayedColor.r = locRealColor.r = color.r;
  2230. locDisplayedColor.g = locRealColor.g = color.g;
  2231. locDisplayedColor.b = locRealColor.b = color.b;
  2232. var parentColor, locParent = this._parent;
  2233. if (locParent && locParent.cascadeColor)
  2234. parentColor = locParent.getDisplayedColor();
  2235. else
  2236. parentColor = cc.color.WHITE;
  2237. this.updateDisplayedColor(parentColor);
  2238. /*if (color.a !== undefined && !color.a_undefined) { //setColor doesn't support changing opacity, please use setOpacity
  2239. this.setOpacity(color.a);
  2240. }*/
  2241. },
  2242. /**
  2243. * Update the displayed color of Node
  2244. * @function
  2245. * @param {cc.Color} parentColor
  2246. */
  2247. updateDisplayedColor: function (parentColor) {
  2248. var locDispColor = this._displayedColor, locRealColor = this._realColor;
  2249. locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0);
  2250. locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0);
  2251. locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0);
  2252. if (this._cascadeColorEnabled) {
  2253. var selChildren = this._children;
  2254. for (var i = 0; i < selChildren.length; i++) {
  2255. var item = selChildren[i];
  2256. if (item)
  2257. item.updateDisplayedColor(locDispColor);
  2258. }
  2259. }
  2260. },
  2261. /**
  2262. * Returns whether node's color value affect its child nodes.
  2263. * @function
  2264. * @returns {boolean}
  2265. */
  2266. isCascadeColorEnabled: function () {
  2267. return this._cascadeColorEnabled;
  2268. },
  2269. /**
  2270. * Enable or disable cascade color, if cascade enabled, child nodes' opacity will be the cascade value of parent color and its own color.
  2271. * @param {boolean} cascadeColorEnabled
  2272. */
  2273. setCascadeColorEnabled: function (cascadeColorEnabled) {
  2274. if (this._cascadeColorEnabled === cascadeColorEnabled)
  2275. return;
  2276. this._cascadeColorEnabled = cascadeColorEnabled;
  2277. if (this._cascadeColorEnabled)
  2278. this._enableCascadeColor();
  2279. else
  2280. this._disableCascadeColor();
  2281. },
  2282. _enableCascadeColor: function () {
  2283. var parentColor , locParent = this._parent;
  2284. if (locParent && locParent.cascadeColor)
  2285. parentColor = locParent.getDisplayedColor();
  2286. else
  2287. parentColor = cc.color.WHITE;
  2288. this.updateDisplayedColor(parentColor);
  2289. },
  2290. _disableCascadeColor: function () {
  2291. var locDisplayedColor = this._displayedColor, locRealColor = this._realColor;
  2292. locDisplayedColor.r = locRealColor.r;
  2293. locDisplayedColor.g = locRealColor.g;
  2294. locDisplayedColor.b = locRealColor.b;
  2295. var selChildren = this._children, whiteColor = cc.color.WHITE;
  2296. for (var i = 0; i < selChildren.length; i++) {
  2297. var item = selChildren[i];
  2298. if (item)
  2299. item.updateDisplayedColor(whiteColor);
  2300. }
  2301. },
  2302. /**
  2303. * Set whether color should be changed with the opacity value,
  2304. * useless in cc.Node, but this function is overrided in some class to have such behavior.
  2305. * @function
  2306. * @param {Boolean} value
  2307. */
  2308. setOpacityModifyRGB: function (opacityValue) {
  2309. },
  2310. /**
  2311. * Get whether color should be changed with the opacity value
  2312. * @function
  2313. * @return {Boolean}
  2314. */
  2315. isOpacityModifyRGB: function () {
  2316. return false;
  2317. }
  2318. });
  2319. /**
  2320. * Allocates and initializes a node.
  2321. * @deprecated since v3.0, please use new construction instead.
  2322. * @see cc.Node
  2323. * @return {cc.Node}
  2324. */
  2325. cc.Node.create = function () {
  2326. return new cc.Node();
  2327. };
  2328. cc.Node._StateCallbackType = {onEnter: 1, onExit: 2, cleanup: 3, onEnterTransitionDidFinish: 4, updateTransform: 5, onExitTransitionDidStart: 6, sortAllChildren: 7};
  2329. if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
  2330. //redefine cc.Node
  2331. var _p = cc.Node.prototype;
  2332. _p.ctor = function () {
  2333. this._initNode();
  2334. };
  2335. _p.setNodeDirty = function () {
  2336. var _t = this;
  2337. _t._setNodeDirtyForCache();
  2338. _t._transformDirty === false && (_t._transformDirty = _t._inverseDirty = true);
  2339. };
  2340. _p.visit = function (ctx) {
  2341. var _t = this;
  2342. // quick return if not visible
  2343. if (!_t._visible)
  2344. return;
  2345. //visit for canvas
  2346. var context = ctx || cc._renderContext, i;
  2347. var children = _t._children, child;
  2348. context.save();
  2349. _t.transform(context);
  2350. var len = children.length;
  2351. if (len > 0) {
  2352. _t.sortAllChildren();
  2353. // draw children zOrder < 0
  2354. for (i = 0; i < len; i++) {
  2355. child = children[i];
  2356. if (child._localZOrder < 0)
  2357. child.visit(context);
  2358. else
  2359. break;
  2360. }
  2361. _t.draw(context);
  2362. for (; i < len; i++) {
  2363. children[i].visit(context);
  2364. }
  2365. } else
  2366. _t.draw(context);
  2367. this._cacheDirty = false;
  2368. _t.arrivalOrder = 0;
  2369. context.restore();
  2370. };
  2371. _p.transform = function (ctx) {
  2372. // transform for canvas
  2373. var context = ctx || cc._renderContext, eglViewer = cc.view;
  2374. var t = this.getNodeToParentTransform();
  2375. context.transform(t.a, t.c, t.b, t.d, t.tx * eglViewer.getScaleX(), -t.ty * eglViewer.getScaleY());
  2376. };
  2377. _p.getNodeToParentTransform = function () {
  2378. var _t = this;
  2379. if (_t._transformDirty) {
  2380. var t = _t._transform;// quick reference
  2381. // base position
  2382. t.tx = _t._position.x;
  2383. t.ty = _t._position.y;
  2384. // rotation Cos and Sin
  2385. var Cos = 1, Sin = 0;
  2386. if (_t._rotationX) {
  2387. Cos = Math.cos(_t._rotationRadiansX);
  2388. Sin = Math.sin(_t._rotationRadiansX);
  2389. }
  2390. // base abcd
  2391. t.a = t.d = Cos;
  2392. t.b = -Sin;
  2393. t.c = Sin;
  2394. var lScaleX = _t._scaleX, lScaleY = _t._scaleY;
  2395. var appX = _t._anchorPointInPoints.x, appY = _t._anchorPointInPoints.y;
  2396. // Firefox on Vista and XP crashes
  2397. // GPU thread in case of scale(0.0, 0.0)
  2398. var sx = (lScaleX < 0.000001 && lScaleX > -0.000001) ? 0.000001 : lScaleX,
  2399. sy = (lScaleY < 0.000001 && lScaleY > -0.000001) ? 0.000001 : lScaleY;
  2400. // skew
  2401. if (_t._skewX || _t._skewY) {
  2402. // offset the anchorpoint
  2403. var skx = Math.tan(-_t._skewX * Math.PI / 180);
  2404. var sky = Math.tan(-_t._skewY * Math.PI / 180);
  2405. if(skx === Infinity){
  2406. skx = 99999999;
  2407. }
  2408. if(sky === Infinity){
  2409. sky = 99999999;
  2410. }
  2411. var xx = appY * skx * sx;
  2412. var yy = appX * sky * sy;
  2413. t.a = Cos + -Sin * sky;
  2414. t.b = Cos * skx + -Sin;
  2415. t.c = Sin + Cos * sky;
  2416. t.d = Sin * skx + Cos;
  2417. t.tx += Cos * xx + -Sin * yy;
  2418. t.ty += Sin * xx + Cos * yy;
  2419. }
  2420. // scale
  2421. if (lScaleX !== 1 || lScaleY !== 1) {
  2422. t.a *= sx;
  2423. t.c *= sx;
  2424. t.b *= sy;
  2425. t.d *= sy;
  2426. }
  2427. // adjust anchorPoint
  2428. t.tx += Cos * -appX * sx + -Sin * appY * sy;
  2429. t.ty -= Sin * -appX * sx + Cos * appY * sy;
  2430. // if ignore anchorPoint
  2431. if (_t._ignoreAnchorPointForPosition) {
  2432. t.tx += appX;
  2433. t.ty += appY;
  2434. }
  2435. if (_t._additionalTransformDirty) {
  2436. _t._transform = cc.affineTransformConcat(t, _t._additionalTransform);
  2437. _t._additionalTransformDirty = false;
  2438. }
  2439. _t._transformDirty = false;
  2440. }
  2441. return _t._transform;
  2442. };
  2443. _p = null;
  2444. } else {
  2445. cc.assert(typeof cc._tmp.WebGLCCNode === "function", cc._LogInfos.MissingFile, "BaseNodesWebGL.js");
  2446. cc._tmp.WebGLCCNode();
  2447. delete cc._tmp.WebGLCCNode;
  2448. }
  2449. cc.assert(typeof cc._tmp.PrototypeCCNode === "function", cc._LogInfos.MissingFile, "BaseNodesPropertyDefine.js");
  2450. cc._tmp.PrototypeCCNode();
  2451. delete cc._tmp.PrototypeCCNode;