Event when object added to scene?

I am trying to get tweening working on a sprite that is a child of a Container-subclass, however the tween does not work. I am setting-up the tween within the Container-subclass constructor() when the Container is not yet added to the scene, which I assume is the reason it’s not working.

Is there an event I can hook that occurs when a game object is added to the scene, or do I need to use a bespoke method of my own?

Cheers,
Andy

:wave: A tween can work on anything, but I guess it’s possible you won’t get certain values you want until after the child and Container are added to the display list. I’m not sure.

You could set this.sys.displayList.addCallback but it seems a bit much. Can you just add the tween after the Container is in the scene?

1 Like

Yeah I’ve moved the tween to preUpdate() and it’s still not working, so it’s the tween that’s the issue - not where I’m setting it up :smile: I am generally interested if there is such a lifecycle callback on game objects though, as it could be useful?

There’s nothing on the game object itself, AFAIK. There is this.sys.displayList.addCallback and this.sys.updateList PROCESS_QUEUE_ADD event.

OK thanks.

BTW the reason my tween wasn’t working was because I hadn’t passed paused: false when creating it, which seems strange as all examples I have seen don’t need that as paused is false by default. Does having the tween on the sprite within a container affect this, do you know?

How was the tween not working?

The sprite was not rotating. Its the upper part of a tail and was not working until I added paused: false.

const finMovementAmount = 10;
const finMovementTime = 300;
this.tailUpperTween = this.scene.tweens.add({
	targets: this.tailUpperSprite,
	angle: { from: -finMovementAmount, to: finMovementAmount },
	ease: 'Linear',
	duration: finMovementTime,
	yoyo: true,
	repeat: -1,
	paused: false
});

Well, that is unusual.

I cannot play with it atm. I will remove that line and see if that stops it working again. You know how it is - multiple things changing at once etc…

Well I removed the paused: false line and it still works! I cannot explain why it wasn’t working before and I tried for a couple of hours :expressionless: Oh well, nvm.