游戏主要逻辑:
1,地板滚动
两块地板循环滚动,没啥好说的。
核心代码:
this.groundNode[0].x += Constant.GROUND_VX
this.groundNode[1].x += Constant.GROUND_VX
if (this.groundNode[0].x + this._width/2 < - this._size.width/2) {
this.groundNode[0].x = this.groundNode[1].x + this._width - 5
}
if (this.groundNode[1].x + this._width/2 < - this._size.width/2) {
this.groundNode[1].x = this.groundNode[0].x + this._width - 5
}
2,小鸟坠落与弹跳
坠落的效果需要让小鸟在Y轴负方向有一个加速度,即单位时间类的偏移量还有一个增量。弹跳只需要让原本为负的偏移值变为正。
在onLoad方法中:
this.velocity = 0; //初始下降速度为零
在onDrop方法中:
this.node.y += this.velocity ;
this.velocity -= this.gravity;
3,管道生成逻辑
管道的定位需要五个参数
pipeMaxOffsetY //Y轴最大偏移量
pipeMinGap //管道最小间隙
pipeMaxGap //管道最大间隙
pipeSpawnInterval //管道生成时间间隔
pipeSpawnOffsetX //管道屏幕外生成偏移量
核心代码:
spawnPipes: function() {
var pipeUp = cc.instantiate(this.pipePrefabs[Constant.PIPE_UP]);
pipeUp.getComponent('Pipe').init(Constant.PIPE_UP);
var pipeHeight = pipeUp.getComponent(