筹码思路:
初始放置4个筹码到场景中,设置一个公有的父节点用来开启点击事件,通过点击生成新的筹码放置到游戏中
start() {
this.btnMove();
},
btnMove() {
this.btnPar.children.forEach((e, index, arr) => {
cc.tween(e)
.to(0.2, { position: cc.v2(-200 + 135 * index, -295) })
.start()
});
},
onButtonClicked(event) {
let target = event.target;
//通过获取节点名字来获取砝码的类型
let chipType = target.name.slice(3);
//通过获取的类型,来得到预制件
let prefab = this.chipPrefabs[chipType];
let chip = cc.instantiate(prefab);
let com = chip.addComponent("Chip");
let ranX = Math.random() * 100 - 50;
com.init(target.position, cc.v2(ranX, 0));
chip.setPosition(target.position);
this.chipPar.addChild(chip);
}
在场景中的筹码都设置点击事件用于筹码回收
onLoad() {
this.node.on("touchstart", this.onTouchStart, this);
},
init(startPos, endPos) {
this.startPos = startPos;
this.endPos = endPos;
this.isClick = false;
},
start() {
this.chipOut();
},
chipOut() {
cc.tween(this.node)
.to(0.5, { position: this.endPos })
.call(() => { this.isClick = true })
.start()
},
onTouchStart() {
if (this.isClick) {
cc.tween(this.node)
.to(0.5, { position: this.startPos })
.call(() => { this.node.destroy() })
.start()
}
}
游戏主体逻辑思路:
发的牌要去重:(res是要发的牌,里面的模板字符串是用来判断牌号是否在res中已存在)
prepare(res) {
//去重
let count = 1;
while (count) {
let ran1 = Math.random() * 4 | 0 + 1;
let ran2 = Math.random() * 13 | 0 + 1;
let pokeName = `${ran1}-${ran2}`
if (this.res.indexOf(pokeName) === -1) {
this.res.push(pokeName);
count--;
}
}
//拿到要分的牌名 并且返回
return this.res[this.res.length - 1];
},
发牌给庄家的思路:
庄家拿牌有个规律那就是必须小于21点才拿
hit:玩家拿牌如果超过21点就爆掉
stand:听牌,玩家直接进入判断结算