学习同色消消小游戏
- 随机生成小球
- 点击小球切换成下一个小球
- 初始化地图
- 检查小球是否全部相同
- 升级
- 结束逻辑
效果图
【截图】
首先,搭建好场景布局。这里用到的小球name是有顺序的,一会做点击小球切换的时候,是根据顺序切换的。
【截图】
小球做成预制体,绑定ball脚本。添加一个button组件指定点击事件nextBall()
ball脚本完整代码如下:
cc.Class({
extends: cc.Component,
properties: {
ballAtlas: cc.SpriteAtlas
},
onLoad () {
},
// 随机小球
randBall (typeCnt) {
let frames = this.ballAtlas.getSpriteFrames();
if (typeCnt > frames.length) {
typeCnt = frames.length;
}
let randIndex = parseInt(Math.random() * typeCnt);
let sprite = this.node.getComponent(cc.Sprite);
sprite.spriteFrame = frames[randIndex];
this.typeCnt = typeCnt;
}