Why Cocos creator 3.8 tween doesnt work into async func (with Promise)

188 views Asked by At

I`m new in Cocos Creator engine, and now completely stuck with tweening.

I have next tween func in my code:

    private magnetTween(pos: Vec3): Tween<Readonly<Vec3>>
    {
        const offset = Math.abs(this.node.worldPosition.x - pos.x);
        const duration = offset / this.gameConfig.CameraConfig.MagnetSpeed;
        console.log('tween duration ' + duration)

        return tween(this.node.worldPosition).to(duration, pos,
            {
                onUpdate: (target: Vec3) => 
                {
                    this.node.setWorldPosition(target);
                }
            });
    }

And also the next Promise that I really need to await in other classes

    public async magnetTo(obj: Node): Promise<void>
    {
        console.log('magnet func start');

        const pos = obj.getWorldPosition();

        const prm = new Promise<void>((resolve, reject) => {
            this.magnetTween(pos)
             .call(() => resolve())
             .start();
        });

        await prm;
    }

But it doesnt work.

After some tests I investigated that:

  1. Tween works fine, if it was called out of async func. If I call it like this:
    protected start(): void 
    {
        const pos = new Vec3(10000, 0, 0);

        const promise = new Promise<void>((resolve, reject) => { this.magnetTween(pos)
             .call(() => resolve())
             .start();});

        promise.then(value => console.log('completed'))
    }

I get my completed log after node movement.

  1. Promise in my async func also works fine. If I use the syntax below, I also get my completed log after timeout
    public async magnetTo(obj: Node): Promise<void>
    {
        console.log('magnet func start');

        this.delay = 1000;
        const prn = new Promise<number>((resolve, reject) => 
        {
           setTimeout(resolve, this.delay);
        });

        await prn;
        console.log('completed')
    }

There is a very small amount of info about Cocos in web, so I really need help.

1

There are 1 answers

2
Cbubg On

Okay, there was a problem in another class, where I called Tween.Stop() func at the same moment. This func stops ALL of active tweens on scene, so my tween obviosly stopped after start. There is no issue with Promises!