GameShop.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //钻石
  2. var diamondInfo = cc.Class({
  3. name: "diamondInfo",
  4. properties: {
  5. diamondSprite: {
  6. default: null,
  7. type: cc.SpriteFrame,
  8. },
  9. diamondPrice: {
  10. default: -1,
  11. type: cc.Integer,
  12. },
  13. }
  14. });
  15. cc.Class({
  16. extends: cc.Component,
  17. properties: {
  18. diamondItem: {
  19. default: null,
  20. type: cc.Node,
  21. serializable: true,
  22. },
  23. diamondContent: {
  24. default: null,
  25. type: cc.Node,
  26. serializable: true,
  27. },
  28. diamondInfos: {
  29. default: [],
  30. type: [diamondInfo]
  31. },
  32. //支付部分
  33. playHead: {
  34. default: null,
  35. type: cc.Sprite,
  36. },
  37. playPrice: {
  38. default: null,
  39. type: cc.Label,
  40. },
  41. playEdit: {
  42. default: null,
  43. type: cc.EditBox,
  44. },
  45. PlayNode:cc.Node
  46. },
  47. // LIFE-CYCLE CALLBACKS:
  48. // onLoad () {},
  49. start() {
  50. for (let index = 0; index < this.diamondInfos.length; index++) {
  51. // const element = array[index];
  52. let diamondItem = cc.instantiate(this.diamondItem);
  53. diamondItem.active = true;
  54. diamondItem.parent = this.diamondContent;
  55. diamondItem.getChildByName('MainSprite').getComponent(cc.Sprite).spriteFrame = this.diamondInfos[index].diamondSprite;
  56. diamondItem.getChildByName('num').getComponent(cc.Label).string = 'x' + this.diamondInfos[index].diamondPrice * 10;
  57. var clickEventHandler = new cc.Component.EventHandler();
  58. clickEventHandler.target = this.node;
  59. clickEventHandler.component = "GameShop";//这个是脚本文件名
  60. clickEventHandler.handler = "onBuyDiamond"; //回调函名称
  61. clickEventHandler.customEventData = this.diamondInfos[index].diamondPrice; //用户数据
  62. var button = diamondItem.getChildByName('buybutton').getComponent(cc.Button); //获取cc.Button组件
  63. button.node.getChildByName('Label').getComponent(cc.Label).string = this.diamondInfos[index].diamondPrice + '元';
  64. button.clickEvents.push(clickEventHandler); //增加处理
  65. }
  66. //如果存在用户信息
  67. if (G.userInfo == null) return;
  68. let _url = G.userInfo.avatarUrl;
  69. //显示头像
  70. cc.loader.load({ url: _url, type: 'jpg' }, function (err, texture) {
  71. var frame = new cc.SpriteFrame(texture);
  72. if (err) {
  73. console.log('支付的头像:', err);
  74. }
  75. this.playHead.getComponent(cc.Sprite).spriteFrame = frame;
  76. }.bind(this));
  77. },
  78. onOpenShop(){
  79. this.node.active = true;
  80. },
  81. onCloseShop(){
  82. this.node.active = false;
  83. },
  84. onBuyDiamond(event, data) {
  85. console.log('购买数目:', data);
  86. //开启支付页面
  87. this.onOpenPlay({price:data})
  88. },
  89. onOpenPlay(data){
  90. this.PlayNode.active = true;
  91. this.playPrice.string = data.price+'.00';
  92. },
  93. onClosePlay(){
  94. this.PlayNode.active = false;
  95. },
  96. //更改密码宽度
  97. onChangePassWord() {
  98. // console.log('密码改变:', this.playEdit.string);
  99. // let string = this.playEdit.string;
  100. // let currentArray = string.split('');
  101. // let endstring = '';
  102. // for (let index = 0; index < currentArray.length; index++) {
  103. // const element = currentArray[index];
  104. // endstring += ' ' + element;
  105. // }
  106. // this.playEdit.string = endstring;
  107. },
  108. // update (dt) {},
  109. });