[LayaAir3]预制体动画节点播放事件结束,回调事件没有触发
const { regClass, property } = Laya;
@regClass()
export class enemy extends Laya.Script {
@property(String)
public name: string = "小菜";
@property(Number)
public hp: number = 1000;
// 血量
@property(Number)
public attack: number = 10;
// 攻击力
@property(Number)
public defense: number = 5;
// 防御
@property(Number)
public speed: number = 2;
// 移动或者攻击速度
@property(String)
public type: string = "normal"; // 可以是 "normal", "boss", "elite" 等
// 野怪类型
@property({ type: Laya.FontClip })
public damage: Laya.FontClip
@property({ type: Laya.ProgressBar })
public progress: Laya.ProgressBar
@property({type:Laya.Animation})
public dieAnim:Laya.Animation
@property({type:Laya.Animation})
public attackAnim:Laya.Animation
private currentHp:number
// 组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
console.log(`${this.name} has awakened with ${this.hp} HP and ${this.attack} attack.`);
this.currentHp=this.hp
this.dieAnim.on(Laya.Event.COMPLETE, this, ()=>{
console.log('播放结束')
});
this.dieAnim.on(Laya.Event.STOPPED, this, ()=>{
console.log('播放结束')
});
Laya.timer.once(1000,this,this.die)
}
getRandomNumber(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
onTimer() {
let damage=this.getRandomNumber(0,1000)
this.takeDamage(damage)
}
// 组件被启用后执行,例如节点被添加到舞台后
onEnable(): void {
// console.log(`${this.name} is enabled.`);
}
// 组件被禁用时执行,例如节点从舞台移除后
onDisable(): void {
// console.log(`${this.name} is disabled.`);
}
// 第一次执行update之前执行,只会执行一次
onStart(): void {
console.log(`${this.name} is starting.`);
}
// 手动调用节点销毁时执行
onDestroy(): void {
console.log(`${this.name} is destroyed.`);
}
// 每帧更新时执行,尽量不要在这里写大循环逻辑或者使用getComponent方法
onUpdate(): void {
// 例如,可以在这里处理怪物的移动逻辑
// this.move();
}
// 示例方法:怪物移动
move(): void {
// 简单的移动逻辑,假设怪物沿X轴移动
let owner = this.owner as Laya.Sprite;
owner.x += this.speed;
}
// 示例方法:怪物受到攻击
takeDamage(amount: number): void {
this.currentHp -= amount;
this.showDamage(amount)
this.progress.value=Number((this.currentHp/this.hp).toFixed(2))
if (this.currentHp <= 0) {
this.die();
}
}
showDamage(damage:number){
this.damage.value=String(damage)
let initY=this.damage.y
Laya.Tween.to(this.damage, {y: this.damage.y - 20, alpha: 1}, 500, Laya.Ease.circOut, Laya.Handler.create(this, ()=>{
this.damage.alpha=0
this.damage.y=initY
}));
}
// 示例方法:怪物死亡,仅仅是播放动作
die(): void {
console.log(`${this.name} has died.`);
// 播放死亡动作后
// this.attackAnim.visible=false
// this.dieAnim.visible=true
// 去掉血条
this.progress.visible=false
// 对每个动作添加一些事件
this.dieAnim.play(0, false);
// Laya.timer.once(1000,this,this.dieAction)
}
// 真正的死亡逻辑
dieAction(){
console.log('死亡')
// 触发奖励啥的
// 销毁展示
this.owner.destroy(); // 销毁怪物节点
}
}
@regClass()
export class enemy extends Laya.Script {
@property(String)
public name: string = "小菜";
@property(Number)
public hp: number = 1000;
// 血量
@property(Number)
public attack: number = 10;
// 攻击力
@property(Number)
public defense: number = 5;
// 防御
@property(Number)
public speed: number = 2;
// 移动或者攻击速度
@property(String)
public type: string = "normal"; // 可以是 "normal", "boss", "elite" 等
// 野怪类型
@property({ type: Laya.FontClip })
public damage: Laya.FontClip
@property({ type: Laya.ProgressBar })
public progress: Laya.ProgressBar
@property({type:Laya.Animation})
public dieAnim:Laya.Animation
@property({type:Laya.Animation})
public attackAnim:Laya.Animation
private currentHp:number
// 组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
console.log(`${this.name} has awakened with ${this.hp} HP and ${this.attack} attack.`);
this.currentHp=this.hp
this.dieAnim.on(Laya.Event.COMPLETE, this, ()=>{
console.log('播放结束')
});
this.dieAnim.on(Laya.Event.STOPPED, this, ()=>{
console.log('播放结束')
});
Laya.timer.once(1000,this,this.die)
}
getRandomNumber(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
onTimer() {
let damage=this.getRandomNumber(0,1000)
this.takeDamage(damage)
}
// 组件被启用后执行,例如节点被添加到舞台后
onEnable(): void {
// console.log(`${this.name} is enabled.`);
}
// 组件被禁用时执行,例如节点从舞台移除后
onDisable(): void {
// console.log(`${this.name} is disabled.`);
}
// 第一次执行update之前执行,只会执行一次
onStart(): void {
console.log(`${this.name} is starting.`);
}
// 手动调用节点销毁时执行
onDestroy(): void {
console.log(`${this.name} is destroyed.`);
}
// 每帧更新时执行,尽量不要在这里写大循环逻辑或者使用getComponent方法
onUpdate(): void {
// 例如,可以在这里处理怪物的移动逻辑
// this.move();
}
// 示例方法:怪物移动
move(): void {
// 简单的移动逻辑,假设怪物沿X轴移动
let owner = this.owner as Laya.Sprite;
owner.x += this.speed;
}
// 示例方法:怪物受到攻击
takeDamage(amount: number): void {
this.currentHp -= amount;
this.showDamage(amount)
this.progress.value=Number((this.currentHp/this.hp).toFixed(2))
if (this.currentHp <= 0) {
this.die();
}
}
showDamage(damage:number){
this.damage.value=String(damage)
let initY=this.damage.y
Laya.Tween.to(this.damage, {y: this.damage.y - 20, alpha: 1}, 500, Laya.Ease.circOut, Laya.Handler.create(this, ()=>{
this.damage.alpha=0
this.damage.y=initY
}));
}
// 示例方法:怪物死亡,仅仅是播放动作
die(): void {
console.log(`${this.name} has died.`);
// 播放死亡动作后
// this.attackAnim.visible=false
// this.dieAnim.visible=true
// 去掉血条
this.progress.visible=false
// 对每个动作添加一些事件
this.dieAnim.play(0, false);
// Laya.timer.once(1000,this,this.dieAction)
}
// 真正的死亡逻辑
dieAction(){
console.log('死亡')
// 触发奖励啥的
// 销毁展示
this.owner.destroy(); // 销毁怪物节点
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
1 个回复
LayaAir大为
赞同来自: