ITeye博客

十四 手游开发神器 cocos2d-x editor 之串联游戏流程

makeapp628 · 2014-02-22 10:20 · 移动开发 · Markdown

一般游戏都包括几个流程,加载界面,开始界面,选关界面,主界面,暂停界面等等;这节我将这些流程都串联起来;

 

代码下载:http://www.kuaipan.cn/file/id_25348935635745200.htm?source=1

 

loading界面;

LoadingLayer.ccbx

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Document
  3. jsControlled="true"
  4. jsController=""
  5. resolution="default"
  6. >
  7. <Resolutions>
  8. <Resolution centeredOrigin="false" ext="iphone" height="1280" width="720" name="default" scale="1"/>
  9. <Resolution centeredOrigin="false" ext="iphone" height="720" width="1280" name="default1" scale="1"/>
  10. </Resolutions>
  11. <Animations>
  12. <Animation autoPlay="true"
  13. id="0"
  14. name="Default Timeline"
  15. length="10"
  16. chainedId="0"
  17. offset="0.0"
  18. position="0.0"
  19. resolution="30"
  20. scale="128">
  21. <CallbackChannel>
  22. </CallbackChannel>
  23. <SoundChannel>
  24. </SoundChannel>
  25. </Animation>
  26. </Animations>
  27. <Layer
  28. positionX="0" positionY="0"
  29. width="100" height="100" sizeType="Percent"
  30. anchorPointX="0.5" anchorPointY="0.5" ignoreAnchorPoint="true"
  31. scaleX="1" scaleY="1"
  32. touchEnabled="true" positionType="LeftBottom" target="Owner">
  33. <LayerColor positionType="LeftBottom" width="720" height="1280" positionX="0" positionY="0" anchorPointX="0"
  34. anchorPointY="0" color="#ffff40b8" target="Doc"/>
  35. <Sprite positionType="LeftBottom" width="245.0" height="72.0" positionX="381.0" positionY="661.0" anchorPointX="0.5"
  36. anchorPointY="0.5" src="Resources/snow_packer.plist/sa_loading_0001.png"
  37. target="Owner"/>
  38. </Layer>
  39. </Document>

 

 

开始界面;

StartLayer.ccbx

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Document
  3. jsControlled="true"
  4. jsController="StartLayer"
  5. resolution="default"
  6. >
  7. <Resolutions>
  8. <Resolution centeredOrigin="false" ext="iphone" height="1280" width="720" name="default" scale="1"/>
  9. <Resolution centeredOrigin="false" ext="iphone" height="720" width="1280" name="default1" scale="1"/>
  10. </Resolutions>
  11. <Animations>
  12. <Animation autoPlay="true"
  13. id="0"
  14. name="Default Timeline"
  15. length="10"
  16. chainedId="0"
  17. offset="0.0"
  18. position="0.0"
  19. resolution="30"
  20. scale="128">
  21. <CallbackChannel>
  22. </CallbackChannel>
  23. <SoundChannel>
  24. </SoundChannel>
  25. </Animation>
  26. </Animations>
  27. <Layer
  28. positionX="0" positionY="0"
  29. width="100" height="100" sizeType="Percent"
  30. anchorPointX="0.5" anchorPointY="0.5" ignoreAnchorPoint="true"
  31. scaleX="1" scaleY="1"
  32. touchEnabled="true" positionType="LeftBottom" visible="true">
  33. <LayerColor positionType="LeftBottom" width="720" height="1280" positionX="0" positionY="0" anchorPointX="0"
  34. anchorPointY="0" color="#ff10ff9e"/>
  35. <Menu positionType="LeftBottom" width="40" height="40" positionX="354.0" positionY="712.0" anchorPointX="0.5"
  36. anchorPointY="0.5">
  37. <MenuItem positionType="LeftBottom" width="203" height="129" positionX="0" positionY="0" anchorPointX="0.5"
  38. anchorPointY="0.5" onClick="onPlayClicked" normalImage="Resources/snow_packer.plist/m_play.png"
  39. target="Doc" selectedImage="Resources/snow_packer.plist/m_play.png"/>
  40. </Menu>
  41. </Layer>
  42. </Document>

StartLayer.js

 

 

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. //
  2. // CleanerScoreScene class
  3. //
  4. var StartLayer = function () {
  5. cc.log("StartLayer")
  6. this.LoadingLayer = this.LoadingLayer || {};
  7. this.passTime = 0;
  8. this.goStart = false;
  9. };
  10. StartLayer.prototype.onDidLoadFromCCB = function () {
  11. //    this.rootNode.onUpdate = function (dt)
  12. //    {
  13. //        this.controller.onUpdate();
  14. //    };
  15. //    this.rootNode.schedule(this.rootNode.onUpdate);
  16. if (sys.platform == 'browser') {
  17. this.onEnter();
  18. }
  19. else {
  20. this.rootNode.onEnter = function () {
  21. this.controller.onEnter();
  22. };
  23. }
  24. this.rootNode.onExit = function () {
  25. this.controller.onExit();
  26. };
  27. this.rootNode.schedule(function (dt) {
  28. this.controller.onUpdate(dt);
  29. });
  30. };
  31. StartLayer.prototype.onEnter = function () {
  32. this.LoadingLayer = cc.BuilderReader.loadAsNodeFrom("", "LoadingLayer", this);
  33. this.LoadingLayer.setPosition(cc.p(0, 0));
  34. this.LoadingLayer.setZOrder(200);
  35. this.rootNode.addChild(this.LoadingLayer);
  36. //  cc.Director.getInstance().pause();
  37. /*this.LoadingLayer.scheduleOnce(this.removeLoading, 1);*/
  38. this.goStart = true;
  39. this.startTime = this.passTime;
  40. }
  41. StartLayer.prototype.removeLoading = function () {
  42. // if (this.LoadingLayer) {
  43. cc.log("removeLoading");
  44. // cc.Director.getInstance().resume();
  45. this.LoadingLayer.removeFromParent();
  46. }
  47. StartLayer.prototype.onUpdate = function (dt) {
  48. this.passTime += dt;
  49. if (this.passTime - this.startTime > 3 && this.goStart) {
  50. this.removeLoading();
  51. this.goStart = false;
  52. }
  53. }
  54. StartLayer.prototype.onPlayClicked = function () {
  55. cc.BuilderReader.runScene("", "GameSelectLayer");
  56. }
  57. StartLayer.prototype.onExit = function () {
  58. }

 

 

选关界面

GameSelectLayer.ccbx

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Document
  3. jsControlled="true"
  4. jsController="GameSelectLayer"
  5. resolution="default"
  6. >
  7. <Resolutions>
  8. <Resolution centeredOrigin="false" ext="iphone" height="1280" width="720" name="default" scale="1"/>
  9. <Resolution centeredOrigin="false" ext="iphone" height="720" width="1280" name="default1" scale="1"/>
  10. </Resolutions>
  11. <Animations>
  12. <Animation autoPlay="true"
  13. id="0"
  14. name="Default Timeline"
  15. length="10"
  16. chainedId="0"
  17. offset="0.0"
  18. position="0.0"
  19. resolution="30"
  20. scale="128">
  21. <CallbackChannel>
  22. </CallbackChannel>
  23. <SoundChannel>
  24. </SoundChannel>
  25. </Animation>
  26. </Animations>
  27. <Layer
  28. positionX="0" positionY="0"
  29. width="100" height="100" sizeType="Percent"
  30. anchorPointX="0.5" anchorPointY="0.5" ignoreAnchorPoint="true"
  31. scaleX="1" scaleY="1"
  32. touchEnabled="true" positionType="LeftBottom">
  33. <LayerColor positionType="LeftBottom" width="720" height="1280" positionX="0" positionY="0" anchorPointX="0"
  34. anchorPointY="0" color="#ff20b4ff"/>
  35. <Sprite name="bg_g" var="b1" target="Doc" positionX="360" positionY="641"
  36. positionType="LeftBottom" anchorPointX="0.5" anchorPointY="0.5"
  37. scaleX="1" scaleY="1" scaleType="Absolute" ignoreAnchorPoint="false"
  38. src="Resources/s_bg_grade.png" visible="false" color="#ffffffff"/>
  39. <Sprite positionType="LeftBottom" width="587.0" height="834" positionX="361.0" positionY="689.0" anchorPointX="0.5"
  40. anchorPointY="0.5" src="Resources/s_bg_grade.png">
  41. <Node positionType="LeftBottom" width="40" height="100" positionX="42.0" positionY="42.0" anchorPointX="0"
  42. anchorPointY="0" target="Doc" var="listTop">
  43. <ScrollView name="levelScrollPanel"
  44. var="levelScrollPanel" target="Doc" positionX="0" positionY="100"
  45. positionType="LeftBottom" width="510" height="500"
  46. sizeType="Absolute" anchorPointX="0" anchorPointY="0"
  47. scaleX="1" scaleY="1"
  48. clipsToBounds="true"
  49. direction="Horizontal"
  50. bounces="true"
  51. ignoreAnchorPoint="true" visible="true" touchEnabled="true" mouseEnabled="true">
  52. </ScrollView></Node>
  53. <Node positionType="LeftBottom" width="40" height="40" positionX="268.0" positionY="73.0" anchorPointX="0.5"
  54. anchorPointY="0.5" var="dotNode" target="Doc"/>
  55. </Sprite>
  56. </Layer>
  57. </Document>

GameSelectLayer.js

 

 

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. GAME_LEVEL = 0;
  2. var GameSelectLayer = function () {
  3. cc.log("GameSelectLayer");
  4. this.levelScrollPanel = this.levelScrollPanel || {};
  5. this.pageIndex = 0;
  6. this.pageMax = 4;
  7. this.dotNode = this.dotNode || {};
  8. this.start = false;
  9. };
  10. GameSelectLayer.prototype.onDidLoadFromCCB = function () {
  11. if (sys.platform == 'browser') {
  12. this.onEnter();
  13. }
  14. else {
  15. this.rootNode.onEnter = function () {
  16. this.controller.onEnter();
  17. };
  18. }
  19. this.rootNode.onExit = function () {
  20. this.controller.onExit();
  21. };
  22. this.rootNode.onTouchesBegan = function (touches, event) {
  23. this.controller.onTouchesBegan(touches, event);
  24. return true;
  25. };
  26. this.rootNode.onTouchesMoved = function (touches, event) {
  27. this.controller.onTouchesMoved(touches, event);
  28. return true;
  29. };
  30. this.rootNode.onTouchesEnded = function (touches, event) {
  31. this.controller.onTouchesEnded(touches, event);
  32. return true;
  33. };
  34. this.rootNode.setTouchEnabled(true);
  35. };
  36. GameSelectLayer.prototype.onEnter = function () {
  37. this.maxColumns = 3;
  38. this.columnSize = 177;
  39. this.maxRows = 5;
  40. this.rowSize = 104;
  41. cc.log("GameSelectLayer");
  42. this.itemDatas = [];
  43. var itemName = "";
  44. var levelPan = cc.Layer.create();
  45. var currentLevel = sys.localStorage.getItem("pb_level");
  46. if (currentLevel == null) {
  47. currentLevel = 0;
  48. }
  49. sys.localStorage.setItem("pb_level_score_1", "S");
  50. currentLevel = 3;
  51. for (var i = 0; i < 50; i++) {
  52. var xIndex = Math.floor(i % 3) + Math.floor(i / 15) * 3;
  53. var yIndex = Math.floor(i / 3 % 5);
  54. var levelScore = "A"
  55. if (i < currentLevel) {
  56. levelScore = sys.localStorage.getItem("pb_level_score_" + i);
  57. if (levelScore == null || levelScore == '') {
  58. levelScore = "A";
  59. }
  60. if ("S" == levelScore) {
  61. itemName = "GameSelectItemScoreS";
  62. }
  63. else {
  64. itemName = "GameSelectItemScore";
  65. }
  66. }
  67. else if (i == currentLevel) {
  68. itemName = "GameSelectItemLevel";
  69. }
  70. else {
  71. itemName = "GameSelectItemLocked";
  72. }
  73. var itemData = {
  74. xIndex: xIndex,
  75. yIndex: yIndex,
  76. index: i,
  77. type: itemName,
  78. onItemAction: function () {
  79. cc.log("onItemAction");
  80. var action = cc.ScaleBy.create(0.5, 0.8);
  81. this.rootNode.runAction(cc.Sequence.create(
  82. action, action.reverse(), cc.DelayTime.create(2)
  83. //  cc.CallFunc.create(this.onItemClicked())
  84. ));
  85. },
  86. onItemClicked: function () {
  87. cc.log("onItemClicked");
  88. GAME_LEVEL = (this.index + 1);
  89. require("MainLayer.js");
  90. cc.BuilderReader.runScene("", "MainLayer");
  91. },
  92. onLockOpened: function () {
  93. }
  94. };
  95. var item = cc.BuilderReader.loadAsNodeFrom("", "GameSelectItemSprite", itemData);
  96. if (item == null) {
  97. continue;
  98. }
  99. item.setPosition(cc.p(this.columnSize * xIndex, this.rowSize * (4 - yIndex)));
  100. item.setZOrder(11);
  101. itemData.rootNode = item;
  102. itemData.controller = this;
  103. this.itemDatas.push(itemData);
  104. itemData.levelNum.setString("" + (i + 1));
  105. levelPan.addChild(item);
  106. }
  107. levelPan.setContentSize(cc.size(this.columnSize * 12, this.rowSize * 5));
  108. this.levelScrollPanel.setTouchEnabled(false);
  109. this.levelScrollPanel.setBounceable(true);
  110. this.levelScrollPanel.setContainer(levelPan);
  111. this.levelScrollPanel.setTouchPriority(-99999);
  112. this.pageDots(0);
  113. }
  114. GameSelectLayer.prototype.onUpdate = function () {
  115. }
  116. GameSelectLayer.prototype.onExit = function () {
  117. }
  118. GameSelectLayer.prototype.onTouchesBegan = function (touches, event) {
  119. cc.log("onTouchesBegan");
  120. this.beganPosition = touches[0].getLocation();
  121. }
  122. GameSelectLayer.prototype.onTouchesMoved = function (touches, event) {
  123. this.movePosition = touches[0].getLocation();
  124. }
  125. GameSelectLayer.prototype.onTouchesEnded = function (touches, event) {
  126. cc.log("onTouchesEnded");
  127. var loc = touches[0].getLocation();
  128. var distanceX = this.beganPosition.x - loc.x;
  129. var x = this.levelScrollPanel.getContentOffset().x;
  130. var y = this.levelScrollPanel.getContentOffset().y;
  131. this.levelScrollPanel.unscheduleAllCallbacks();
  132. if (distanceX > 50) {
  133. if (this.pageIndex < 4) {
  134. this.pageIndex += 1;
  135. }
  136. this.pageDots(this.pageIndex);
  137. }
  138. else if (distanceX < -50) {
  139. if (this.pageIndex > 0) {
  140. this.pageIndex -= 1;
  141. }
  142. this.pageDots(this.pageIndex);
  143. }
  144. else {
  145. this.onItemClicked(loc);
  146. }
  147. this.levelScrollPanel.setContentOffsetInDuration(cc.p(-this.columnSize * 3 * this.pageIndex, y), 0.5);
  148. }
  149. GameSelectLayer.prototype.onItemClicked = function (location) {
  150. var x = location.x;
  151. var y = location.y;
  152. if (!isInScroll(location)) {
  153. cc.log("out");
  154. return;
  155. }
  156. var scrollPanelRect = this.levelScrollPanel.getBoundingBox();
  157. var xIndex = Math.floor((x - 110) / this.columnSize) + this.pageIndex * 3;
  158. var yIndex = 4 - Math.floor((y - 385) / this.rowSize);
  159. cc.log("scrollX==" + scrollPanelRect.x + ",scrollY==" + scrollPanelRect.y);
  160. cc.log("xIndex==" + xIndex + ",yIndex==" + yIndex);
  161. for (var i = 0; i < this.itemDatas.length; i++) {
  162. if (this.itemDatas[i].xIndex == xIndex && this.itemDatas[i].yIndex == yIndex) {
  163. cc.log("click i=" + i);
  164. this.itemDatas[i].onItemClicked();
  165. break;
  166. }
  167. }
  168. }
  169. GameSelectLayer.prototype.pageDots = function (position) {
  170. this.dotNode.removeAllChildren();
  171. for (var i = 0; i < 4; i++) {
  172. var dots = ["s_point.png", "s_point_s.png"];
  173. var type = 0;
  174. if (position == i) {
  175. type = 1;
  176. }
  177. var dotSprite = cc.Sprite.createWithSpriteFrameName(dots[type]);
  178. dotSprite.setAnchorPoint(cc.p(0, 1));
  179. dotSprite.setPosition(cc.p(30 * i, 60));
  180. dotSprite.setZOrder(100);
  181. this.dotNode.addChild(dotSprite);
  182. }
  183. }
  184. function isInScroll(location) {
  185. var x = location.x;
  186. var y = location.y;
  187. if (x > 110 && x < (110 + 510) && y > 385 && y < (385 + 500)) {
  188. return true;
  189. }
  190. return false
  191. }

GameSelectItemSprite.ccbx

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Document
  3. jsControlled="true"
  4. jsController=""
  5. resolution="default"
  6. >
  7. <Resolutions>
  8. <Resolution centeredOrigin="false" ext="iphone" height="1280" width="720" name="default" scale="1"/>
  9. <Resolution centeredOrigin="false" ext="iphone" height="720" width="1280" name="default1" scale="1"/>
  10. </Resolutions>
  11. <Animations>
  12. <Animation autoPlay="true"
  13. id="0"
  14. name="Default Timeline"
  15. length="10"
  16. chainedId="0"
  17. offset="0.0"
  18. position="0.0"
  19. resolution="30"
  20. scale="128">
  21. <CallbackChannel>
  22. </CallbackChannel>
  23. <SoundChannel>
  24. </SoundChannel>
  25. </Animation>
  26. </Animations>
  27. <Sprite
  28. positionX="0" positionY="0"
  29. sizeType="Absolute"
  30. anchorPointX="0.5" anchorPointY="0.5" ignoreAnchorPoint="true"
  31. scaleX="1.0" scaleY="1.0"
  32. positionType="LeftBottom" src="Resources/snow_packer.plist/s_bun_red_normal.png" target="Owner" var="bgSprite" width="156" height="83"
  33. color="#ff15ffa0">
  34. <LayerColor positionType="LeftBottom" width="156" height="83" positionX="0" positionY="0" anchorPointX="0"
  35. anchorPointY="0" color="#ff0cffa2" visible="true"/>
  36. <Sprite positionType="LeftBottom" width="77.0" height="77.0" positionX="113.0" positionY="40.0" anchorPointX="0.5"
  37. anchorPointY="0.5" src="Resources/snow_packer.plist/m_star_s.png"
  38. scaleX="0.6" scaleY="0.6"/>
  39. <LabelBMFont positionType="LeftBottom" width="40" height="40" positionX="39.0" positionY="56.0" anchorPointX="0.5"
  40. anchorPointY="0.5" text="2" src="Resources/fonts/s_number_gold.fnt" var="levelNum" target="Owner" name="levelNum"
  41. scaleX="1.5" scaleY="1.5"/>
  42. </Sprite>
  43. </Document>

 

 

主游戏场景;

MainLayer.ccbx

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Document
  3. jsControlled="true"
  4. jsController="MainLayer"
  5. resolution="default"
  6. >
  7. <Resolutions>
  8. <Resolution centeredOrigin="false" ext="iphone" height="1280" width="720" name="default" scale="1"/>
  9. <Resolution centeredOrigin="false" ext="iphone" height="720" width="1280" name="default1" scale="1"/>
  10. </Resolutions>
  11. <Animations>
  12. <Animation autoPlay="true"
  13. id="0"
  14. name="Default Timeline"
  15. length="10"
  16. chainedId="0"
  17. offset="0.0"
  18. position="0.0"
  19. resolution="30"
  20. scale="128">
  21. <CallbackChannel>
  22. </CallbackChannel>
  23. <SoundChannel>
  24. </SoundChannel>
  25. </Animation>
  26. </Animations>
  27. <Layer
  28. positionX="0" positionY="0.0"
  29. sizeType="Percent"
  30. width="100" height="100"
  31. anchorPointX="0.5" anchorPointY="0.5" ignoreAnchorPoint="true"
  32. scaleX="1" scaleY="1"
  33. >
  34. <Sprite positionType="LeftBottom" width="720" height="1280" positionX="0" positionY="0"
  35. anchorPointX="0"
  36. anchorPointY="0" src="Resources/m_bg_main.jpg">
  37. <Sprite positionType="LeftBottom" width="182.0" height="238.0" positionX="117.0" positionY="1133.0" anchorPointX="0.5"
  38. anchorPointY="0.5" src="Resources/snow_packer.plist/m_indicator2.png"/>
  39. <LabelTTF positionType="LeftBottom" width="200" height="50" positionX="303.0" positionY="1177.0" anchorPointX="0.5"
  40. anchorPointY="0.5" text="分数:" fontSize="30" vAlign="Center" hAlign="Top" color="#ffff3219"
  41. visible="false"/>
  42. <LabelBMFont positionType="LeftBottom" width="40" height="40" positionX="241.0" positionY="1068.0" anchorPointX="0.5"
  43. anchorPointY="0.5" src="Resources/fonts/score.fnt" text="123" scaleX="1" scaleY="1"
  44. var="scoreLabel" target="Doc" visible="false"/>
  45. <Sprite positionType="LeftBottom" width="113.0" height="120.0" positionX="350.0" positionY="747.0" anchorPointX="0.5"
  46. anchorPointY="0.5" src="Resources/snow_packer.plist/m_monster_g_0001.png"
  47. var="monster" target="Doc"/>
  48. <Menu positionType="LeftBottom" width="100" height="100" positionX="608.0" positionY="1166.0" anchorPointX="0.5"
  49. anchorPointY="0.5">
  50. <MenuItem positionType="LeftBottom" width="107" height="112" positionX="13.0" positionY="18.0"
  51. anchorPointX="0.5"
  52. anchorPointY="0.5" normalImage="Resources/snow_packer.plist/m_bun_pause_s.png"
  53. onClick="onPauseClicked" target="Doc"/>
  54. </Menu>
  55. </Sprite>
  56. <Menu positionType="LeftBottom" width="40" height="40" positionX="371.0" positionY="299.0" anchorPointX="0.5"
  57. anchorPointY="0.5" scaleX="2.4" scaleY="1.725" visible="false">
  58. <MenuItem positionType="LeftBottom" width="40" height="40" positionX="0" positionY="0"
  59. anchorPointX="0"
  60. anchorPointY="0"
  61. normalImage="Resources/CloseNormal.png"
  62. selectedImage="Resources/CloseSelected.png" disabledImage="Resources/CloseSelected.png"
  63. target="Doc" onClick="onExitClicked"/>
  64. </Menu>
  65. </Layer>
  66. </Document>

MainLayer.js

 

 

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. //
  2. // CleanerScoreScene class
  3. //
  4. var MainLayer = function () {
  5. cc.log("MainLayer")
  6. this.scoreLabel = this.scoreLabel || {};
  7. this.monster = this.monster || {};
  8. this.score = 123;
  9. };
  10. MainLayer.prototype.onDidLoadFromCCB = function () {
  11. if (sys.platform == 'browser') {
  12. this.onEnter();
  13. }
  14. else {
  15. this.rootNode.onEnter = function () {
  16. this.controller.onEnter();
  17. };
  18. }
  19. this.rootNode.schedule(function (dt) {
  20. this.controller.onUpdate(dt);
  21. });
  22. this.rootNode.onExit = function () {
  23. this.controller.onExit();
  24. };
  25. this.rootNode.onTouchesBegan = function (touches, event) {
  26. this.controller.onTouchesBegan(touches, event);
  27. return true;
  28. };
  29. this.rootNode.onTouchesMoved = function (touches, event) {
  30. this.controller.onTouchesMoved(touches, event);
  31. return true;
  32. };
  33. this.rootNode.onTouchesEnded = function (touches, event) {
  34. this.controller.onTouchesEnded(touches, event);
  35. return true;
  36. };
  37. this.rootNode.setTouchEnabled(true);
  38. };
  39. MainLayer.prototype.onEnter = function () {
  40. var flowerParticle = cc.ParticleSystem.create("Resources/particles/flower.plist");
  41. flowerParticle.setAnchorPoint(cc.p(0.5, 0.5));
  42. flowerParticle.setPosition(cc.p(60, 160));
  43. flowerParticle.setPositionType(1);
  44. this.monster.addChild(flowerParticle);
  45. cc.AudioEngine.getInstance().playMusic("Resources/sounds/bg_music.mp3", true);
  46. }
  47. MainLayer.prototype.monsterMove = function (x, y) {
  48. this.monster.stopAllActions();
  49. cc.AnimationCache.getInstance().addAnimations("Resources/snow_frame.plist");//添加帧动画文件
  50. var action0 = cc.Sequence.create(cc.MoveTo.create(5, cc.p(x, y)));  //向前移动
  51. var actionFrame = cc.Animate.create(cc.AnimationCache.getInstance().getAnimation("monster"));   //获取帧动画
  52. var action1 = cc.Repeat.create(actionFrame, 90000);
  53. var action2 = cc.Spawn.create(action0, action1); //同步动画
  54. this.monster.runAction(action2);
  55. }
  56. MainLayer.prototype.createParticle = function (name, x, y) {
  57. var particle = cc.ParticleSystem.create("Resources/particles/" + name + ".plist");
  58. particle.setAnchorPoint(cc.p(0.5, 0.5));
  59. particle.setPosition(cc.p(x, y));
  60. particle.setPositionType(1);
  61. particle.setDuration(3);
  62. this.rootNode.addChild(particle);
  63. }
  64. MainLayer.prototype.onUpdate = function (dt) {
  65. this.score += dt;
  66. this.scoreLabel.setString(Math.floor(this.score));
  67. }
  68. MainLayer.prototype.onExitClicked = function () {
  69. cc.log("onExitClicked");
  70. }
  71. MainLayer.prototype.onExit = function () {
  72. cc.log("onExit");
  73. }
  74. MainLayer.prototype.onRenewClicked = function () {  //返回到开始界面
  75. cc.Director.getInstance().resume();
  76. cc.log("onRenewClicked");
  77. cc.BuilderReader.runScene("", "StartLayer");
  78. }
  79. MainLayer.prototype.onSelectClicked = function () {  //选关
  80. cc.Director.getInstance().resume();
  81. cc.log("onListClicked");
  82. cc.BuilderReader.runScene("", "GameSelectLayer");
  83. }
  84. MainLayer.prototype.onReplayClicked = function () {  //新游戏
  85. cc.Director.getInstance().resume();
  86. cc.log("onReplayClicked");
  87. cc.BuilderReader.runScene("", "MainLayer");
  88. }
  89. /*MainLayer.prototype.onReturnClicked = function () {  //返回游戏
  90. cc.log("onReturnClicked");
  91. if (this.paused) {
  92. if (this.pausedLayer) {
  93. this.pausedLayer.removeFromParent();
  94. this.pausedLayer = null;
  95. }
  96. cc.Director.getInstance().resume();
  97. this.paused = false;
  98. }
  99. }*/
  100. MainLayer.prototype.onPauseClicked = function () {   //点击暂停游戏
  101. this.pausedLayer = cc.BuilderReader.loadAsNodeFrom("", "PauseLayer", this);
  102. this.pausedLayer.setPosition(cc.p(0, 0));
  103. this.pausedLayer.setZOrder(200);
  104. this.rootNode.addChild(this.pausedLayer);
  105. this.paused = true;
  106. cc.AudioEngine.getInstance().stopMusic();
  107. cc.Director.getInstance().pause();
  108. }
  109. MainLayer.prototype.onTouchesBegan = function (touches, event) {
  110. var loc = touches[0].getLocation();
  111. }
  112. MainLayer.prototype.onTouchesMoved = function (touches, event) {
  113. cc.log("onTouchesMoved");
  114. }
  115. MainLayer.prototype.onTouchesEnded = function (touches, event) {
  116. cc.log("onTouchesEnded");
  117. var loc = touches[0].getLocation();
  118. cc.AudioEngine.getInstance().playEffect("Resources/sounds/bomb.mp3", false);
  119. this.monsterMove(loc.x, loc.y);
  120. this.createParticle("around", loc.x, loc.y);
  121. }

 

 

暂停界面;

PauseLayer.ccbx

 

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Document
  3. jsControlled="true"
  4. jsController=""
  5. resolution="default"
  6. >
  7. <Resolutions>
  8. <Resolution centeredOrigin="false" ext="iphone" height="1280" width="720" name="default" scale="1"/>
  9. <Resolution centeredOrigin="false" ext="iphone" height="720" width="1280" name="default1" scale="1"/>
  10. </Resolutions>
  11. <Animations>
  12. <Animation autoPlay="true"
  13. id="0"
  14. name="Default Timeline"
  15. length="10"
  16. chainedId="0"
  17. offset="0.0"
  18. position="0.0"
  19. resolution="30"
  20. scale="128">
  21. <CallbackChannel>
  22. </CallbackChannel>
  23. <SoundChannel>
  24. </SoundChannel>
  25. </Animation>
  26. </Animations>
  27. <Layer
  28. positionX="0" positionY="0"
  29. width="100" height="100" sizeType="Percent"
  30. anchorPointX="0.5" anchorPointY="0.5" ignoreAnchorPoint="true"
  31. scaleX="1" scaleY="1"
  32. touchEnabled="true" positionType="LeftBottom" target="Owner" mouseEnabled="true" keyboardEnabled="true">
  33. <LayerColor positionType="LeftBottom" width="720" height="1280" positionX="0" positionY="0" anchorPointX="0"
  34. anchorPointY="0" color="#5757565a" touchEnabled="true" mouseEnabled="true" keyboardEnabled="true"/>
  35. <Sprite positionType="LeftBottom" width="587.0" height="834.0" positionX="362.0" positionY="645.0"
  36. anchorPointX="0.5"
  37. anchorPointY="0.5" src="Resources/s_bg_grade.png">
  38. <!--<LayerColor positionType="LeftBottom" width="600" height="800" positionX="66.0" positionY="234.0"
  39. anchorPointX="0"
  40. anchorPointY="0" color="#ffff55fd" touchEnabled="true" mouseEnabled="true" keyboardEnabled="true"
  41. accelerometerEnabled="true">-->
  42. <Sprite positionType="LeftBottom" width="113.0" height="120.0" positionX="307.0" positionY="588.0"
  43. anchorPointX="0.5"
  44. anchorPointY="0.5" src="Resources/snow_packer.plist/m_monster_g_0002.png"
  45. scaleX="2" scaleY="2"/>
  46. <Menu positionType="LeftBottom" width="40" height="40" positionX="281.0" positionY="259.0"
  47. anchorPointX="0.5"
  48. anchorPointY="0.5">
  49. <MenuItem positionType="LeftBottom" width="99" height="94" positionX="-136.0" positionY="-4.0"
  50. anchorPointX="0.5" anchorPointY="0.5" normalImage="Resources/snow_packer.plist/m_bun_back.png"
  51. target="Owner" onClick="onRenewClicked"
  52. selectedImage="Resources/snow_packer.plist/m_bun_back.png"/>
  53. <MenuItem positionType="LeftBottom" width="99" height="94" positionX="30.0" positionY="-3.0"
  54. anchorPointX="0.5"
  55. anchorPointY="0.5" normalImage="Resources/snow_packer.plist/m_bun_again.png" target="Owner"
  56. onClick="onReplayClicked" selectedImage="Resources/snow_packer.plist/m_bun_again.png"/>
  57. <MenuItem positionType="LeftBottom" width="99" height="94" positionX="201.0" positionY="-1.0"
  58. anchorPointX="0.5" anchorPointY="0.5" normalImage="Resources/snow_packer.plist/m_bun_list.png"
  59. target="Owner" onClick="onSelectClicked"
  60. selectedImage="Resources/snow_packer.plist/m_bun_list.png"/>
  61. </Menu>
  62. <!--</LayerColor>-->
  63. </Sprite>
  64. </Layer>
  65. </Document>

最后主函数Main,js,Main.xml;

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>name</key>
  6. <string>Game</string>
  7. <key>language</key>
  8. <string>Javascript</string>
  9. <key>main</key>
  10. <string>main.js</string>
  11. <key>designWidth</key>
  12. <string>720</string>
  13. <key>designHeight</key>
  14. <string>1280</string>
  15. <key>designPolicy</key>
  16. <string>NO_BORDER</string>
  17. <key>viewWidth</key>
  18. <string>360</string>
  19. <key>viewHeight</key>
  20. <string>640</string>
  21. <key>debug</key>
  22. <string>false</string>
  23. </dict>
  24. </plist>

 

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. if (sys.platform == 'browser') {
  2. var require = function (file) {
  3. var d = document;
  4. var s = d.createElement('script');
  5. s.src = file;
  6. d.body.appendChild(s);
  7. }
  8. } else {
  9. require("jsb.js");
  10. }
  11. cc.debug = function (msg) {
  12. cc.log(msg);
  13. }
  14. cc.BuilderReader.replaceScene = function (path, ccbName) {
  15. var scene = cc.BuilderReader.loadAsSceneFrom(path, ccbName);
  16. cc.Director.getInstance().replaceScene(scene);
  17. return scene;
  18. }
  19. cc.BuilderReader.loadAsScene = function (file, owner, parentSize) {
  20. var node = cc.BuilderReader.load(file, owner, parentSize);
  21. var scene = cc.Scene.create();
  22. scene.addChild(node);
  23. return scene;
  24. };
  25. cc.BuilderReader.loadAsSceneFrom = function (path, ccbName) {
  26. cc.BuilderReader.setResourcePath(path + "/");
  27. return cc.BuilderReader.loadAsScene(path + "/" + ccbName);
  28. }
  29. cc.BuilderReader.loadAsNodeFrom = function (path, ccbName, owner) {
  30. cc.BuilderReader.setResourcePath(path + "/");
  31. return cc.BuilderReader.load(path + "/" + ccbName, owner);
  32. }
  33. cc.BuilderReader.runScene = function (module, name) {
  34. var director = cc.Director.getInstance();
  35. var scene = cc.BuilderReader.loadAsSceneFrom(module, name);
  36. var runningScene = director.getRunningScene();
  37. if (runningScene === null) {
  38. cc.log("runWithScene");
  39. director.runWithScene(scene);
  40. }
  41. else {
  42. cc.log("replaceScene");
  43. director.replaceScene(scene);
  44. }
  45. }
  46. var ccb_resources = [
  47. {type: 'image', src: "Resources/HelloWorld.png"},
  48. {type: 'image', src: "Resources/CloseNormal.png"},
  49. {type: 'image', src: "Resources/CloseSelected.png"}
  50. ];
  51. require("MainLayer.js");
  52. require("StartLayer.js");
  53. require("GameSelectLayer.js");
  54. if (sys.platform == 'browser') {
  55. var Cocos2dXApplication = cc.Application.extend({
  56. config: document['ccConfig'],
  57. ctor: function () {
  58. this._super();
  59. cc.COCOS2D_DEBUG = this.config['COCOS2D_DEBUG'];
  60. cc.initDebugSetting();
  61. cc.setup(this.config['tag']);
  62. cc.AppController.shareAppController().didFinishLaunchingWithOptions();
  63. },
  64. applicationDidFinishLaunching: function () {
  65. var director = cc.Director.getInstance();
  66. // director->enableRetinaDisplay(true);
  67. // director.setDisplayStats(this.config['showFPS']);
  68. // set FPS. the default value is 1.0/60 if you don't call this
  69. director.setAnimationInterval(1.0 / this.config['frameRate']);
  70. var glView = director.getOpenGLView();
  71. glView.setDesignResolutionSize(1280, 720, cc.RESOLUTION_POLICY.SHOW_ALL);
  72. cc.Loader.preload(ccb_resources, function () {
  73. cc.BuilderReader.runScene("", "StartLayer");
  74. }, this);
  75. return true;
  76. }
  77. });
  78. var myApp = new Cocos2dXApplication();
  79. } else {
  80. cc.BuilderReader.runScene("", "StartLayer");
  81. }

 

 

 

下一篇文章 我会介绍cocos2d-x  editor的其他特性    笔者(李元友)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。