一、<重新开始>按钮
- 在
game.js
属性中添加restartButton
。并与Canvas节点
绑定。
properties: {
...
restartButton: cc.Button,
},
- 在
onLoad()方法
中,添加对restartButton的click
响应事件,调用restart方法
。
onLoad() {
this.restartButton.node.on('click', this.restart, this);
},
- restart方法直接调用
init()方法
即可。打印相关log。
restart(event) {
console.log('restart!');
this.init();
},
- 预览。可以看到点击<重新开始>按钮,游戏重新开始,分数清零。

二、<再玩一次>按钮
- 与<重新开始>一致。添加
tryAgainButton
。并与Canvas节点
绑定。在onLoad()方法
中,添加对tryAgainButton的click响应
事件,调用restart方法
。
properties: {
...
tryAgainButton: cc.Button,
},
onLoad() {
...
this.tryAgainButton.node.on('click', this.restart, this);
},
- 为了能快速预览,将
ROWS改为2
。预览。可以看到在失败后点击<再玩一次>按钮,游戏重新开始,分数清零。
