承接上文
block移动以及小鸟添加
创建block脚本并挂载到block预制体上
runBlock() {
let speed: number = 2;
this.node.x -= speed;
}
update(dt) {
this.runBlock();
}
创建一个新的node放上替换小鸟图片,并创建一个hero脚本挂载到上面
//初始化小鸟
initHero() {
this.node.angle = 0;
this.node.position = cc.v2(-120, 100);
}
initPlayTouch() {
this.time = 0;
this.fail = false;
}
endPlayTouch() {
this.fail = true;
}
setRotation(type) {
this.node.stopActionByTag(1);
if (type == 'up') {
this.node.angle = 20;
} else if (type == 'down') {
let act_1 = cc.rotateTo(0.5, -90);
act_1.setTag(1);//设置tag标记动作
this.node.runAction(act_1);
}
}
管道、地面和小鸟的碰撞
首先对block以及小鸟添加碰撞组件并分组,其次编写相应方法
onCollisionEnter(other: cc.BoxCollider, self: cc.BoxCollider) {
if (GameType.getInstance().getType() == 3) return
if (other.node.group == 'block') {
GameType.getInstance().setType(3);
cc.audioEngine.play(this.hit, false, 1);
cc.audioEngine.play(this.swoosh, false, 1);
cc.audioEngine.play(this.die, false, 1);
let act_1 = cc.delayTime(0.2);
let act_2 = cc.moveTo(1, cc.v2(this.node.x, -cc.winSize.height / 2 + 160));
let end = cc.sequence(act_1, act_2);
self.node.runAction(end);
this.scheduleOnce(() => {
self.node.parent.getComponent('Game').toGameOver();
}, 1);
}
if (other.node.group == 'land') {
GameType.getInstance().setType(3);
cc.audioEngine.play(this.swoosh, false, 1);
cc.audioEngine.play(this.die, false, 1);
self.node.parent.getComponent('Game').toGameOver();
}
}