123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- // Learn TypeScript:
- // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
- // Learn Attribute:
- // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class edLayout extends cc.Component {
- @property(cc.Label)
- label: cc.Label = null;
- Click: cc.Node = null;
- // LIFE-CYCLE CALLBACKS:
- // onLoad () {}
- start() {
- this.schedule(() => {
- let temp = this.node.children
- for (let index = 0; index < temp.length; index++) {
- const element = temp[index];
- if (element.childrenCount <= 0) {
- let tempNOde = new cc.Node
- tempNOde.parent = element
- let mylabel = tempNOde.addComponent(cc.Label)
- mylabel.fontSize = 80
- mylabel.lineHeight = 80
- mylabel.string = index + ''
- }
- }
- }, 1, 9999999999999)
- }
- onLoad() {
- //绑定事件
- //因为摇杆很小,如果给摇杆绑定事件玩家将很难控制,摇杆的背景比较大,所以把事件都绑定在背景上是不错的选择
- this.node.on(cc.Node.EventType.TOUCH_START, this.starttt, this);//当手指在背景上移动时触发move事件
- this.node.on(cc.Node.EventType.TOUCH_MOVE, this.move, this);//当手指在背景上移动时触发move事件
- this.node.on(cc.Node.EventType.TOUCH_END, this.finish, this);//当手指在目标节点区域内离开屏幕时触发finish事件
- this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.finish, this);//当手指在目标节点区域外离开屏幕时触发finish事件
- }
- startpos: cc.Vec2
- starttt(event: cc.Event.EventTouch) {
- this.startpos = event.getLocation()
- }
- move(event: cc.Event.EventTouch) {//负责移动摇杆 手指移动时调用
- // this.node.x += (event.getLocationX() - this.startpos.x) * 0.1
- // this.node.y += (event.getLocationY() - this.startpos.y) * 0.1
- this.node.x += event.getDeltaX()
- this.node.y += event.getDeltaY()
- }
- finish() {//摇杆弹回原位置
- }
- initx(x) {
- console.log(x);
- this.heng = x
- this.init()
- }
- inity(y) {
- console.log(y);
- this.shu = y
- this.init()
- }
- heng: number = 0
- shu: number = 0
- init() {
- if (!this.heng || !this.shu) {
- return
- }
- this.node.setContentSize(750 * this.heng, 1334 * this.shu)
- let total = this.heng * this.shu
- let background = this.node.getChildByName("background")
- this.node.children.forEach((e, i) => {
- if (i != 0) {
- e.destroy()
- }
- })
- for (let index = 0; index < total; index++) {
- if (index == 0) {
- background.on('touchstart', () => {
- console.log(index);
- window['selectNode'] && (window['selectNode'].opacity = 255)
- background.opacity = 180
- window['selectNode'] = background
- })
- let Label = background.getChildByName("Label")
- Label.getComponent(cc.Label).string = index.toString()
- continue
- } else {
- let temp = cc.instantiate(background)
- temp.parent = this.node
- let Label = temp.getChildByName("Label")
- Label.getComponent(cc.Label).string = index.toString()
- temp.on('touchstart', () => {
- console.log(index);
- window['selectNode'] && (window['selectNode'].opacity = 255)
- window['selectNode'] = temp
- temp.opacity = 180
- })
- }
- }
- }
- hengjiayi() {
- let my_laye = this.node.getComponent(cc.Layout)
- my_laye.startAxis = cc.Layout.AxisDirection.HORIZONTAL
- var a = cc.instantiate(this.node.children[this.node.childrenCount - 1]);
- a.parent = this.node
- }
- hengjianyi() {
- let my_laye = this.node.getComponent(cc.Layout)
- my_laye.startAxis = cc.Layout.AxisDirection.HORIZONTAL
- this.node.children[this.node.childrenCount - 1].destroy()
- }
- shujiayi() {
- let my_laye = this.node.getComponent(cc.Layout)
- my_laye.startAxis = cc.Layout.AxisDirection.VERTICAL
- var a = cc.instantiate(this.node.children[this.node.childrenCount - 1]);
- a.parent = this.node
- }
- shujianyi() {
- let my_laye = this.node.getComponent(cc.Layout)
- my_laye.startAxis = cc.Layout.AxisDirection.VERTICAL
- this.node.children[this.node.childrenCount - 1].destroy()
- }
- /////
- shang() {
- this.node.y += 20
- }
- xia() {
- this.node.y -= 20
- }
- zuo() {
- this.node.x -= 20
- }
- you() {
- this.node.x += 20
- }
- shanchu() {
- if (window['selectfootstep']) {
- window['selectfootstep'].destroy()
- }
- }
- out() {
- // 将JavaScript对象转换为JSON字符串
- let tempmap = this.node.children
- let subMapData = {
- map: [],
- mapX: 0,
- mapY: 0,
- }
- for (let index = 0; index < tempmap.length; index++) {
- const element = tempmap[index];
- let Mapdata = {
- bg: '',
- fs: []
- }
- Mapdata.bg = (element.getComponent(cc.Sprite).spriteFrame.name).replace(/[^0-9]/ig, "")
- for (let index1 = 1; index1 < element.children.length; index1++) {
- const prop = element.children[index1];
- Mapdata.fs.push(
- {
- sp: prop.getComponent(cc.Sprite).spriteFrame.name.replace(/[^0-9]/ig, ""),
- // sp: prop.getComponent(cc.Sprite).spriteFrame.name,
- ro:prop.rotation,
- sc:prop.scale,
- x: prop.position.x.toFixed(2),
- y: prop.position.y.toFixed(2)
- }
- )
- }
- subMapData.map.push(Mapdata)
- }
- subMapData.mapX = this.heng
- subMapData.mapY = this.shu
- console.log(subMapData);
- const jsonData = JSON.stringify(subMapData);
- console.log(jsonData);
- this.saveForBrowser(jsonData, 'map1')
-
- }
- /**
- * 存字符串内容到文件。
- * @param textToWrite - 要保存的文件内容
- * @param fileNameToSaveAs - 要保存的文件名
- */
- private saveForBrowser(textToWrite, fileNameToSaveAs) {
- let textFileAsBlob = new Blob([textToWrite], { type: 'application/json' });
- let downloadLink = document.createElement("a");
- downloadLink.download = fileNameToSaveAs;
- downloadLink.innerHTML = "Download File";
- if (window.webkitURL != null) {
- downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
- }
- downloadLink.click();
- }
- }
|