学习完美方块小游戏
通过学习完美方块小游戏,了解缓动系统(cc.tween)的简单运用
【游戏效果图】
【游戏玩法】:玩家通过长按屏幕,使方块放大到一定的大小后下落,如果被基座接住算是过关,掉落或者被墙体接住算是不过关。
首先,搭建好场景与布置好节点,需要注意一下,基座与墙体的左右两个方块锚点改成如下图:
在game脚本中编写游戏逻辑:
绑定相应的节点、绑定touchstart、touchend事件、初始化、随机设置背景颜色
properties: {
blockNode: cc.Node,
baseArea: [cc.Node],
wallArea: [cc.Node],
scoreLabel: cc.Label
},
onLoad () {
this.node.on('touchstart', this.grow, this);
this.node.on('touchend', this.stop, this);
this.score = 0; // 得分
this.resetBgColor(); // 随机背景颜色
this.init(); // 初始化
},
基座、墙体、方块初始化
// 初始化
init() {
this.gameState = 'idle'; // 状态机
this.resetBaseAndWall(); // 设置基座与墙体
this.resetBlock(); // 设置block
},
// 设置基座与墙体
resetBaseAndWall() {
let baseGap = 100 + Math.random() * 100;
let wallGap = baseGap + 30 + Math.random