-
Creator 版本: 3.8.3
-
重现方式:浏览器或者编辑器里运行
-
首个报错:没有报错
-
之前哪个版本是正常的:2.4.10
-
重现概率: 100%
BUG描述:使用动态创建图集并播放动画 动画在首次播放时成功 但是再关闭动画后在生成新的节点添加动画后 会播放不出来 测试过 两次动态创建帧动画的参数一致 也添加了spirte组件 新生成的节点会有
this.activeInHierarchy为false的错误 但是播放动画前修改也没有效果 animation播放动画的事件在第一次播放时可以触发play和lastframe事件 但是第二次只能触发play事件
private loadClip(loadFrames: SpriteAtlas, clipName: string, clipDirect: number, clipAction: string, warpMode: number = AnimationClip.WrapMode.Loop) {
if (this.resourceType == "pet") {
console.log("Attempting to play animation:",loadFrames);
console.log("Attempting to play animation:",clipName);
console.log("Attempting to play animation:",clipDirect);
console.log("Attempting to play animation:",clipAction);
console.log("Attempting to play animation:",warpMode);
}
let clip: AnimationClip = null;
if (loadFrames && loadFrames.getSpriteFrames) {
// let spriteAtlas: SpriteAtlas = new SpriteAtlas();
// clip = AnimationClip.createWithSpriteFrames(spriteAtlas.getSpriteFrames(), this.frameRateNum);
clip = AnimationClip.createWithSpriteFrames(loadFrames.getSpriteFrames(), this.frameRateNum);
for (let i = 0; i < loadFrames.getSpriteFrames().length; i++)
clip.wrapMode = AnimationClip.WrapMode.Loop;
clip.name = clipName;
// clip.duration = 0.8;
if (this.resourceType != ACTION_RES_TYPE.HORSE && this.resourceType != ACTION_RES_TYPE.HORSE_HEAD &&
(this.resourceAction == ACTION_TYPE.ATTACK || this.resourceAction == ACTION_TYPE.RIDE_ATTACK)) {
if (clip.events.length == 0) {
clip.events.push({
frame: clip.duration + (AnimaConfig.DEATH_FRAME - 2) / this.frameRateNum,
func: "attackFrameEvent",
params: [clip.duration + ""]
});
}
}
// 在播放动画前重置事件
clip.events = [];
clip.events.push({
frame: clip.duration + (AnimaConfig.DEATH_FRAME - 2) / this.frameRateNum,
func: "attackFrameEvent",
params: [clip.duration + ""]
});
if (!isValid(this.clipBody)) {
return;
}
if (!this.activeInHierarchy) {
console.error('Node is not active in the scene');
this.active = true;
}
let parentNode = this.parent;
while (parentNode) {
if (!parentNode.activeInHierarchy) {
console.warn("Parent node was not active. Activating parent node...");
parentNode.active = true;
}
parentNode = parentNode.parent;
}
//this.getComponent(Sprite).spriteFrame = loadFrames.getSpriteFrames()[0];
try {
let animation = this.getComponent(Animation);
animation.playOnLoad = true;
if (this.resourceType == "pet") {
console.log(loadFrames.getSpriteFrames());
animation.off(Animation.EventType.PLAY); // 移除之前的监听器
animation.off(Animation.EventType.LASTFRAME); // 移除之前的监听器
animation.on(Animation.EventType.PLAY, () => {
console.log('Animation started:', clip.name);
});
animation.on(Animation.EventType.LASTFRAME, () => {
if (!this.activeInHierarchy) {
console.error("Node became inactive during animation playback.");
} else {
const currentSpriteFrame = this.getComponent(Sprite).spriteFrame;
console.log('Current SpriteFrame on LASTFRAME:', currentSpriteFrame);
}
});
}
// 重新初始化动画组件和状态
animation.stop();
animation.clips.forEach((clip) => {
let state = animation.getState(clip.name);
if (state && state.isPlaying) {
animation.stop();
}
//console.log('clean animation state:', state)
});
animation.removeClip(clip);
// animation.addClip(clip, clipName);
if (animation.clips.indexOf(clip) === -1) {
animation.addClip(clip, clipName);
}
if (this.resourceAction == clipAction && this.resourceDirect == clipDirect) {
let animaState: AnimationState = animation.getState(clip.name);
if (!animaState) {
animaState = animation.createState(clip, clip.name);
}
//console.log('Current animation state:', animaState);
// 确保每次播放动画时,重置动画状态
if (animaState) {
animaState.wrapMode = AnimationClip.WrapMode.Loop;
animaState.speed = this.frameSetRate;
animaState.time = 0; // 重置时间
if (this.resourceType == "pet") {
console.log("Attempting to play animation:", clip.name);
}
animation.play(clip.name);
let currentState = animation.getState(clip.name);
if (this.resourceType == "pet") {
if (currentState && currentState.isPlaying) {
console.log("Animation is playing:", animaState);
} else {
console.warn("Animation failed to play:", clip.name);
}
console.log("Clip duration:", clip.duration);
console.log("Number of frames in clip:", loadFrames.getSpriteFrames().length);
}
} else {
console.warn("动画状态未初始化,无法播放:", clip.name);
}
}
} catch (e) {
clip = null;
Tool.log("loadClip 发生错误:" + e.stack);
}
}
return clip;
}
