/samples/TestJavascript/Resources/js/test-sprite.js

https://bitbucket.org/Tsiannian/cocos2d-x · JavaScript · 781 lines · 513 code · 162 blank · 106 comment · 74 complexity · 492d017ff4684d93fc25b646b08ccd25 MD5 · raw file

  1. //
  2. // http://www.cocos2d-iphone.org
  3. //
  4. // Javascript + cocos2d sprite tests
  5. //
  6. require("js/helper.js");
  7. director = cc.Director.getInstance();
  8. winSize = director.getWinSize();
  9. centerPos = cc.p( winSize.width/2, winSize.height/2 );
  10. var scenes = []
  11. var currentScene = 0;
  12. var nextSpriteTestAction = function () {
  13. currentScene = currentScene + 1;
  14. if( currentScene >= scenes.length )
  15. currentScene = 0;
  16. loadScene(currentScene);
  17. };
  18. var backSpriteTestAction = function () {
  19. currentScene = currentScene -1;
  20. if( currentScene < 0 )
  21. currentScene = scenes.length -1;
  22. loadScene(currentScene);
  23. };
  24. var restartSpriteTestAction = function () {
  25. loadScene( currentScene );
  26. };
  27. var loadScene = function (sceneIdx)
  28. {
  29. winSize = director.getWinSize();
  30. centerPos = cc.p( winSize.width/2, winSize.height/2 );
  31. var scene = new cc.Scene();
  32. scene.init();
  33. var layer = new scenes[ sceneIdx ]();
  34. scene.addChild( layer );
  35. // scene.walkSceneGraph(0);
  36. director.replaceScene( scene );
  37. // __jsc__.garbageCollect();
  38. }
  39. //------------------------------------------------------------------
  40. //
  41. // BaseLayer
  42. //
  43. //------------------------------------------------------------------
  44. var BaseLayer = function() {
  45. //
  46. // VERY IMPORTANT
  47. //
  48. // Only subclasses of a native classes MUST call __associateObjectWithNative
  49. // Failure to do so, it will crash.
  50. //
  51. var parent = goog.base(this);
  52. __associateObjWithNative( this, parent );
  53. this.init();
  54. this.title = function () {
  55. return "No title";
  56. }
  57. this.subtitle = function () {
  58. return "No Subtitle";
  59. }
  60. }
  61. goog.inherits(BaseLayer, cc.Layer );
  62. //
  63. // Instance 'base' methods
  64. // XXX: Should be defined after "goog.inherits"
  65. //
  66. BaseLayer.prototype.onEnter = function() {
  67. var label = cc.LabelTTF.create(this.title(), "Arial", 28);
  68. this.addChild(label, 1);
  69. label.setPosition( cc.p(winSize.width / 2, winSize.height - 50));
  70. var strSubtitle = this.subtitle();
  71. if (strSubtitle != "") {
  72. var l = cc.LabelTTF.create(strSubtitle, "Thonburi", 16);
  73. this.addChild(l, 1);
  74. l.setPosition( cc.p(winSize.width / 2, winSize.height - 80));
  75. }
  76. // Menu
  77. var item1 = cc.MenuItemImage.create("Images/b1.png", "Images/b2.png", this, this.backCallback);
  78. var item2 = cc.MenuItemImage.create("Images/r1.png", "Images/r2.png", this, this.restartCallback);
  79. var item3 = cc.MenuItemImage.create("Images/f1.png", "Images/f2.png", this, this.nextCallback);
  80. var item4 = cc.MenuItemFont.create("back", this, function() { require("js/main.js"); } );
  81. cc.MenuItemFont.setFontSize( 22 );
  82. var menu = cc.Menu.create(item1, item2, item3, item4 );
  83. menu.setPosition( cc.p(0,0) );
  84. item1.setPosition( cc.p(winSize.width / 2 - 100, 30));
  85. item2.setPosition( cc.p(winSize.width / 2, 30));
  86. item3.setPosition( cc.p(winSize.width / 2 + 100, 30));
  87. item4.setPosition( cc.p(winSize.width - 60, winSize.height - 30 ) );
  88. this.addChild(menu, 1);
  89. }
  90. BaseLayer.prototype.restartCallback = function (sender) {
  91. cc.log("restart called");
  92. restartSpriteTestAction();
  93. }
  94. BaseLayer.prototype.nextCallback = function (sender) {
  95. cc.log("next called");
  96. nextSpriteTestAction();
  97. }
  98. BaseLayer.prototype.backCallback = function (sender) {
  99. cc.log("back called");
  100. backSpriteTestAction();
  101. }
  102. //------------------------------------------------------------------
  103. //
  104. // Sprite Touch test
  105. //
  106. //------------------------------------------------------------------
  107. var SpriteTouchTest = function() {
  108. goog.base(this);
  109. this.initialize = function() {
  110. var platform = __getPlatform();
  111. if( platform.substring(0,7) == 'desktop' )
  112. this.setMouseEnabled( true );
  113. else if( platform.substring(0,6) == 'mobile' )
  114. this.setTouchEnabled( true );
  115. this.addSprite( centerPos );
  116. }
  117. this.addSprite = function(pos) {
  118. var sprite = this.createSprite( pos );
  119. this.addChild( sprite );
  120. }
  121. this.title = function () {
  122. return "Sprite: Simple action test";
  123. }
  124. this.subtitle = function () {
  125. return "Tap screen to add more sprites";
  126. }
  127. this.initialize();
  128. }
  129. goog.inherits(SpriteTouchTest, BaseLayer );
  130. SpriteTouchTest.prototype.onMouseDown = function( event ) {
  131. this.addSprite( event.getLocation() );
  132. }
  133. SpriteTouchTest.prototype.onTouchesEnded = function( touches, event ) {
  134. var l = touches.length;
  135. for( var i=0; i < l; i++) {
  136. this.addSprite( touches[i].getLocation() );
  137. }
  138. }
  139. SpriteTouchTest.prototype.createSprite = function( pos ) {
  140. var idx = Math.random() * 1400 / 100;
  141. idx = Math.floor( idx );
  142. var x = Math.floor(idx%5) * 85;
  143. var y = Math.floor(idx/5) * 121;
  144. var sprite = cc.Sprite.create("Images/grossini_dance_atlas.png", cc.rect(x,y,85,121) );
  145. sprite.setPosition( pos );
  146. var rand = Math.random();
  147. if( rand < 0.20 ) {
  148. var action = cc.ScaleBy.create(3, 2 );
  149. } else if(rand < 0.40) {
  150. var action = cc.RotateBy.create(3, 360 );
  151. } else if( rand < 0.60) {
  152. var action = cc.Blink.create(1, 3 );
  153. } else if( rand < 0.8 ) {
  154. var action = cc.TintBy.create(2, 0, -255, -255 );
  155. } else {
  156. var action = cc.FadeOut.create( 2 );
  157. }
  158. var action_back = action.reverse();
  159. var seq = cc.Sequence.create(action, action_back);
  160. sprite.runAction( cc.RepeatForever.create( seq ) );
  161. return sprite;
  162. }
  163. //------------------------------------------------------------------
  164. //
  165. // Sprite Batch Touch test
  166. //
  167. //------------------------------------------------------------------
  168. var SpriteBatchTouchTest = function() {
  169. goog.base(this);
  170. this.initialize = function() {
  171. this.batch = cc.SpriteBatchNode.create("Images/grossini_dance_atlas.png", 50 );
  172. this.addChild( this.batch );
  173. var platform = __getPlatform();
  174. if( platform == 'OSX' ) {
  175. this.setIsMouseEnabled( true );
  176. } else if( platform == 'iOS' ) {
  177. this.setIsTouchEnabled( true );
  178. }
  179. }
  180. this.addSprite = function(pos) {
  181. var sprite = this.createSprite( pos );
  182. this.batch.addChild( sprite );
  183. }
  184. this.title = function () {
  185. return "SpriteBatch: Simple action test";
  186. }
  187. this.subtitle = function () {
  188. return "Tap screen to add more sprites";
  189. }
  190. this.initialize();
  191. }
  192. goog.inherits( SpriteBatchTouchTest, SpriteTouchTest );
  193. //------------------------------------------------------------------
  194. //
  195. // Sprite vs. SpriteBatch Animation
  196. //
  197. //------------------------------------------------------------------
  198. var SpriteFrameTest = function() {
  199. goog.base(this);
  200. this.initialize = function() {
  201. var cache = cc.SpriteFrameCache.getInstance();
  202. cache.addSpriteFrames( "animations/grossini.plist" );
  203. cache.addSpriteFrames( "animations/grossini_gray.plist", "animations/grossini_gray.png" );
  204. cache.addSpriteFrames( "animations/grossini_blue.plist", "animations/grossini_blue.png" );
  205. //
  206. // Animation using Sprite batch
  207. //
  208. // A CCSpriteBatchNode can reference one and only one texture (one .png file)
  209. // Sprites that are contained in that texture can be instantiatied as CCSprites and then added to the CCSpriteBatchNode
  210. // All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call
  211. // If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient
  212. //
  213. // When you animate a sprite, CCAnimation changes the frame of the sprite using setDisplayFrame: (this is why the animation must be in the same texture)
  214. // When setDisplayFrame: is used in the CCAnimation it changes the frame to one specified by the CCSpriteFrames that were added to the animation,
  215. // but texture id is still the same and so the sprite is still a child of the CCSpriteBatchNode,
  216. // and therefore all the animation sprites are also drawn as part of the CCSpriteBatchNode
  217. //
  218. var sprite1 = cc.Sprite.createWithSpriteFrameName("grossini_dance_01.png");
  219. sprite1.setPosition( cc.p( winSize.width/2-80, winSize.height/2) );
  220. var spritebatch = cc.SpriteBatchNode.create("animations/grossini.pvr");
  221. spritebatch.addChild(sprite1);
  222. this.addChild( spritebatch );
  223. var frames = []
  224. for( var i = 1; i < 15; i++) {
  225. if( i < 10 ) {
  226. var name = "grossini_dance_0" + i + ".png";
  227. } else {
  228. var name = "grossini_dance_" + i + ".png";
  229. }
  230. var frame = cache.getSpriteFrame( name );
  231. frames.push( frame );
  232. }
  233. var animation = cc.Animation.createWithSpriteFrames( frames, 0.3 );
  234. // 14 frames * 1sec = 14 seconds
  235. sprite1.runAction( cc.RepeatForever.create( cc.Animate.create( animation ) ) );
  236. //
  237. // Animation using standard Sprite
  238. //
  239. //
  240. var sprite2 = cc.Sprite.createWithSpriteFrameName( "grossini_dance_01.png" );
  241. sprite2.setPosition( cc.p( winSize.width/2 + 80, winSize.height/2) );
  242. this.addChild( sprite2 );
  243. var moreFrames = []
  244. for(var i = 1; i < 15; i++) {
  245. if( i < 10 ) {
  246. var name = "grossini_dance_gray_0" + i + ".png";
  247. } else {
  248. var name = "grossini_dance_gray_" + i + ".png";
  249. }
  250. var frame = cache.getSpriteFrame( name );
  251. moreFrames.push( frame );
  252. }
  253. for( var i = 1; i < 5; i++) {
  254. var name = "grossini_blue_0" + i + ".png";
  255. var frame = cache.getSpriteFrame( name );
  256. moreFrames.push( frame );
  257. }
  258. moreFrames.concat( frames );
  259. var animMixed = cc.Animation.createWithSpriteFrames( moreFrames, 0.3 );
  260. // 32 frames * 1 seconds = 32 seconds
  261. sprite2.runAction( cc.RepeatForever.create( cc.Animate.create( animMixed ) ) );
  262. // to test issue #732, uncomment the following line
  263. sprite2.setFlipX( false );
  264. sprite2.setFlipY( false );
  265. }
  266. this.title = function () {
  267. return "Sprite vs. SpriteBatchNode animation";
  268. }
  269. this.subtitle = function () {
  270. return "Testing issue #792";
  271. }
  272. this.initialize();
  273. }
  274. goog.inherits( SpriteFrameTest, BaseLayer );
  275. //------------------------------------------------------------------
  276. //
  277. // SpriteAnchorPoint
  278. //
  279. //------------------------------------------------------------------
  280. var SpriteAnchorPoint = function() {
  281. goog.base(this);
  282. this.initialize = function() {
  283. for(var i=0;i<3;i++) {
  284. var sprite = cc.Sprite.create("Images/grossini_dance_atlas.png", cc.rect(85*i, 121*1, 85, 121) );
  285. sprite.setPosition( cc.p( winSize.width/4*(i+1), winSize.height/2) );
  286. var point = cc.Sprite.create( "Images/r1.png" );
  287. point.setScale( 0.25 );
  288. point.setPosition( sprite.getPosition() );
  289. this.addChild( point, 10 );
  290. if( i == 0 ) {
  291. sprite.setAnchorPoint( cc.p( 0, 0) );
  292. } else if( i == 1 ) {
  293. sprite.setAnchorPoint( cc.p(0.5, 0.5) );
  294. } else if( i == 2 ) {
  295. sprite.setAnchorPoint( cc.p(1,1) );
  296. }
  297. point.setPosition( sprite.getPosition() );
  298. var rotate = cc.RotateBy.create(10, 360);
  299. var action = cc.RepeatForever.create( rotate );
  300. sprite.runAction( action );
  301. this.addChild( sprite, i );
  302. }
  303. }
  304. this.title = function () {
  305. return "Sprite: anchor point";
  306. }
  307. this.subtitle = function () {
  308. return "Testing 3 different anchor points";
  309. }
  310. this.initialize();
  311. }
  312. goog.inherits( SpriteAnchorPoint, BaseLayer );
  313. //------------------------------------------------------------------
  314. //
  315. // SpriteBatchAnchorPoint
  316. //
  317. //------------------------------------------------------------------
  318. var SpriteBatchAnchorPoint = function() {
  319. goog.base(this);
  320. this.initialize = function() {
  321. var batch = cc.SpriteBatchNode.create( "Images/grossini_dance_atlas.png" );
  322. for(var i=0;i<3;i++) {
  323. var sprite = cc.Sprite.create("Images/grossini_dance_atlas.png", cc.rect(85*i, 121*1, 85, 121) );
  324. sprite.setPosition( cc.p( winSize.width/4*(i+1), winSize.height/2) );
  325. var point = cc.Sprite.create( "Images/r1.png" );
  326. point.setScale( 0.25 );
  327. point.setPosition( sprite.getPosition() );
  328. this.addChild( point, 10 );
  329. if( i == 0 ) {
  330. sprite.setAnchorPoint( cc.p( 0, 0) );
  331. } else if( i == 1 ) {
  332. sprite.setAnchorPoint( cc.p(0.5, 0.5) );
  333. } else if( i == 2 ) {
  334. sprite.setAnchorPoint( cc.p(1,1) );
  335. }
  336. point.setPosition( sprite.getPosition() );
  337. var rotate = cc.RotateBy.create(10, 360);
  338. var action = cc.RepeatForever.create( rotate );
  339. sprite.runAction( action );
  340. batch.addChild( sprite, i );
  341. }
  342. this.addChild( batch );
  343. }
  344. this.title = function () {
  345. return "Sprite Batch: anchor point";
  346. }
  347. this.subtitle = function () {
  348. return "Testing 3 different anchor points";
  349. }
  350. this.initialize();
  351. }
  352. goog.inherits( SpriteBatchAnchorPoint, BaseLayer );
  353. //------------------------------------------------------------------
  354. //
  355. // SpriteOffsetAnchorFlip
  356. //
  357. //------------------------------------------------------------------
  358. var SpriteOffsetAnchorFlip = function() {
  359. goog.base(this);
  360. this.initialize = function() {
  361. var cache = cc.SpriteFrameCache.getInstance();
  362. cache.addSpriteFrames("animations/grossini.plist");
  363. cache.addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png");
  364. for(var i=0;i<3;i++) {
  365. var sprite = cc.Sprite.create("Images/grossini_dance_atlas.png", cc.rect(85*i, 121*1, 85, 121) );
  366. sprite.setPosition( cc.p( winSize.width/4*(i+1), winSize.height/2) );
  367. var point = cc.Sprite.create( "Images/r1.png" );
  368. point.setScale( 0.25 );
  369. point.setPosition( sprite.getPosition() );
  370. this.addChild( point, 10 );
  371. if( i == 0 ) {
  372. sprite.setAnchorPoint( cc.p( 0, 0) );
  373. } else if( i == 1 ) {
  374. sprite.setAnchorPoint( cc.p(0.5, 0.5) );
  375. } else if( i == 2 ) {
  376. sprite.setAnchorPoint( cc.p(1,1) );
  377. }
  378. point.setPosition( sprite.getPosition() );
  379. var frames = []
  380. for( var j = 1; j < 15; j++) {
  381. if( j < 10 ) {
  382. var name = "grossini_dance_0" + j + ".png";
  383. } else {
  384. var name = "grossini_dance_" + j + ".png";
  385. }
  386. var frame = cache.getSpriteFrame( name );
  387. frames.push( frame );
  388. }
  389. var animation = cc.Animation.createWithSpriteFrames( frames, 0.3 );
  390. sprite.runAction( cc.RepeatForever.create( cc.Animate.create( animation ) ) );
  391. var flip = cc.FlipY.create( true );
  392. var flip_back = cc.FlipY.create( false );
  393. var delay = cc.DelayTime.create( 1 );
  394. var delay2 = cc.DelayTime.create( 1 );
  395. var seq = cc.Sequence.create( delay, flip, delay2, flip_back );
  396. sprite.runAction( cc.RepeatForever.create( seq ) );
  397. this.addChild( sprite );
  398. }
  399. }
  400. this.title = function () {
  401. return "Sprite offset + anchor + flip";
  402. }
  403. this.subtitle = function () {
  404. return "issue #1078";
  405. }
  406. this.initialize();
  407. }
  408. goog.inherits( SpriteOffsetAnchorFlip, BaseLayer );
  409. //------------------------------------------------------------------
  410. //
  411. // SpriteBatchOffsetAnchorFlip
  412. //
  413. //------------------------------------------------------------------
  414. var SpriteBatchOffsetAnchorFlip = function() {
  415. goog.base(this);
  416. this.initialize = function() {
  417. var cache = cc.SpriteFrameCache.getInstance();
  418. cache.addSpriteFrames("animations/grossini.plist");
  419. cache.addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png");
  420. var batch = cc.SpriteBatchNode.create("animations/grossini.pvr");
  421. for(var i=0;i<3;i++) {
  422. var sprite = cc.Sprite.createWithSpriteFrameName("grossini_dance_01.png");
  423. sprite.setPosition( cc.p( winSize.width/4*(i+1), winSize.height/2) );
  424. var point = cc.Sprite.create( "Images/r1.png" );
  425. point.setScale( 0.25 );
  426. point.setPosition( sprite.getPosition() );
  427. this.addChild( point, 10 );
  428. if( i == 0 ) {
  429. sprite.setAnchorPoint( cc.p( 0, 0) );
  430. } else if( i == 1 ) {
  431. sprite.setAnchorPoint( cc.p(0.5, 0.5) );
  432. } else if( i == 2 ) {
  433. sprite.setAnchorPoint( cc.p(1,1) );
  434. }
  435. point.setPosition( sprite.getPosition() );
  436. var frames = []
  437. for( var j = 1; j < 15; j++) {
  438. if( j < 10 ) {
  439. var name = "grossini_dance_0" + j + ".png";
  440. } else {
  441. var name = "grossini_dance_" + j + ".png";
  442. }
  443. var frame = cache.getSpriteFrame( name );
  444. frames.push( frame );
  445. }
  446. var animation = cc.Animation.createWithSpriteFrames( frames, 0.3 );
  447. sprite.runAction( cc.RepeatForever.create( cc.Animate.create( animation ) ) );
  448. var flip = cc.FlipY.create( true );
  449. var flip_back = cc.FlipY.create( false );
  450. var delay = cc.DelayTime.create( 1 );
  451. var delay2 = cc.DelayTime.create( 1 );
  452. var seq = cc.Sequence.create( delay, flip, delay2, flip_back );
  453. sprite.runAction( cc.RepeatForever.create( seq ) );
  454. batch.addChild( sprite );
  455. }
  456. this.addChild(batch);
  457. }
  458. this.title = function () {
  459. return "SpriteBatch offset + anchor + flip";
  460. }
  461. this.subtitle = function () {
  462. return "issue #1078";
  463. }
  464. this.initialize();
  465. }
  466. goog.inherits( SpriteBatchOffsetAnchorFlip, BaseLayer );
  467. //------------------------------------------------------------------
  468. //
  469. // SpriteColorOpacity
  470. //
  471. //------------------------------------------------------------------
  472. var SpriteColorOpacity = function() {
  473. goog.base(this);
  474. this.initialize = function() {
  475. var sprite1 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 0, 121 * 1, 85, 121));
  476. var sprite2 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 1, 121 * 1, 85, 121));
  477. var sprite3 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 2, 121 * 1, 85, 121));
  478. var sprite4 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 3, 121 * 1, 85, 121));
  479. var sprite5 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 0, 121 * 1, 85, 121));
  480. var sprite6 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 1, 121 * 1, 85, 121));
  481. var sprite7 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 2, 121 * 1, 85, 121));
  482. var sprite8 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 3, 121 * 1, 85, 121));
  483. sprite1.setPosition(cc.p((winSize.width / 5) * 1, (winSize.height / 3) * 1));
  484. sprite2.setPosition(cc.p((winSize.width / 5) * 2, (winSize.height / 3) * 1));
  485. sprite3.setPosition(cc.p((winSize.width / 5) * 3, (winSize.height / 3) * 1));
  486. sprite4.setPosition(cc.p((winSize.width / 5) * 4, (winSize.height / 3) * 1));
  487. sprite5.setPosition(cc.p((winSize.width / 5) * 1, (winSize.height / 3) * 2));
  488. sprite6.setPosition(cc.p((winSize.width / 5) * 2, (winSize.height / 3) * 2));
  489. sprite7.setPosition(cc.p((winSize.width / 5) * 3, (winSize.height / 3) * 2));
  490. sprite8.setPosition(cc.p((winSize.width / 5) * 4, (winSize.height / 3) * 2));
  491. var action = cc.FadeIn.create(2);
  492. var action_back = action.reverse();
  493. var fade = cc.RepeatForever.create( cc.Sequence.create( action, action_back ) );
  494. var tintRed = cc.TintBy.create(2, 0, -255, -255);
  495. // var tintRed = cc.RotateBy.create(2, 360 );
  496. var tintRedBack = tintRed.reverse();
  497. var red = cc.RepeatForever.create(cc.Sequence.create( tintRed, tintRedBack ) );
  498. var tintGreen = cc.TintBy.create(2, -255, 0, -255);
  499. var tintGreenBack = tintGreen.reverse();
  500. var green = cc.RepeatForever.create(cc.Sequence.create( tintGreen, tintGreenBack ) );
  501. var tintBlue = cc.TintBy.create(2, -255, -255, 0);
  502. var tintBlueBack = tintBlue.reverse();
  503. var blue = cc.RepeatForever.create(cc.Sequence.create( tintBlue, tintBlueBack ) );
  504. sprite5.runAction(red);
  505. sprite6.runAction(green);
  506. sprite7.runAction(blue);
  507. sprite8.runAction(fade);
  508. // late add: test dirtyColor and dirtyPosition
  509. this.addChild(sprite1);
  510. this.addChild(sprite2);
  511. this.addChild(sprite3);
  512. this.addChild(sprite4);
  513. this.addChild(sprite5);
  514. this.addChild(sprite6);
  515. this.addChild(sprite7);
  516. this.addChild(sprite8);
  517. }
  518. //
  519. // Instance methods
  520. //
  521. this.title = function () {
  522. return "Sprite: Color & Opacity";
  523. }
  524. this.subtitle = function () {
  525. return "testing opacity and color";
  526. }
  527. this.initialize();
  528. }
  529. goog.inherits(SpriteColorOpacity, BaseLayer );
  530. //------------------------------------------------------------------
  531. //
  532. // SpriteBatchColorOpacity
  533. //
  534. //------------------------------------------------------------------
  535. var SpriteBatchColorOpacity = function() {
  536. goog.base(this);
  537. this.initialize = function() {
  538. var batch = cc.SpriteBatchNode.create('Images/grossini_dance_atlas.png', 10);
  539. var sprite1 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 0, 121 * 1, 85, 121));
  540. var sprite2 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 1, 121 * 1, 85, 121));
  541. var sprite3 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 2, 121 * 1, 85, 121));
  542. var sprite4 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 3, 121 * 1, 85, 121));
  543. var sprite5 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 0, 121 * 1, 85, 121));
  544. var sprite6 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 1, 121 * 1, 85, 121));
  545. var sprite7 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 2, 121 * 1, 85, 121));
  546. var sprite8 = cc.Sprite.create('Images/grossini_dance_atlas.png', cc.rect(85 * 3, 121 * 1, 85, 121));
  547. sprite1.setPosition(cc.p((winSize.width / 5) * 1, (winSize.height / 3) * 1));
  548. sprite2.setPosition(cc.p((winSize.width / 5) * 2, (winSize.height / 3) * 1));
  549. sprite3.setPosition(cc.p((winSize.width / 5) * 3, (winSize.height / 3) * 1));
  550. sprite4.setPosition(cc.p((winSize.width / 5) * 4, (winSize.height / 3) * 1));
  551. sprite5.setPosition(cc.p((winSize.width / 5) * 1, (winSize.height / 3) * 2));
  552. sprite6.setPosition(cc.p((winSize.width / 5) * 2, (winSize.height / 3) * 2));
  553. sprite7.setPosition(cc.p((winSize.width / 5) * 3, (winSize.height / 3) * 2));
  554. sprite8.setPosition(cc.p((winSize.width / 5) * 4, (winSize.height / 3) * 2));
  555. var action = cc.FadeIn.create(2);
  556. var action_back = action.reverse();
  557. var fade = cc.RepeatForever.create( cc.Sequence.create( action, action_back ) );
  558. var tintRed = cc.TintBy.create(2, 0, -255, -255);
  559. // var tintRed = cc.RotateBy.create(2, 360 );
  560. var tintRedBack = tintRed.reverse();
  561. var red = cc.RepeatForever.create(cc.Sequence.create( tintRed, tintRedBack ) );
  562. var tintGreen = cc.TintBy.create(2, -255, 0, -255);
  563. var tintGreenBack = tintGreen.reverse();
  564. var green = cc.RepeatForever.create(cc.Sequence.create( tintGreen, tintGreenBack ) );
  565. var tintBlue = cc.TintBy.create(2, -255, -255, 0);
  566. var tintBlueBack = tintBlue.reverse();
  567. var blue = cc.RepeatForever.create(cc.Sequence.create( tintBlue, tintBlueBack ) );
  568. sprite5.runAction(red);
  569. sprite6.runAction(green);
  570. sprite7.runAction(blue);
  571. sprite8.runAction(fade);
  572. // late add: test dirtyColor and dirtyPosition
  573. this.addChild(batch);
  574. batch.addChild(sprite1);
  575. batch.addChild(sprite2);
  576. batch.addChild(sprite3);
  577. batch.addChild(sprite4);
  578. batch.addChild(sprite5);
  579. batch.addChild(sprite6);
  580. batch.addChild(sprite7);
  581. batch.addChild(sprite8);
  582. }
  583. //
  584. // Instance methods
  585. //
  586. this.title = function () {
  587. return "Sprite Batch: Color & Opacity";
  588. }
  589. this.subtitle = function () {
  590. return "testing opacity and color with batches";
  591. }
  592. this.initialize();
  593. }
  594. goog.inherits(SpriteBatchColorOpacity, BaseLayer );
  595. //
  596. // Order of tests
  597. //
  598. scenes.push( SpriteTouchTest ); scenes.push( SpriteBatchTouchTest );
  599. scenes.push( SpriteFrameTest );
  600. scenes.push( SpriteAnchorPoint ); scenes.push( SpriteBatchAnchorPoint );
  601. scenes.push( SpriteOffsetAnchorFlip ); scenes.push( SpriteBatchOffsetAnchorFlip );
  602. scenes.push( SpriteColorOpacity ); scenes.push( SpriteBatchColorOpacity );
  603. //------------------------------------------------------------------
  604. //
  605. // Main entry point
  606. //
  607. //------------------------------------------------------------------
  608. function run()
  609. {
  610. var scene = cc.Scene.create();
  611. var layer = new scenes[currentScene]();
  612. scene.addChild( layer );
  613. var runningScene = director.getRunningScene();
  614. if( runningScene == null )
  615. director.runWithScene( scene );
  616. else
  617. director.replaceScene( cc.TransitionFade.create(0.5, scene ) );
  618. }
  619. run();