Added addedToScene and removedFromScene methods

* `GameObject.addedToScene` is a new method that custom Game Objects can use to perform additional set-up when a Game Object is added to a Scene. For example, Sprite uses this to add itself to the Update List.
* `GameObject.removedFromScene` is a new method that custom Game Objects can use to perform additional tear-down when a Game Object is removed from a Scene. For example, Sprite uses this to remove themselves from the Update List.
* Game Objects no longer automatically remove themselves from the Update List during `preDestroy`. This should be handled directly in the `removedFromScene` method now.
This commit is contained in:
Richard Davey 2020-08-24 19:18:29 +01:00
parent bc93416ec7
commit 671c92e450

View file

@ -58,9 +58,9 @@ var GameObject = new Class({
/**
* The current state of this Game Object.
*
*
* Phaser itself will never modify this value, although plugins may do so.
*
*
* Use this property to track the state of a Game Object during its lifetime. For example, it could change from
* a state of 'moving', to 'attacking', to 'dead'. The state value should be an integer (ideally mapped to a constant
* in your game code), or a string. These are recommended to keep it light and simple, with fast comparisons.
@ -230,9 +230,9 @@ var GameObject = new Class({
/**
* Sets the current state of this Game Object.
*
*
* Phaser itself will never modify the State of a Game Object, although plugins may do so.
*
*
* For example, a Game Object could change from a state of 'moving', to 'attacking', to 'dead'.
* The state value should typically be an integer (ideally mapped to a constant
* in your game code), but could also be a string. It is recommended to keep it light and simple.
@ -337,7 +337,7 @@ var GameObject = new Class({
* before setting the value.
*
* If the key doesn't already exist in the Data Manager then it is created.
*
*
* When the value is first set, a `setdata` event is emitted from this Game Object.
*
* @method Phaser.GameObjects.GameObject#incData
@ -367,7 +367,7 @@ var GameObject = new Class({
* before setting the value.
*
* If the key doesn't already exist in the Data Manager then it is created.
*
*
* When the value is first set, a `setdata` event is emitted from this Game Object.
*
* @method Phaser.GameObjects.GameObject#toggleData
@ -505,7 +505,7 @@ var GameObject = new Class({
* If you wish to only temporarily stop an object from receiving input then use
* `disableInteractive` instead, as that toggles the interactive state, where-as
* this erases it completely.
*
*
* If you wish to resize a hit area, don't remove and then set it as being
* interactive. Instead, access the hitarea object directly and resize the shape
* being used. I.e.: `sprite.input.hitArea.setSize(width, height)` (assuming the
@ -525,6 +525,36 @@ var GameObject = new Class({
return this;
},
/**
* This callback is invoked when this Game Object is added to a Scene.
*
* Can be overriden by custom Game Objects, but be aware of some Game Objects that
* will use this, such as Sprites, to add themselves into the Update List.
*
* You can also listen for the `ADDED_TO_SCENE` event from this Game Object.
*
* @method Phaser.GameObjects.GameObject#addedToScene
* @since 3.50.0
*/
addedToScene: function ()
{
},
/**
* This callback is invoked when this Game Object is removed from a Scene.
*
* Can be overriden by custom Game Objects, but be aware of some Game Objects that
* will use this, such as Sprites, to removed themselves from the Update List.
*
* You can also listen for the `REMOVED_FROM_SCENE` event from this Game Object.
*
* @method Phaser.GameObjects.GameObject#removedFromScene
* @since 3.50.0
*/
removedFromScene: function ()
{
},
/**
* To be overridden by custom GameObjects. Allows base objects to be used in a Pool.
*
@ -651,7 +681,6 @@ var GameObject = new Class({
if (!fromScene)
{
sys.displayList.remove(this);
sys.updateList.remove(this);
}
if (this.input)