Creator实战项目【保卫萝卜】

编程入门 行业动态 更新时间:2024-10-10 19:17:14

Creator实战项目【保卫<a href=https://www.elefans.com/category/jswz/34/1666477.html style=萝卜】"/>

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实战项目【保卫萝卜】

本文发布于:2024-03-08 13:17:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1720988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:萝卜   实战   项目   Creator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!