shop.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. var ShopLayer = cc.Layer.extend({
  2. sprite: null,
  3. ctor: function () {
  4. var self = this;
  5. self._super();
  6. self.y = winSize.height;
  7. var STOP_PRICE = 30;
  8. var SINGLE_PRICE = 10;
  9. var SAME_PRICE = 50;
  10. var scoreArea = ccui.Layout.create();
  11. scoreArea.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  12. scoreArea.setBackGroundColor(cc.color(111, 168, 233));
  13. scoreArea.setSize(cc.size(494, 150));
  14. scoreArea.x = 72;
  15. scoreArea.y = -194;
  16. self.addChild(scoreArea);
  17. var yourScoreLabel = ccui.Text.create();
  18. yourScoreLabel.attr({
  19. anchorX: 0,
  20. color: cc.color(255, 255, 255),
  21. fontSize: 38,
  22. x: 20,
  23. y: 114,
  24. textAlign: cc.TEXT_ALIGNMENT_LEFT,
  25. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  26. string: Localize[lang]['yourScore'] + ':'
  27. });
  28. scoreArea.addChild(yourScoreLabel);
  29. var yourScore = ccui.Text.create();
  30. yourScore.attr({
  31. color: cc.color(255, 210, 0),
  32. fontSize: 58,
  33. x: 250,
  34. y: 50,
  35. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  36. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  37. string: totalScore
  38. });
  39. scoreArea.addChild(yourScore);
  40. var line = ccui.Layout.create();
  41. line.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  42. line.setBackGroundColor(cc.color(208, 208, 208));
  43. line.setContentSize(cc.size(496, 2));
  44. line.attr({
  45. x: 72,
  46. y: -221
  47. });
  48. self.addChild(line);
  49. var showPopup = function (tool) {
  50. curTool = tool;
  51. alpha.enabled = alpha.visible = buyPopup.enabled = buyPopup.visible = true;
  52. amountText.string = 1;
  53. };
  54. var closePopup = function () {
  55. alpha.enabled = alpha.visible = buyPopup.enabled = buyPopup.visible = false;
  56. };
  57. var curTool;
  58. // stop
  59. var stopArea = ccui.Layout.create();
  60. stopArea.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  61. stopArea.setBackGroundColor(cc.color(255, 255, 255));
  62. stopArea.setSize(cc.size(494, 150));
  63. stopArea.x = 72;
  64. stopArea.y = -384;
  65. self.addChild(stopArea);
  66. var stopIcon = ccui.ImageView.create();
  67. stopIcon.loadTexture(res.stop, texType);
  68. stopIcon.attr({
  69. x: 35,
  70. y: 110
  71. });
  72. stopArea.addChild(stopIcon);
  73. var stopCount = ccui.Text.create();
  74. stopCount.attr({
  75. color: cc.color(0, 0, 0),
  76. fontSize: 32,
  77. x: 35,
  78. y: 30,
  79. textAlign: cc.TEXT_ALIGNMENT_LEFT,
  80. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  81. string: stopLeft
  82. });
  83. stopArea.addChild(stopCount);
  84. var stopText = ccui.Text.create();
  85. stopText.attr({
  86. anchorX: 0,
  87. anchorY: 1,
  88. color: cc.color(150, 150, 150),
  89. fontSize: 22,
  90. x: 80,
  91. y: 138,
  92. textAlign: cc.TEXT_ALIGNMENT_LEFT,
  93. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_TOP,
  94. string: Localize[lang]['stop']
  95. });
  96. stopText.setTextAreaSize(cc.size(400, 70));
  97. stopArea.addChild(stopText);
  98. var stopBuyBtn = ccui.Button.create();
  99. stopBuyBtn.setScale9Enabled(true);
  100. stopBuyBtn.setCapInsets(cc.rect(1, 1, 1, 1));
  101. stopBuyBtn.loadTextures(res.btn4, res.btn4, null, texType);
  102. stopBuyBtn.setContentSize(cc.size(150, 50));
  103. stopBuyBtn.setPressedActionEnabled(true);
  104. stopBuyBtn.attr({
  105. x: 412,
  106. y: 31
  107. });
  108. stopBuyBtn.addTouchEventListener(function (sender, type) {
  109. if (type == ccui.Widget.TOUCH_ENDED) {
  110. showPopup('stop');
  111. }
  112. }, stopArea);
  113. stopArea.addChild(stopBuyBtn);
  114. var stopBuyText = ccui.Text.create();
  115. stopBuyText.attr({
  116. color: cc.color(255, 255, 255),
  117. fontSize: 22,
  118. x: 76,
  119. y: 36,
  120. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  121. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  122. string: Localize[lang]['buy']
  123. });
  124. stopBuyBtn.addChild(stopBuyText);
  125. var stopPrice = ccui.Text.create();
  126. stopPrice.attr({
  127. color: cc.color(255, 210, 0),
  128. fontSize: 18,
  129. x: 76,
  130. y: 12,
  131. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  132. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  133. string: STOP_PRICE
  134. });
  135. stopBuyBtn.addChild(stopPrice);
  136. // single
  137. var singleArea = ccui.Layout.create();
  138. singleArea.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  139. singleArea.setBackGroundColor(cc.color(255, 255, 255));
  140. singleArea.setSize(cc.size(494, 150));
  141. singleArea.x = 72;
  142. singleArea.y = -556;
  143. self.addChild(singleArea);
  144. var singleIcon = ccui.ImageView.create();
  145. singleIcon.loadTexture(res.single, texType);
  146. singleIcon.attr({
  147. x: 35,
  148. y: 110
  149. });
  150. singleArea.addChild(singleIcon);
  151. var singleCount = ccui.Text.create();
  152. singleCount.attr({
  153. color: cc.color(0, 0, 0),
  154. fontSize: 32,
  155. x: 35,
  156. y: 30,
  157. textAlign: cc.TEXT_ALIGNMENT_LEFT,
  158. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  159. string: singleLeft
  160. });
  161. singleArea.addChild(singleCount);
  162. var singleText = ccui.Text.create();
  163. singleText.attr({
  164. anchorX: 0,
  165. anchorY: 1,
  166. color: cc.color(150, 150, 150),
  167. fontSize: 22,
  168. x: 80,
  169. y: 138,
  170. textAlign: cc.TEXT_ALIGNMENT_LEFT,
  171. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_TOP,
  172. string: Localize[lang]['single1']
  173. });
  174. singleText.setTextAreaSize(cc.size(400, 70));
  175. singleArea.addChild(singleText);
  176. var singleBuyBtn = ccui.Button.create();
  177. singleBuyBtn.setScale9Enabled(true);
  178. singleBuyBtn.setCapInsets(cc.rect(1, 1, 1, 1));
  179. singleBuyBtn.loadTextures(res.btn4, res.btn4, null, texType);
  180. singleBuyBtn.setContentSize(cc.size(150, 50));
  181. singleBuyBtn.setPressedActionEnabled(true);
  182. singleBuyBtn.attr({
  183. x: 412,
  184. y: 31
  185. });
  186. singleBuyBtn.addTouchEventListener(function (sender, type) {
  187. if (type == ccui.Widget.TOUCH_ENDED) {
  188. showPopup('single');
  189. }
  190. }, singleArea);
  191. singleArea.addChild(singleBuyBtn);
  192. var singleBuyText = ccui.Text.create();
  193. singleBuyText.attr({
  194. color: cc.color(255, 255, 255),
  195. fontSize: 22,
  196. x: 76,
  197. y: 36,
  198. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  199. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  200. string: Localize[lang]['buy']
  201. });
  202. singleBuyBtn.addChild(singleBuyText);
  203. var singlePrice = ccui.Text.create();
  204. singlePrice.attr({
  205. color: cc.color(255, 210, 0),
  206. fontSize: 18,
  207. x: 76,
  208. y: 12,
  209. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  210. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  211. string: SINGLE_PRICE
  212. });
  213. singleBuyBtn.addChild(singlePrice);
  214. // same
  215. var sameArea = ccui.Layout.create();
  216. sameArea.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  217. sameArea.setBackGroundColor(cc.color(255, 255, 255));
  218. sameArea.setSize(cc.size(494, 150));
  219. sameArea.x = 72;
  220. sameArea.y = -726;
  221. self.addChild(sameArea);
  222. var sameIcon = ccui.ImageView.create();
  223. sameIcon.loadTexture(res.same, texType);
  224. sameIcon.attr({
  225. x: 35,
  226. y: 110
  227. });
  228. sameArea.addChild(sameIcon);
  229. var sameCount = ccui.Text.create();
  230. sameCount.attr({
  231. color: cc.color(0, 0, 0),
  232. fontSize: 32,
  233. x: 35,
  234. y: 30,
  235. textAlign: cc.TEXT_ALIGNMENT_LEFT,
  236. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  237. string: sameLeft
  238. });
  239. sameArea.addChild(sameCount);
  240. var sameText = ccui.Text.create();
  241. sameText.attr({
  242. anchorX: 0,
  243. anchorY: 1,
  244. color: cc.color(150, 150, 150),
  245. fontSize: 22,
  246. x: 80,
  247. y: 138,
  248. textAlign: cc.TEXT_ALIGNMENT_LEFT,
  249. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_TOP,
  250. string: Localize[lang]['same']
  251. });
  252. sameText.setTextAreaSize(cc.size(400, 70));
  253. sameArea.addChild(sameText);
  254. var sameBuyBtn = ccui.Button.create();
  255. sameBuyBtn.setScale9Enabled(true);
  256. sameBuyBtn.setCapInsets(cc.rect(1, 1, 1, 1));
  257. sameBuyBtn.loadTextures(res.btn4, res.btn4, null, texType);
  258. sameBuyBtn.setContentSize(cc.size(150, 50));
  259. sameBuyBtn.setPressedActionEnabled(true);
  260. sameBuyBtn.attr({
  261. x: 412,
  262. y: 31
  263. });
  264. sameBuyBtn.addTouchEventListener(function (sender, type) {
  265. if (type == ccui.Widget.TOUCH_ENDED) {
  266. showPopup('same');
  267. }
  268. }, sameArea);
  269. sameArea.addChild(sameBuyBtn);
  270. var sameBuyText = ccui.Text.create();
  271. sameBuyText.attr({
  272. color: cc.color(255, 255, 255),
  273. fontSize: 22,
  274. x: 76,
  275. y: 36,
  276. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  277. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  278. string: Localize[lang]['buy']
  279. });
  280. sameBuyBtn.addChild(sameBuyText);
  281. var samePrice = ccui.Text.create();
  282. samePrice.attr({
  283. color: cc.color(255, 210, 0),
  284. fontSize: 18,
  285. x: 76,
  286. y: 12,
  287. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  288. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  289. string: SAME_PRICE
  290. });
  291. sameBuyBtn.addChild(samePrice);
  292. var checkScore = function () {
  293. stopBuyBtn.setTouchEnabled(totalScore >= STOP_PRICE);
  294. singleBuyBtn.setTouchEnabled(totalScore >= SINGLE_PRICE);
  295. sameBuyBtn.setTouchEnabled(totalScore >= SAME_PRICE);
  296. };
  297. checkScore();
  298. var alpha = ccui.Layout.create();
  299. alpha.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  300. alpha.setBackGroundColor(cc.color(0, 0, 0));
  301. alpha.setBackGroundColorOpacity(128);
  302. alpha.setSize(winSize);
  303. alpha.attr({
  304. y: -winSize.height,
  305. zIndex: 100,
  306. touchEnabled: true,
  307. enabled: false,
  308. visible: false
  309. });
  310. self.addChild(alpha);
  311. var buyPopup = ccui.Layout.create();
  312. buyPopup.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  313. buyPopup.setBackGroundColor(cc.color(246, 246, 246));
  314. buyPopup.setSize(cc.size(462, 270));
  315. buyPopup.attr({
  316. x: winSize.width / 2 - 231,
  317. y: -winSize.height / 2 - 135,
  318. zIndex: 101
  319. });
  320. self.addChild(buyPopup);
  321. var minusBtn = ccui.Button.create();
  322. minusBtn.loadTextures(res.minus, res.minus, null, texType);
  323. minusBtn.setPressedActionEnabled(true);
  324. minusBtn.attr({
  325. x: 102,
  326. y: 180
  327. });
  328. minusBtn.addTouchEventListener(function (sender, type) {
  329. if (type == ccui.Widget.TOUCH_ENDED) {
  330. if (amountText.string > 1) {
  331. amountText.string = (amountText.string >> 0) - 1;
  332. }
  333. }
  334. }, buyPopup);
  335. buyPopup.addChild(minusBtn);
  336. var amountBg = ccui.Layout.create();
  337. amountBg.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
  338. amountBg.setBackGroundColor(cc.color(255, 255, 255));
  339. amountBg.setSize(cc.size(150, 70));
  340. amountBg.attr({
  341. x: 151,
  342. y: 145
  343. });
  344. buyPopup.addChild(amountBg);
  345. var amountText = ccui.Text.create();
  346. amountText.attr({
  347. color: cc.color(144, 144, 144),
  348. fontSize: 38,
  349. x: 224,
  350. y: 178,
  351. textAlign: cc.TEXT_ALIGNMENT_CENTER,
  352. verticalAlign: cc.VERTICAL_TEXT_ALIGNMENT_CENTER,
  353. string: 1
  354. });
  355. buyPopup.addChild(amountText);
  356. var addBtn = ccui.Button.create();
  357. addBtn.loadTextures(res.add, res.add, null, texType);
  358. addBtn.setPressedActionEnabled(true);
  359. addBtn.attr({
  360. x: 353,
  361. y: 180
  362. });
  363. addBtn.addTouchEventListener(function (sender, type) {
  364. if (type == ccui.Widget.TOUCH_ENDED) {
  365. var price;
  366. switch (curTool) {
  367. case 'stop':
  368. price = STOP_PRICE;
  369. break;
  370. case 'single':
  371. price = SINGLE_PRICE;
  372. break;
  373. case 'same':
  374. price = SAME_PRICE;
  375. break;
  376. }
  377. var amount = (amountText.string >> 0) + 1;
  378. if (totalScore >= price * amount) {
  379. amountText.string = amount;
  380. }
  381. }
  382. }, buyPopup);
  383. buyPopup.addChild(addBtn);
  384. var buyBtn = ccui.Button.create();
  385. buyBtn.setScale9Enabled(true);
  386. buyBtn.setCapInsets(cc.rect(1, 1, 1, 1));
  387. buyBtn.loadTextures(res.btn4, res.btn4, null, texType);
  388. buyBtn.setContentSize(cc.size(260, 80));
  389. buyBtn.setTitleText(Localize[lang]['buy']);
  390. buyBtn.setTitleFontSize(38);
  391. buyBtn.setPressedActionEnabled(true);
  392. buyBtn.attr({
  393. x: 230,
  394. y: 68
  395. });
  396. buyBtn.addTouchEventListener(function (sender, type) {
  397. if (type == ccui.Widget.TOUCH_ENDED) {
  398. var price, amount = amountText.string >> 0;
  399. switch (curTool) {
  400. case 'stop':
  401. price = STOP_PRICE;
  402. stopLeft += amount;
  403. stopCount.string = stopLeft;
  404. break;
  405. case 'single':
  406. price = SINGLE_PRICE;
  407. singleLeft += amount;
  408. singleCount.string = singleLeft;
  409. break;
  410. case 'same':
  411. price = SAME_PRICE;
  412. sameLeft += amount;
  413. sameCount.string = sameLeft;
  414. break;
  415. }
  416. totalScore -= price * amount;
  417. yourScore.string = totalScore;
  418. checkScore();
  419. closePopup();
  420. }
  421. }, buyPopup);
  422. buyPopup.addChild(buyBtn);
  423. var closeBtn = ccui.Button.create();
  424. closeBtn.setScale9Enabled(true);
  425. closeBtn.setCapInsets(cc.rect(1, 1, 1, 1));
  426. closeBtn.loadTextures(res.btn4, res.btn4, null, texType);
  427. closeBtn.setContentSize(cc.size(40, 40));
  428. closeBtn.setTitleText('X');
  429. closeBtn.setTitleFontSize(20);
  430. closeBtn.setPressedActionEnabled(true);
  431. closeBtn.attr({
  432. x: 440,
  433. y: 248
  434. });
  435. closeBtn.addTouchEventListener(function (sender, type) {
  436. if (type == ccui.Widget.TOUCH_ENDED) {
  437. closePopup();
  438. }
  439. }, buyPopup);
  440. buyPopup.addChild(closeBtn);
  441. buyPopup.enabled = buyPopup.visible = false;
  442. var startBtn = ccui.Button.create();
  443. startBtn.setScale9Enabled(true);
  444. startBtn.setCapInsets(cc.rect(1, 1, 1, 1));
  445. startBtn.loadTextures(res.btn4, res.btn4, null, texType);
  446. startBtn.setContentSize(cc.size(288, 70));
  447. startBtn.setTitleText(Localize[lang]['start']);
  448. startBtn.setTitleFontSize(40);
  449. startBtn.setPressedActionEnabled(true);
  450. startBtn.attr({
  451. x: winSize.width / 2,
  452. y: -773
  453. });
  454. startBtn.addTouchEventListener(function (sender, type) {
  455. if (type == ccui.Widget.TOUCH_ENDED) {
  456. director.runScene(new LevelScene());
  457. }
  458. }, self);
  459. self.addChild(startBtn);
  460. return true;
  461. }
  462. });
  463. var ShopScene = cc.Scene.extend({
  464. onEnter: function () {
  465. this._super();
  466. var layer = new ShopLayer();
  467. this.addChild(layer);
  468. }
  469. });