ITeye博客

十三 手游开发神器 cocos2d-x editor 之选关滑动界面(ScrollView)

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

这一节,我将实现游戏的选关界面,游戏50关,每一屏15关,总共4屏,左滑动可切换到上一屏,右滑动可切换到下一屏;

 

效果如下;

开始进入选关界面;

 

滑动到最后一屏;

 

点击关卡,进入主界面;

 

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

 

 

首先新建,GameSelectLayer.ccbx,GameSelectLayer.js,指定控制器让两个文件关联;设计选关场景如下,调节参数

 

创建每个单元格关卡GameSelectItemSprite.ccbx,这是一个小精灵层,如下图;

 

 

设计完成后,打开MainLayer.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. }

同时修改Main.js,加入,require("GameSelectLayer.js");;

 

然后修改StartLayer.js,让点击开始后跳转到GameSelectLayer.js;

 

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

 

  1. //
  2. // CleanerScoreScene class
  3. //
  4. var StartLayer = function () {
  5. cc.log("StartLayer")
  6. };
  7. StartLayer.prototype.onDidLoadFromCCB = function () {
  8. //    this.rootNode.onUpdate = function (dt)
  9. //    {
  10. //        this.controller.onUpdate();
  11. //    };
  12. //    this.rootNode.schedule(this.rootNode.onUpdate);
  13. if (sys.platform == 'browser') {
  14. this.onEnter();
  15. }
  16. else {
  17. this.rootNode.onEnter = function () {
  18. this.controller.onEnter();
  19. };
  20. }
  21. this.rootNode.onExit = function () {
  22. this.controller.onExit();
  23. };
  24. };
  25. StartLayer.prototype.onEnter = function () {
  26. }
  27. StartLayer.prototype.onUpdate = function () {
  28. }
  29. StartLayer.prototype.onPlayClicked = function () {
  30. cc.BuilderReader.runScene("", "GameSelectLayer");
  31. }
  32. StartLayer.prototype.onExit = function () {
  33. }

完成后运行,ok,效果最上面;

 

下一篇文章 我会介绍cocos2d-x  editor的串联游戏流程    笔者(李元友)

资料来源:cocos2d-x  editor

评论(0)

暂无评论。

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