Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweens: Duration of 0 Makes Values NaN (Alpha) #6955

Open
kainage opened this issue Nov 19, 2024 · 4 comments
Open

Tweens: Duration of 0 Makes Values NaN (Alpha) #6955

kainage opened this issue Nov 19, 2024 · 4 comments

Comments

@kainage
Copy link
Contributor

kainage commented Nov 19, 2024

Version

  • Phaser Version: 3.87.0 && v4.0.0-beta.1

Description

Adding a tween with a duration of 0 leaves the object in an invalid state with NaN as the value and cannot be tweened afterwards, unless the value is explicitly set to a valid one.

NOTE: This works in 3.86.0 and prior

Example Test Code

Tweak the sprite alpha example to a tween and see the outcome. This might be happening with other values, but I haven't tested those.

Sandbox

class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v385');
        this.load.image('bunny', 'assets/sprites/bunny.png');
    }

    create ()
    {
        const bunny = this.add.sprite(400, 300, 'bunny');

        this.add.tween(
            {
                targets: bunny,
                alpha: 0.5,
                duration: 0
            }
        )

        console.log(bunny)
    }
}

const config = {
    type: Phaser.WEBGL,
    parent: 'phaser-example',
    width: 800,
    height: 600,
    scene: Example
};

const game = new Phaser.Game(config);

@photonstorm
Copy link
Collaborator

What's the point of a duration of zero?

@kainage
Copy link
Contributor Author

kainage commented Nov 19, 2024

What's the point of a duration of zero?

When passing in dynamic values sometimes a 0 makes it's way down the chain. In a large codebase where this has worked for ages, it's a tricky one to track down. Either way, it shouldn't leave the sprite in a state where you cannot tween it back to a different value.

@GillesVermeulen
Copy link

I'd like to chime in that I also encountered this unexpected behavior.
In my case i was tweening the x/y-coordinates, and the tween duration is dynamically calculated by measuring the distance between the current and the target coordinates, so the longer the distance, the longer the duration of the tween. In the case where the new coordinates are the same as the current coordinates, the duration is calculated as zero and the gameobject disappears.

@samme
Copy link
Contributor

samme commented Nov 23, 2024

You can use Math.max(0.01, duration) to get the previous behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants