Stop Tween For One Child

I’ve been trying for the last day and a half to get this to work, to no avail. My current code pauses the tween for everything, which I understand because it’s pausing the tween as a whole. I just can’t figure out how to pause the tween for only one member of the group but keep it active for everything else. (I’m trying to recreate Super Mario N.E.S. for my first project and currently working on the piranha plants that stop when you’re near them.)

gameState.piranhaMove = this.tweens.add({
            targets: gameState.piranhaPlants.getChildren(),
            y: '+=26',
            duration: 2000,
            ease: 'Linear',
            hold: 2000,
            yoyo: true,
            repeat: -1
        });

Phaser.Actions.Call(gameState.piranhaPlants.getChildren(), function(piranha) {
            if (gameState.player.x > piranha.x - 32 && gameState.player.x < piranha.x + 32) {
                gameState.piranhaMove.on('yoyo', function() {
                    piranha.pause();
                }, this);
            };
        });

You cannot specify a specific target in a tween.
An option would be to splice gameState.piranhaMove.data with the targetIndex. To keep the tween manager happy you’ll then have to decrease/increase gameState.piranhaMove.totalData by the number of deleted/added targets.

1 Like

How would I splice the targetIndex? Do I need to name the children for that?

You could give each child an (insert) id.
Or just use a for loop from 0 to getChildren.length - 1.

1 Like

Thanks much for the response. I wasn’t sure how to do exactly what you’re suggesting, but, you did give me an idea. I did array the targets like you suggested and I’m now disabling that target’s visibility and disabling the body when the player gets near. So the tween is always running, I just can’t see or interact with that plant while close now.

Update: my workaround didn’t work after all, as soon as I’d destroy one of the children the tween stopped working entirely anyways. This time around I created a loop to make multiple tweens, one for each object, everything is working fine now. I need to learn how to push data though, as suggested, I assume it would be much easier and use less resources.

This example splices the tween data. Just click on a fish to toggle tween state.

1 Like

That’s a big help, thanks much!