复制一个自己喜欢的游戏

在以前玩了一款比较喜欢的游戏,但是由于运营商不给力,给种bug最后关服了,感觉十分遗憾.
本来以为会一直遗憾下去,没想到发现了creator,比较简单的游戏制作工具,虽然自己对编程一无所知但还是决定自己制作看看有木有可能,制作一款单机来玩玩看
成功找到该游戏的apk,反编译,还好是以前的老游戏,没有各种加密,成功获取各种资源.
拿到资源开搞
1,拼Ui毫无难度

1赞

妖精的尾巴,我燃烧起来了

1赞

作为一个自己喜欢的卡牌游戏,当然抽卡是最大的乐趣所在,决定先解决商城
1先拼ui


2.然后制作了一个关于卡牌的预制物体
3.开始写代码,总感觉代码存在各种问题,但目前也没有发现有什么不妥之处,于是决定讲究一下吧,反正随便玩玩,看以后有能力在优化一下

import splash from “…/splashScript”;
import Bean from “…/Bean”;
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
@property(cc.Sprite)
bigFaceRec: cc.Sprite;
@property(cc.Sprite)
bigface: cc.Sprite;
@property(cc.Sprite)
zhiyeBg: cc.Sprite;
@property(cc.Sprite)
zhieyeICon: cc.Sprite;
@property(cc.Layout)
oneCardLayout: cc.Layout;
@property(cc.Label)
name1: cc.Label;
@property(cc.AudioClip)
draw: cc.AudioClip;
@property(cc.Button)
supian: cc.Button;
//卡牌单抽
drawACard() {
this.oneCardLayout.node.active = true;
cc.audioEngine.play(this.draw, false, 1);
//this.shop.node.active =false;
var index = Math.floor(cc.rand() % splash.cards.length) + 1;//随机生成1到10的数
var cardinfo = splash.getCard(index);
if (cardinfo.starts == 6) {//
this.drawACard();
} else {
cc.loader.loadRes(“change”, cc.SpriteAtlas, (err, attls) => {
cc.loader.loadRes(“bigFace/” + cardinfo.bigface, cc.SpriteFrame, (err, spriterFrame) => {
this.bigFaceRec.spriteFrame = attls.getSpriteFrame(cardinfo.bigFaceRec);
this.bigface.spriteFrame = spriterFrame;
this.zhiyeBg.spriteFrame = attls.getSpriteFrame(cardinfo.zhiyeBg);
this.zhieyeICon.spriteFrame = attls.getSpriteFrame(cardinfo.zhieyeICon);
this.name1.string = cardinfo.name;
var uri = cc.url.raw(‘resources/music/’ + cardinfo.music + “.mp3”);
cc.audioEngine.play(uri, false, 1);
var tag = false;
var starts = false;
var cunzai
for (var i = 0; i < splash.beibaos.length; i++) {
cunzai = splash.beibaos[i];
if (cunzai.isfight == 1) {
continue;
}
if (cunzai.name == cardinfo.name) {
starts = true;
if (cunzai.starts > cardinfo.starts) {
//直接转换成碎片
tag = true;
} else {
//删除原来的数据
splash.beibaos.splice(i, 1);
}
break;
}
}
//根据标志存储数据
if (tag) {
this.saveSuiPian(cardinfo);
} else {

                    let beibao = new Bean();
                    if (starts) {
                        this.saveSuiPian(cunzai);
                        beibao.saveBeibao(beibao.beibao, cardinfo,splash);

                    } else {
                       beibao.saveBeibao(beibao.beibao, cardinfo,splash);
                    }
                    
                }
            });
        });
    }


}

//碎片
saveSuiPian(cardinfo) {
    //存在 转化成碎片
    var suipian = {
        id: "",
        name: "",
        face: "",
        suipianNumber: ""
    }
    var isName = false;
    var suipianIndex;
    this.supian.node.active = true;
    for (var j = 0; j < splash.suipianArr.length; j++) {
        if (splash.suipianArr[j].name == cardinfo.name) {
            isName = true;
            suipianIndex = j;
            break;
        }
    }
    if (isName) {
        //存在直接谁改碎片数量
        splash.suipianArr[suipianIndex].suipianNumber = parseInt(splash.suipianArr[suipianIndex].suipianNumber) + cardinfo.starts * 20 + "";
    } else {
        //不存在存入数据
        suipian.id = cardinfo.id + "";
        suipian.name = cardinfo.name;
        suipian.face = cardinfo.face;
        suipian.suipianNumber = cardinfo.starts * 20 + "";
        splash.suipianArr.push(suipian);
    }
    cc.sys.localStorage.setItem("suipianBeibao", JSON.stringify(splash.suipianArr));
}
//返回shop界面
toShop() {
    this.oneCardLayout.node.active = false;
    this.supian.node.active = false;
    cc.audioEngine.stopAll();
  
}

}
3.最后感觉效果还行,然后就是感觉十连抽太麻烦了,于是决定不在写十连抽的代码

mark mark

现在的玩家真任性,看那些研发还敢惹玩家不爽。

一言不合就反编译,666

既然抽卡完成后,当然便是存储卡牌,背包系统借鉴于官方教程中的ui教程


代码:
const { ccclass, property } = cc._decorator;
import splash from “…/splashScript”;
import bean from “…/Bean”;
@ccclass
export default class NewClass extends cc.Component {

public addSuiPianMessage(pre: cc.Prefab, sco: cc.ScrollView, xinging: cc.SpriteFrame, renwu: cc.Layout) {
    var suipian = splash.suipianArr;
    var cardMassage = {
        id: Number,
        name: "",
        faceKuang: cc.SpriteFrame,
        facePic: cc.SpriteFrame,
        zhiyeBg: cc.SpriteFrame,
        zhiyeIcon: cc.SpriteFrame,
        suipianNumber: ""
    }
    var content = sco.content;
    content.removeAllChildren();
    var space = 5;
    var cardArr: any[] = [];
    content.height = 0;
    content.height = suipian.length * (91 + 5) + 25;
    cc.loader.loadRes("change", cc.SpriteAtlas, (err, atlas1) => {
        if (err != null) {
            cc.log(err.message);
            return;
        }
        for (var i = 0; i < suipian.length; i++) {
            var suipianInfo = suipian[i];
            cardArr.push(i);
            if (suipianInfo == null) {
                break;
            }
            cc.loader.loadRes("face/" + suipianInfo.face, cc.SpriteFrame, (err, spriteframe) => {
                for (var j = 0; j < cardArr.length; j++) {
                    var suipianInfo = suipian[j];
                    var cardInfo = splash.getCard(suipianInfo.id);
                    if (suipianInfo.face == spriteframe.name) {
                        cardMassage.zhiyeBg = atlas1.getSpriteFrame(cardInfo.zhiyeBg);
                        cardMassage.zhiyeIcon = atlas1.getSpriteFrame(cardInfo.zhieyeICon);
                        cardMassage.id = cardInfo.id;
                        cardMassage.name = cardInfo.name;
                        cardMassage.facePic = spriteframe;
                        cardMassage.suipianNumber = suipianInfo.suipianNumber;
                        var xinxing = cc.instantiate(pre);
                        xinxing.getComponent("xingxiBean").rewnwu = renwu;
                        xinxing.getComponent("xingxiBean").initSuipianMessage(cardMassage, xinging);
                        if (j == 0) {
                            xinxing.setPosition(0, -55);
                        } else {
                            xinxing.setPosition(0, j * (-91 - space) - 55);
                        }
                        content.addChild(xinxing);

                        //cardArr.splice(j,1);
                        break;
                    }
                }



            });

        }

    });

}

public addMessage(pre: cc.Prefab, sco: cc.ScrollView, xinging: cc.SpriteFrame, renwu: cc.Layout) {

    var cardMassage = new bean().beibao;
    var content = sco.content;
    content.removeAllChildren();
    var space = 5;
    var beibaos = splash.beibaos;
    content.height = 0;
    content.height = beibaos.length * (91 + 5) + 25;
    cc.loader.loadRes("change", cc.SpriteAtlas, (err, atlas1) => {
        var cardArr: any[] = [];
        var cardIsFight: any[] = [];
        var spriteframeArr: any[] = [];
        var cardFiveArr: any[] = [];
        var beibaoInfo;
        var cardInfo;
        if (err != null) {
            cc.log(err.message);
            return;
        }
        for (let i = 0; i < beibaos.length; i++) {
            beibaoInfo = beibaos[i];
            cardInfo = splash.getCard(beibaos[i].id);
            if (parseInt(beibaoInfo.isfight) == 1) {
                cardIsFight.push(beibaoInfo.id);
            } else {
                if (parseInt(beibaoInfo.starts) >= 5) {
                    cardFiveArr.push(beibaoInfo.id);
                } else {
                    cardArr.push(beibaoInfo.id);
                }

            }

            if (cardInfo == null) {
                break;
            }
            cc.loader.loadRes("face/" + cardInfo.face, cc.SpriteFrame, (err, spriteframe) => {
                spriteframeArr.push(spriteframe);

            });

        }
        setTimeout(() => {
            var count = cardIsFight.length;
            //加载已经上阵的卡牌
            for (let i = 0; i < cardIsFight.length; i++) {
                beibaoInfo = splash.getBebaoInfo(cardIsFight[i]);
                cardInfo = splash.getCard(beibaoInfo.id);
                for (let j = 0; j < spriteframeArr.length; j++) {
                    let spriteframe = spriteframeArr[j];
                    if (cardInfo.face == spriteframe.name) {
                        this.addBeibao(cardMassage, atlas1, cardInfo, pre, i, beibaoInfo, spriteframe, renwu, xinging, space, content);
                        spriteframeArr.splice(j, 1)
                        break;
                    }
                }

            }
            //加载六星或者五星的卡牌
            for (let index = 0; index < cardFiveArr.length; index++) {
                let carID = cardFiveArr[index];
                for (let i = 0; i < beibaos.length; i++) {
                    beibaoInfo = beibaos[i];
                    if (beibaoInfo.isfight == 1) {
                        continue;
                    }
                    if (beibaoInfo.id == carID) {
                        break;
                    }
                }

                cardInfo = splash.getCard(beibaoInfo.id);
                for (let j = 0; j < spriteframeArr.length; j++) {
                    let spriteframe = spriteframeArr[j];
                    if (cardInfo.face == spriteframe.name) {
                        this.addBeibao(cardMassage, atlas1, cardInfo, pre, count++, beibaoInfo, spriteframe, renwu, xinging, space, content);
                        spriteframeArr.splice(j, 1)
                        break;
                    }
                }
            }
            //加载其他卡牌
            for (let index = 0; index < cardArr.length; index++) {
                let carID = cardArr[index];
                for (let i = 0; i < beibaos.length; i++) {
                    beibaoInfo = beibaos[i];
                    if (beibaoInfo.isfight == 1) {
                        continue;
                    }
                    if (beibaoInfo.id == carID) {
                        break;
                    }
                }

                cardInfo = splash.getCard(beibaoInfo.id);
                for (let j = 0; j < spriteframeArr.length; j++) {
                    let spriteframe = spriteframeArr[j];
                    if (cardInfo.face == spriteframe.name) {
                        this.addBeibao(cardMassage, atlas1, cardInfo, pre, count++, beibaoInfo, spriteframe, renwu, xinging, space, content);
                        spriteframeArr.splice(j, 1)
                        break;
                    }
                }
            }

        }, 100 + beibaos.length);


    });

}

addBeibao(cardMassage, atlas1, cardInfo, pre, index, beibaoInfo, spriteframe, renwu, xinging, space, content) {
    cardMassage.zhiyeBg = atlas1.getSpriteFrame(cardInfo.zhiyeBg);
    cardMassage.zhiyeIcon = atlas1.getSpriteFrame(cardInfo.zhieyeICon);
    cardMassage.id = cardInfo.id;
    cardMassage.name = cardInfo.name;
    cardMassage.starts = cardInfo.starts;
    cardMassage.dengji = beibaoInfo.dengji;
    cardMassage.isfight = beibaoInfo.isfight;
    cardMassage.zhandouli = cardInfo.zhandouli;
    cardMassage.facePic = spriteframe;
    cardMassage.faceKuang = atlas1.getSpriteFrame(cardInfo.faceRec);
    var xinxing = cc.instantiate(pre);
    xinxing.getComponent("xingxiBean").rewnwu = renwu;
    xinxing.getComponent("xingxiBean").initCardMessage(cardMassage, xinging);
    if (index == 0) {
        xinxing.setPosition(0, -55);
    } else {
        xinxing.setPosition(0, index * (-91 - space) - 55);
    }
    content.addChild(xinxing);
}

}

人物信息与上阵系统,怎么保存数据和怎么判断阵位是否已经存在,想了半天,由于能力有限,只好使用了一个笨办法,循环判断

代码
const { ccclass, property } = cc._decorator;
import splash from “…/splashScript”;
import ficKuang from “…/ficKuangScript”;
@ccclass
export default class NewClass extends cc.Component {

@property(cc.Button)
retuB: cc.Button;
@property(cc.Sprite)
bigFaceRec: cc.Sprite;
@property(cc.Sprite)
bigface: cc.Sprite;
@property(cc.Label)
name1: cc.Label;
@property([cc.Sprite])
xingxing: cc.Sprite[];//星级
@property(cc.SpriteFrame)
liangxing: cc.SpriteFrame;//星级
@property(cc.ScrollView)
scro: cc.ScrollView;
@property(cc.Button)
shangzhen: cc.Button;
@property(cc.Prefab)
face: cc.Prefab;
@property(cc.Layout)
beibaoBtn: cc.Layout;
@property(cc.Layout)
toast: cc.ScrollView;
@property(cc.ScrollView)
ficeBtn: cc.ScrollView;

cardId: "";
onLoad() {
    //返回
    this.toHome();
    //上阵
    this.fight();


}
fight() {
    this.shangzhen.node.on("touchend", () => {
        var buzhen = splash.buzhen;
        if (buzhen != null) {
            var arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8];
            for (var buzhenID in buzhen) {
                for (let index = 0; index < arr1.length; index++) {
                    let wz = arr1[index];
                    if (parseInt(buzhenID) == wz && buzhen[buzhenID]!=undefined) {
                        arr1.splice(index, 1);
                        break;
                    }
                }
            }
            if (arr1.length == 0) {
                this.beibaoBtn.getComponent("backpackUI").loadData();
                let time = this.toast.getComponent("toast").text("阵容已满", 2);
                setTimeout(() => {
                    this.getComponent(cc.Animation).play("ReceverBeiBaoUIa");
                }, time * 1000);
                return;
            }

            let sorArr = arr1.sort();
            splash.buzhen[sorArr[0]] = this.cardId;
            var data = JSON.stringify(splash.buzhen);
            cc.sys.localStorage.setItem("zhenwei", data);
            for (var index = 0; index < splash.beibaos.length; index++) {
                var element = splash.beibaos[index];
                if (element.id == this.cardId) {
                    element.isfight = 1 + "";
                    this.beibaoBtn.getComponent("backpackUI").loadData();
                    cc.sys.localStorage.setItem("cardBeibao", JSON.stringify(splash.beibaos));
                    break;
                }
            }

        } else {
            var arr: { [key: string]: string; } = {};
            arr[1] = this.cardId;
            splash.buzhen = arr;
            var data = JSON.stringify(arr);
            cc.sys.localStorage.setItem("zhenwei", data);
            for (var index = 0; index < splash.beibaos.length; index++) {
                var element = splash.beibaos[index];
                if (element.id == this.cardId) {
                    element.isfight = 1 + "";
                    this.beibaoBtn.getComponent("backpackUI").loadData();
                    cc.sys.localStorage.setItem("cardBeibao", JSON.stringify(splash.beibaos));
                    break;
                }
            }
        }
        let time = this.toast.getComponent("toast").text("上阵成功", 2);
        console.log(time);
        setTimeout(() => {
            this.getComponent(cc.Animation).play("ReceverBeiBaoUIa");
            let fa = this.ficeBtn.getComponent("ficKuangScript");
            fa.isInitBuzhen = true;
        }, time * 1000);

    });

}
initFace(id, leixing) {
    this.cardId = id;
    var beibaoInfo;
    if (leixing != "suipian") {
        beibaoInfo = splash.getBebaoInfo(id);
        if (beibaoInfo == undefined) {
            return;
        }
        var bigFace;
        if (beibaoInfo.isfight == 1) {
            this.shangzhen.node.active = false;
        } else {
            this.shangzhen.node.active = true;
        }
        if (beibaoInfo.pifuId == 0) {
            bigFace = beibaoInfo.bigface;
        } else {
            //皮肤脸
        }
    } else {
        beibaoInfo = splash.getCard(id);
        if (beibaoInfo == undefined) {
            return;
        }
        bigFace = beibaoInfo.bigface;
        this.shangzhen.node.active = false;
    }
    cc.loader.loadRes("change", cc.SpriteAtlas, (err, atlas) => {
        cc.loader.loadRes("bigFace/" + bigFace, cc.SpriteFrame, (err, spriterFrame) => {
            this.bigface.spriteFrame = spriterFrame;
            this.bigFaceRec.spriteFrame = atlas.getSpriteFrame(beibaoInfo.bigFaceRec);
            this.name1.string = beibaoInfo.name;
            for (var i = 0; i < parseInt(beibaoInfo.starts); i++) {
                this.xingxing[i].spriteFrame = this.liangxing;
            }

        });
    });


}
toHome() {
    this.retuB.node.on("touchend", () => {
        this.getComponent(cc.Animation).play("ReceverBeiBaoUIa");
        //this.node.active = false;
    });

}

}

布阵系统,感觉比较麻烦,使用了碰撞系统,不过不知道为什么第一次碰撞,的时候 onCollisionEnter(other, self) 方法会执行两次,然后让数据错乱,于是加了各种判读,最后才解决.
然后在阵容不存在卡牌的时候,使用了透明精灵来填充位置


//加载已经上阵了卡牌的代码
const { ccclass, property } = cc._decorator;
import splash from “…/splashScript”;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Prefab)
renwu: cc.Prefab;
@property([cc.Sprite])
zhenwei: cc.Sprite[];
@property(cc.Sprite)
layou: cc.Sprite;
@property(cc.Label)
totalZhandouli: cc.Label;
@property(cc.Label)
renNum: cc.Label;
@property([cc.Sprite])
ditu: cc.Sprite[];
tag = 0;
dituWZ: any[] = [];
onLoad() {
this.addRenwu();
}
removeChaid(id) {
this.layou.node.removeChildByTag(parseInt(id));
}
addRenwu() {
var data = {
id: “”,//阵位id
cardID: “”,//卡牌id
name: “”,//名字
dengji: “”,
totalZhandouli: “”,
renNum: “”,
zhiyeBg: “”,
zhieyeICon: “”,
bigface: “”,
bigFaceRec: “”

    }
    var buzhenInfo = splash.buzhen;
    var index = 0;
    var count: any[] = [];
    for (var index = 0; index < this.ditu.length; index++) {
        var element = this.ditu[index];
        element.node.active = true;
        if (this.tag == 0) {
            this.dituWZ.push(element.node.position);
        } else {
            element.node.getComponent("dituId").setId(index);
            element.node.position = this.dituWZ[index];
        }

    }

    cc.loader.loadRes("change", cc.SpriteAtlas, (err, atlas) => {
        for (let zhenweiID in buzhenInfo) {
            let chaild = this.layou.node.getChildByTag(parseInt(zhenweiID) + 10086);
            if (chaild) {
                this.layou.node.removeChildByTag(parseInt(zhenweiID)+10086);
            }
            if (buzhenInfo[zhenweiID] == undefined || buzhenInfo[zhenweiID] == null) {
                continue;
            }
            let cardId = buzhenInfo[zhenweiID];

            //获取背包数据
            count.push(zhenweiID);
            var beibaoInfo1 = splash.getBebaoInfo(cardId);
            if (beibaoInfo1 != null) {
                cc.loader.loadRes("bigFace/" + beibaoInfo1.bigface, cc.SpriteFrame, (err, spriteFrame) => {
                    for (var i = 0; i < count.length; i++) {
                        let zhenwID = count[i];
                        var cardId = buzhenInfo[zhenwID];
                        var beibaoInfo = splash.getBebaoInfo(cardId);
                        var cardInfo = splash.getCard(beibaoInfo.id);
                        if (cardInfo.bigface == spriteFrame.name) {
                            let chaild = this.layou.node.getChildByTag(parseInt(zhenwID)+10086);
                            if (chaild) {
                                this.layou.node.removeChildByTag(parseInt(zhenwID)+10086);
                            }
                            var ren = cc.instantiate(this.renwu);
                            var post = this.zhenwei[zhenwID].node.position;
                            data.dengji = beibaoInfo.dengji;
                            var total = total + parseInt(beibaoInfo.zhandouli);
                            data.zhiyeBg = atlas.getSpriteFrame(cardInfo.zhiyeBg);
                            data.zhieyeICon = atlas.getSpriteFrame(cardInfo.zhieyeICon);
                            data.id = cardInfo.id;
                            data.name = cardInfo.name;
                            data.bigface = spriteFrame;
                            data.bigFaceRec = atlas.getSpriteFrame(cardInfo.bigFaceRec);
                            var x = post.x + 9;
                            var y = post.y + 16.4;
                            data.id = zhenwID;
                            data.cardID = cardId;
                            this.layou.node.addChild(ren, 50);
                            ren.tag = parseInt(zhenwID)+10086;
                            ren.setPosition(cc.p(x, y));
                            ren.getComponent(cc.BoxCollider).tag = cardId;
                            ren.getComponent("qualityBean").init(data);
                            this.totalZhandouli.string = total + "";
                            this.renNum.string = index + 1 + "";
                            //隐藏已经有上阵人物的底图
                            this.ditu[zhenwID].node.active = false;
                            index++;
                            this.tag = 1;
                            count.splice(i, 1);
                            break;
                        }
                    }


                });


            }
        }
    });
}

}

//位置交换代码
const { ccclass, property } = cc._decorator;
import splash from “…/Script/splashScript”;
@ccclass
export default class QualityBean extends cc.Component {

@property(cc.Sprite)
bigFaceRec: cc.Sprite;
@property(cc.Sprite)
bigface: cc.Sprite;
@property(cc.Label)
dengji: cc.Label;
@property(cc.Label)
name1: cc.Label;
@property(cc.Sprite)
zhiyeBg: cc.Sprite;
@property(cc.Sprite)
zhieyeICon: cc.Sprite;
id: “”;//记录位置
isTouch: Boolean;
zuob: any[] = [];
currentPostX;
currentPostY;
lastMove;
movex;
cardID: “”;
touchingNumber = 0;
static arr: { [key: string]: string; } = {};
static touchiid;

onLoad() {
//获取布阵信息在保存
QualityBean.arr = splash.buzhen;
// cc.director.getCollisionManager().enabledDebugDraw = true;
}
lastPost = cc.p(0, 0);

init(data) {
this.id = data.id;
this.cardID = data.cardID;
this.name1.string = data.name;
this.bigface.spriteFrame = data.bigface;
this.bigFaceRec.spriteFrame = data.bigFaceRec;
this.zhieyeICon.spriteFrame = data.zhieyeICon;
this.zhiyeBg.spriteFrame = data.zhiyeBg;
this.dengji.string = data.dengji;
this.startEnvent();
}

update() {//判断移动是否结束

if (this.isTouch) {
  var curent = this.node.position;
  if ((Math.abs(curent.x - this.lastPost.x) < 0.01) && (Math.abs(curent.y - this.lastPost.y) < 0.01)) {
    cc.director.getCollisionManager().enabled = true;
  }
  this.lastPost = curent;
}

}
startEnvent() {//注册事件
this.node.on(“touchstart”, (event) => {
// cc.director.getCollisionManager().enabled = true;
var gesteStart = event.touch;
this.node.setLocalZOrder(100);
this.isTouch = true;
this.currentPostX = this.node.position.x;
this.currentPostY = this.node.position.y;
this.touchingNumber = 0;

  QualityBean.touchiid = this.cardID;
  // cc.director.getCollisionManager().enabled = true;

});

this.node.on("touchmove", (event) => {

  cc.director.getCollisionManager().enabled = false;
  var gesteStart = event.touch;
  var newVec2 = this.node.parent.convertTouchToNodeSpaceAR(gesteStart);
  var x = newVec2.x + this.node.width / 2;
  var y = newVec2.y + this.node.height / 2;
  if (x < -334) {//限制左边界
    x = -334;
  }
  if (y > -35) {//限制上边界
    y = -35;
  }
  if (x > -12) {//限制右边界
    x = -12;
  }
  if (y < -422) {//限制下边界
    y = -422;
  }

  this.node.setPosition(cc.p(x, y));


});

this.node.on("touchend", (event) => {
  this.isTouch = false;
  setTimeout(() => {
    this.node.setLocalZOrder(0);
    if (this.touchingNumber == 0) {
      this.node.setPosition(this.currentPostX, this.currentPostY);
    } else {

      if (this.otherPostX != null && this.oterPostY != null) {
        this.node.setPosition(this.otherPostX, this.oterPostY);
      }
      else {
        this.node.setPosition(this.currentPostX, this.currentPostY);
      }
    }

  });

}, 200);
//触摸点移除道外部的情况
this.node.on('touchcancel', (event) => {
  this.isTouch = false;
  setTimeout(() => {
    this.node.setLocalZOrder(0);
    if (this.touchingNumber == 0) {
      this.node.setPosition(this.currentPostX, this.currentPostY);
    } else {

      if (this.otherPostX != null && this.oterPostY != null) {
        this.node.setPosition(this.otherPostX, this.oterPostY);
      }
      else {
        this.node.setPosition(this.currentPostX, this.currentPostY);
      }
    }

  });

}, 200);

}
otherPostX;
oterPostY;
/**

  • 当碰撞产生的时候调用

  • @param {Collider} other 产生碰撞的另一个碰撞组件

  • @param {Collider} self 产生碰撞的自身的碰撞组件
    */
    onCollisionEnter(other, self) {
    var world = self.world;
    if (this.touchingNumber == 0) {//执行一次
    cc.director.getCollisionManager().enabled = false;
    this.touchingNumber++;
    if (self.tag == QualityBean.touchiid) {//确地是自己的说点击的目标
    //交换位置
    var selfNode = self.node.getComponent(“qualityBean”);
    var othrePsot = other.node.position;
    this.otherPostX = othrePsot.x;
    this.oterPostY = othrePsot.y;
    var move = cc.moveBy(0.00001, cc.p(this.currentPostX - this.otherPostX, this.currentPostY - this.oterPostY));
    other.node.runAction(move);
    //记录位置
    if (other.tag == 500) {
    var OtherNode = other.node.getComponent(“dituId”);
    var oterid = OtherNode.getID();
    QualityBean.arr[selfNode.id] = undefined;
    QualityBean.arr[oterid] = QualityBean.touchiid;
    OtherNode.setId(selfNode.id);
    selfNode.id = oterid;
    } else {
    var OtherNode = other.node.getComponent(“qualityBean”);
    var oterId = OtherNode.id;
    QualityBean.arr[oterId] = QualityBean.touchiid;
    QualityBean.arr[selfNode.id] = OtherNode.cardID;
    OtherNode.id = selfNode.id;
    selfNode.id = oterId;
    }
    splash.buzhen = QualityBean.arr;
    var w = JSON.stringify(splash.buzhen);
    cc.sys.localStorage.setItem(“zhenwei”, w);
    }

    }

}
/**

  • 当碰撞产生后,碰撞结束前的情况下,每次计算碰撞结果后调用

  • @param {Collider} other 产生碰撞的另一个碰撞组件

  • @param {Collider} self 产生碰撞的自身的碰撞组件
    **/

    onCollisionStay(other, self) {

    }

    /*

    • 当碰撞结束后调用
    • @param {Collider} other 产生碰撞的另一个碰撞组件
    • @param {Collider} self 产生碰撞的自身的碰撞组件
      */
      onCollisionExit(other, self) {

    }

}

在代码段前后加上

```

可以格式化代码显示哦,很方便