123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- // "map": [
- // {
- // "bg": "5",
- // "fs": []
- // },
- // {
- // "bg": "1",
- // "fs": []
- // },
- // {
- // "bg": "1",
- // "fs": []
- // },
- // {
- // "bg": "3",
- // "fs": []
- // }
- // ],
- // "mapX": 2,
- // "mapY": 2,
- // "BronID": 0,
- // "BronX": 55,
- // "BronY": 55
- const { ccclass, property } = cc._decorator;
- type ddddtype = {
- map: maptype[]
- mapX: number
- mapY: number
- BronID: number
- BronX: number
- BronY: number
- }
- type maptype = {
- bg: number
- fs: [{}]
- }
- @ccclass
- export default class edLayout extends cc.Component {
- @property(cc.EditBox)
- daoruEditBox: cc.EditBox = 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() {
- // 监听键盘按下事件
- cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
- // 监听键盘松开事件
- cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
- //绑定事件
- //因为摇杆很小,如果给摇杆绑定事件玩家将很难控制,摇杆的背景比较大,所以把事件都绑定在背景上是不错的选择
- 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事件
- }
- onKeyDown(event) {
- switch (event.keyCode) {
- case cc.macro.KEY.a:
- this.zuo()
- this.zuo()
- break;
- case cc.macro.KEY.d:
- this.you()
- this.you()
- break;
- case cc.macro.KEY.w:
- this.shang()
- this.shang()
- break;
- case cc.macro.KEY.s:
- this.xia()
- this.xia()
- break;
- // 添加更多按键处理逻辑
- }
- }
- onKeyUp(event) {
- switch (event.keyCode) {
- case cc.macro.KEY.a:
- console.log('A 键被松开');
- break;
- case cc.macro.KEY.b:
- console.log('B 键被松开');
- break;
- // 添加更多按键处理逻辑
- }
- }
- onDestroy() {
- // 在组件销毁时,取消键盘事件监听
- cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
- cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
- }
- startpos: cc.Vec2
- starttt(event: cc.Event.EventTouch) {
- this.startpos = event.getLocation()
- }
- move(event: cc.Event.EventTouch) {//负责移动摇杆 手指移动时调用
- console.log(event.getLocationX(), event.getLocationY());
- 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', (event) => {
- 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', (event) => {
- 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()
- }
- }
- daoru() {
- this.heng = (window['dd'] as ddddtype).mapX
- this.shu = (window['dd'] as ddddtype).mapY;
- this.init()
- let mmmp = (window['dd'] as ddddtype).map
- mmmp.forEach(e => {
- e.bg
- e.fs
- })
- for (let i = 0; i < this.node.children.length; i++) {
- const node = this.node.children[i];
- const data = mmmp[i];
- const url = "bg/bg" + data.bg
- cc.resources.load(url, cc.SpriteFrame, (err, spriteframe) => {
- if (err) {
- return
- }
- let sprite = node.getComponent(cc.Sprite);
- sprite.spriteFrame = spriteframe;
- });
- for (let j = 0; j < data.fs.length; j++) {
- const element = data.fs[j];
- const url = "footstep/footstep" + element['sp']
- cc.resources.load(url, cc.Prefab, (err, Prefab) => {
- if (err) {
- return
- }
- let a = cc.instantiate(Prefab);
- a.parent = node
- a.active = true
- a.setPosition(element['x'], element['y'])
- a.setRotation(element['ro'])
- a.setScale(element['sc'])
- });
- }
- }
-
- }
- out() {
- // 将JavaScript对象转换为JSON字符串
- let tempmap = this.node.children
- let subMapData = {
- map: [],
- mapX: 0,
- mapY: 0,
- BronID: 0,
- BronX: 0,
- BronY: 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
- subMapData.BronID = 0
- subMapData.BronX = 55
- subMapData.BronY = 55
- 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();
- }
- }
|