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

Tween.setCallback bugs if optional argument (param) is not passed, also the function gets called 3 times instead of once #6047

Closed
orcomarcio opened this issue Mar 21, 2022 · 5 comments

Comments

@orcomarcio
Copy link

orcomarcio commented Mar 21, 2022

Version

  • Phaser Version: 3.55.2
  • Operating system: Windows 10
  • Browser: Chrome

TL;DR

In the API Documentation here says that with Tween.setCallback the params argument is optional, but it throws an error if you don't pass anything. It works with a workaround but it gets called 3 times instead of 1.

Primary Bug

Calling this method for a tween gives an error
testAlertTween.setCallback('onYoyo', function(target){ console.log('test') });

The error gets thrown in the framework at this line (68647)
callback.params[1] = tweenData.target;

Basically it references the param's elements without checking first if it the argument have been passed

The code doesn't give error if I pass an empty array like this
testAlertTween.setCallback('onYoyo', function(target){ console.log('test') }, []);

Secondary Bug

Even though it works with this workaround, it still gets called 3 times in the same cycle instead of once with 'onYoyo' and 'on Repeat.

For more information here's the Tween:
var testAlertTween = this.tweens.add({ targets: testSprite, x: testSprite.x+400, duration: 2500, rotation: 0.3, scale: 1.5, ease: 'Linear', yoyo: true, repeat: -1, });

@orcomarcio orcomarcio changed the title Tween.setCallback bugs if optional argument (param) is not passed, also 'onYoyo' gets called 3 times instead of once Tween.setCallback bugs if optional argument (param) is not passed, also the function gets called 3 times instead of once Mar 21, 2022
@samme
Copy link
Contributor

samme commented Mar 21, 2022

I think this is #5755

Can you make an example for the second bug?

@orcomarcio
Copy link
Author

I have a sprite with a repeating animation (this one)
this.tweens.add({ targets: testSprite, x: testSprite.x+400, duration: 2500, rotation: 0.3, scale: 1.5, ease: 'Linear', yoyo: true, repeat: -1, onRepeat: function (tween, targets) { console.log('test') }, });

When it repeats the console logs 'test' 3 times in the same cycle each time the animation loops. Same happens if I use onYoyo. If I instead put onComplete (after removing repeat:-1 of course) it works as intended and logs 'test' only once.

I made sure the tween is created only once, and this is confirmed by the fact that onComplete works as intended (otherwise it could be an error on my side like creating 3 tweens that work in tandem, but it doesn't seem to be the case). I'm gonna take a look at the source code to see if I can pinpoint the problem.

@orcomarcio
Copy link
Author

orcomarcio commented Mar 21, 2022

UPDATE
I understood what happens. I found out that it calls the function 3 times because 3 values are animated in the same tween (x, rotation and scale), if I animate only one value it logs only once.

I'm not an expert but it seems to me it's not behaving as intended, especially since it's not consistent with how onRepeat works, which gets called only once regardless of the number of values animated.

@samme
Copy link
Contributor

samme commented Mar 21, 2022

That's the correct behavior. See TweenBuilderConfig.

@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

As for the second issue, this is the correct behavior. It should fire once per property being tweened.

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

No branches or pull requests

3 participants