cocos-engine icon indicating copy to clipboard operation
cocos-engine copied to clipboard

spine隐藏时调用setAnimation报错

Open kaxifakl opened this issue 1 year ago • 2 comments

Cocos Creator version

3.8.3

System information

Window 10

Issue description

1.节点一开始active=false,调用setAnimation播放动画,此时会报错

    protected onLoad(): void {
        this.skeleton.setAnimation(0, 'run', true);
        this.skeleton.node.active = true;

        console.log(this.skeleton)
    }

image 2.手动调用skeleton初始化,此时不会报错,但动画不会播放

    protected onLoad(): void {
        if (!this.skeleton._skeleton) {
            this.skeleton.__preload()
        }
        this.skeleton.setAnimation(0, 'run', true);
        this.skeleton.node.active = true;

        console.log(this.skeleton)
    }

Relevant error log output

No response

Steps to reproduce

打开上传的项目,运行scene场景

Minimal reproduction project

NewProject_383.zip

kaxifakl avatar May 23 '24 08:05 kaxifakl

@kaxifakl setAnimation先改成 在node.active=true后面,这个问题后面会修复

bofeng-song avatar May 24 '24 03:05 bofeng-song

哪个版本会修复?

kaxifakl avatar May 24 '24 06:05 kaxifakl

@bofeng-song 这个问题还有后续吗

kaxifakl avatar Oct 30 '24 02:10 kaxifakl

385处理

bofeng-song avatar Oct 30 '24 03:10 bofeng-song

@bofeng-song 这个操作只是不让它报错,在未激活前setAnimation还是无效的。是否有一些缓存机制让animationName缓存下来呢?

kaxifakl avatar Oct 30 '24 07:10 kaxifakl

@bofeng-song 这个操作只是不让它报错,在未激活前setAnimation还是无效的。是否有一些缓存机制让animationName缓存下来呢?

image active后,会设置skeletonData,这个时候需要重新设置animationName,不然animation可能是不存在的。

bofeng-song avatar Oct 30 '24 07:10 bofeng-song

@bofeng-song 那这时候是否可以直接用缓存的animationName赋值呢,并且验证一下animationName是否有效

kaxifakl avatar Oct 30 '24 07:10 kaxifakl

@kaxifakl setSkeletonData 接口正常只做和skeletonData有关的事情,不应该顺带执行setAnimation,这样会职责不清晰

bofeng-song avatar Oct 30 '24 08:10 bofeng-song

@bofeng-song 这里的问题应该是,skeleton组件未激活前,手动赋值了SkeletonData,但__preload()方法未执行,所以this._skeleton为空,此时setAnimation, 会在这里被return image

kaxifakl avatar Oct 30 '24 08:10 kaxifakl

@bofeng-song https://github.com/cocos/cocos-engine/issues/17784 这个issue也是同样的问题,SkeletonData赋值后,setAnimation应该设计为可以被成功调用,即使节点未被激活

kaxifakl avatar Oct 30 '24 08:10 kaxifakl

@kaxifakl https://github.com/cocos/cocos-engine/issues/17784 这个的根本问题是节点重用,skeleton因为skeletonData设置null而被置空,第二次使用时,未对skeleton进行赋值

bofeng-song avatar Oct 30 '24 09:10 bofeng-song