开发消消乐这种游戏的时候,需要实时更新游戏当中的当前关卡值、所用步数、分数。用scheduleUpdate()就可以解决。我们将分数、关卡、步数封装在GameUI一个类中,在GameLayer中new一个GameUI的对象,这是钟比较常规的思路。那么怎样才能保证实时更新这些标签呢?这时候就需要我们在new的时候传递参数,当然在GameUI的ctor中也需要传递一个参数,用来保存它里边的这些标签。代码如下:
GameUI中的核心代码:
var GameUI = cc.Layer.extend({ levelText:0,scoreText:0,stepText:0,gameLayer:null,ctor: function (gameLayer) { this._super(); this.gameLayer=gameLayer; this._initInfoPanel(); this.scheduleUpdate(); },update: function () { this.levelText.setString(""+(this.gameLayer.level+1)); this.scoreText.setString(""+this.gameLayer.score); this.stepText.setString(""+(this.gameLayer.limitStep - this.gameLayer.steps)); }
GameLayer核心代码:
//在新建GameUI的时候把自己传递进来,重点是this。 this.ui = new GameUI(this); this.addChild(this.ui,3);
这样就可以实现实时更新标签。其实这种方式不光可以传递像标签这样的,也可以是Sprite等参数。效果图:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。