萝卜】"/>
Creator实战项目【保卫萝卜】
在怪物脚本中定义怪物血量、公布血条,写一个伤害接口(传入伤害)
//怪物血量
private totalHp:number = 1000;
private hp:number = 1000;Damage(damage:number){this.hp -= damage;//血量为0if(this.hp<=0){//销毁怪物,移除怪物脚本this.remove();}
}
子弹碰撞时处理扣血;当敌人打死怪物以后,要销毁子弹,并且子弹不再移动
//子弹移动
onUpdate(dt){//如果目标脚本依附的节点是空(敌人是空)if(!this.attackTarget.node){//销毁当前子弹this.node.destroy();return;
}//计算向量let deltaPos = this.attackTarget.node.position.sub(this.node.position);//归一化let dir = deltaPos.normalizeSelf();//缩放方向,以速度进行缩放let distancePos = dir.mul(this.speed);//移动let result = this.node.position.add(distancePos);//设置给当前节点this.node.setPosition(result);
}//子弹碰撞
onCollisionEnter(other:cc.Collider,self:cc.Collider){//子弹停止跟随this.speed = 0;//播放爆炸动画let anim = this.node.getComponent(cc.Animation);anim.play('BulletAni');//扣血if(this.attackTarget.node){this.attackTarget.Damage(300);}
}
在子弹层创建子弹时,如果怪物已经被移除,不再创建。
addBullet(pos:cc.Vec3,monsterTs:Monster){//发射子弹的那一刻,如果目标已经被移除,不再创建子弹if(!monsterTs.node){return;}let bulletPrefab = ResMgr.getInstance().getData('PrefabAsset').getData('Bullet');let bulletN = cc.instantiate(bulletPrefab);bulletN.position = pos;bulletN.parent = this.node;//获取子弹脚本let bulletTs = bulletN.getComponent('Bullet');bulletTs.setTarget(monsterTs);//为了子弹朝着怪物移动
}
更多推荐
Creator实战项目【保卫萝卜】
发布评论