edLayout.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class edLayout extends cc.Component {
  10. @property(cc.Label)
  11. label: cc.Label = null;
  12. Click: cc.Node = null;
  13. // LIFE-CYCLE CALLBACKS:
  14. // onLoad () {}
  15. start() {
  16. this.schedule(() => {
  17. let temp = this.node.children
  18. for (let index = 0; index < temp.length; index++) {
  19. const element = temp[index];
  20. if (element.childrenCount <= 0) {
  21. let tempNOde = new cc.Node
  22. tempNOde.parent = element
  23. let mylabel = tempNOde.addComponent(cc.Label)
  24. mylabel.fontSize = 80
  25. mylabel.lineHeight = 80
  26. mylabel.string = index + ''
  27. }
  28. }
  29. }, 1, 9999999999999)
  30. }
  31. onLoad() {
  32. //绑定事件
  33. //因为摇杆很小,如果给摇杆绑定事件玩家将很难控制,摇杆的背景比较大,所以把事件都绑定在背景上是不错的选择
  34. this.node.on(cc.Node.EventType.TOUCH_START, this.starttt, this);//当手指在背景上移动时触发move事件
  35. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.move, this);//当手指在背景上移动时触发move事件
  36. this.node.on(cc.Node.EventType.TOUCH_END, this.finish, this);//当手指在目标节点区域内离开屏幕时触发finish事件
  37. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.finish, this);//当手指在目标节点区域外离开屏幕时触发finish事件
  38. }
  39. startpos: cc.Vec2
  40. starttt(event: cc.Event.EventTouch) {
  41. this.startpos = event.getLocation()
  42. }
  43. move(event: cc.Event.EventTouch) {//负责移动摇杆 手指移动时调用
  44. // this.node.x += (event.getLocationX() - this.startpos.x) * 0.1
  45. // this.node.y += (event.getLocationY() - this.startpos.y) * 0.1
  46. this.node.x += event.getDeltaX()
  47. this.node.y += event.getDeltaY()
  48. }
  49. finish() {//摇杆弹回原位置
  50. }
  51. initx(x) {
  52. console.log(x);
  53. this.heng = x
  54. this.init()
  55. }
  56. inity(y) {
  57. console.log(y);
  58. this.shu = y
  59. this.init()
  60. }
  61. heng: number = 0
  62. shu: number = 0
  63. init() {
  64. if (!this.heng || !this.shu) {
  65. return
  66. }
  67. this.node.setContentSize(750 * this.heng, 1334 * this.shu)
  68. let total = this.heng * this.shu
  69. let background = this.node.getChildByName("background")
  70. this.node.children.forEach((e, i) => {
  71. if (i != 0) {
  72. e.destroy()
  73. }
  74. })
  75. for (let index = 0; index < total; index++) {
  76. if (index == 0) {
  77. background.on('touchstart', () => {
  78. console.log(index);
  79. window['selectNode'] && (window['selectNode'].opacity = 255)
  80. background.opacity = 180
  81. window['selectNode'] = background
  82. })
  83. let Label = background.getChildByName("Label")
  84. Label.getComponent(cc.Label).string = index.toString()
  85. continue
  86. } else {
  87. let temp = cc.instantiate(background)
  88. temp.parent = this.node
  89. let Label = temp.getChildByName("Label")
  90. Label.getComponent(cc.Label).string = index.toString()
  91. temp.on('touchstart', () => {
  92. console.log(index);
  93. window['selectNode'] && (window['selectNode'].opacity = 255)
  94. window['selectNode'] = temp
  95. temp.opacity = 180
  96. })
  97. }
  98. }
  99. }
  100. hengjiayi() {
  101. let my_laye = this.node.getComponent(cc.Layout)
  102. my_laye.startAxis = cc.Layout.AxisDirection.HORIZONTAL
  103. var a = cc.instantiate(this.node.children[this.node.childrenCount - 1]);
  104. a.parent = this.node
  105. }
  106. hengjianyi() {
  107. let my_laye = this.node.getComponent(cc.Layout)
  108. my_laye.startAxis = cc.Layout.AxisDirection.HORIZONTAL
  109. this.node.children[this.node.childrenCount - 1].destroy()
  110. }
  111. shujiayi() {
  112. let my_laye = this.node.getComponent(cc.Layout)
  113. my_laye.startAxis = cc.Layout.AxisDirection.VERTICAL
  114. var a = cc.instantiate(this.node.children[this.node.childrenCount - 1]);
  115. a.parent = this.node
  116. }
  117. shujianyi() {
  118. let my_laye = this.node.getComponent(cc.Layout)
  119. my_laye.startAxis = cc.Layout.AxisDirection.VERTICAL
  120. this.node.children[this.node.childrenCount - 1].destroy()
  121. }
  122. /////
  123. shang() {
  124. this.node.y += 20
  125. }
  126. xia() {
  127. this.node.y -= 20
  128. }
  129. zuo() {
  130. this.node.x -= 20
  131. }
  132. you() {
  133. this.node.x += 20
  134. }
  135. shanchu() {
  136. if (window['selectfootstep']) {
  137. window['selectfootstep'].destroy()
  138. }
  139. }
  140. out() {
  141. // 将JavaScript对象转换为JSON字符串
  142. let tempmap = this.node.children
  143. let subMapData = {
  144. map: [],
  145. mapX: 0,
  146. mapY: 0,
  147. }
  148. for (let index = 0; index < tempmap.length; index++) {
  149. const element = tempmap[index];
  150. let Mapdata = {
  151. bg: '',
  152. fs: []
  153. }
  154. Mapdata.bg = (element.getComponent(cc.Sprite).spriteFrame.name).replace(/[^0-9]/ig, "")
  155. for (let index1 = 1; index1 < element.children.length; index1++) {
  156. const prop = element.children[index1];
  157. Mapdata.fs.push(
  158. {
  159. sp: prop.getComponent(cc.Sprite).spriteFrame.name.replace(/[^0-9]/ig, ""),
  160. // sp: prop.getComponent(cc.Sprite).spriteFrame.name,
  161. ro:prop.rotation,
  162. sc:prop.scale,
  163. x: prop.position.x.toFixed(2),
  164. y: prop.position.y.toFixed(2)
  165. }
  166. )
  167. }
  168. subMapData.map.push(Mapdata)
  169. }
  170. subMapData.mapX = this.heng
  171. subMapData.mapY = this.shu
  172. console.log(subMapData);
  173. const jsonData = JSON.stringify(subMapData);
  174. console.log(jsonData);
  175. this.saveForBrowser(jsonData, 'map1')
  176. }
  177. /**
  178. * 存字符串内容到文件。
  179. * @param textToWrite - 要保存的文件内容
  180. * @param fileNameToSaveAs - 要保存的文件名
  181. */
  182. private saveForBrowser(textToWrite, fileNameToSaveAs) {
  183. let textFileAsBlob = new Blob([textToWrite], { type: 'application/json' });
  184. let downloadLink = document.createElement("a");
  185. downloadLink.download = fileNameToSaveAs;
  186. downloadLink.innerHTML = "Download File";
  187. if (window.webkitURL != null) {
  188. downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
  189. }
  190. downloadLink.click();
  191. }
  192. }