CocosCreator项目实战(10):功能-重新开始


一、<重新开始>按钮

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

在这里插入图片描述


二、<再玩一次>按钮

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

在这里插入图片描述


发布了98 篇原创文章 · 获赞 10 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/Fan0628/article/details/104711750