Add incData, toggleData method

This commit is contained in:
Rex 2020-02-05 16:17:54 +08:00
parent 58d40fa9e0
commit 307c2b1584
2 changed files with 125 additions and 0 deletions

View file

@ -279,6 +279,72 @@ var DataManager = new Class({
return this;
},
/**
* Increase a value for the given key. If the key doesn't already exist in the Data Manager then it is increased from 0.
*
* When the value is first set, a `setdata` event is emitted.
*
* @method Phaser.Data.DataManager#inc
* @fires Phaser.Data.Events#SET_DATA
* @fires Phaser.Data.Events#CHANGE_DATA
* @fires Phaser.Data.Events#CHANGE_DATA_KEY
* @since 3.23.0
*
* @param {(string|object)} key - The key to increase the value for.
* @param {*} [data] - The value to increase for the given key.
*
* @return {Phaser.Data.DataManager} This DataManager object.
*/
inc: function (key, data)
{
if (this._frozen)
{
return this;
}
if (data === undefined)
{
data = 1;
}
var value = this.get(key);
if (value === undefined)
{
value = 0;
}
this.set(key, (value + data));
return this;
},
/**
* Toggle a boolean value for the given key. If the key doesn't already exist in the Data Manager then it is toggled from false.
*
* When the value is first set, a `setdata` event is emitted.
*
* @method Phaser.Data.DataManager#toggle
* @fires Phaser.Data.Events#SET_DATA
* @fires Phaser.Data.Events#CHANGE_DATA
* @fires Phaser.Data.Events#CHANGE_DATA_KEY
* @since 3.23.0
*
* @param {(string|object)} key - The key to toggle the value for.
*
* @return {Phaser.Data.DataManager} This DataManager object.
*/
toggle: function (key)
{
if (this._frozen)
{
return this;
}
this.set(key, !this.get(key));
return this;
},
/**
* Internal value setter, called automatically by the `set` method.
*

View file

@ -330,6 +330,65 @@ var GameObject = new Class({
return this;
},
/**
* Increase a value for the given key within this Game Objects Data Manager. If the key doesn't already exist in the Data Manager then it is increased from 0.
*
* If the Game Object has not been enabled for data (via `setDataEnabled`) then it will be enabled
* 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
* @since 3.23.0
*
* @param {(string|object)} key - The key to increase the value for.
* @param {*} [data] - The value to increase for the given key.
*
* @return {this} This GameObject.
*/
incData: function (key, value)
{
if (!this.data)
{
this.data = new DataManager(this);
}
this.data.inc(key, value);
return this;
},
/**
* Toggle a boolean value for the given key within this Game Objects Data Manager. If the key doesn't already exist in the Data Manager then it is toggled from false.
*
* If the Game Object has not been enabled for data (via `setDataEnabled`) then it will be enabled
* 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
* @since 3.23.0
*
* @param {(string|object)} key - The key to toggle the value for.
*
* @return {this} This GameObject.
*/
toggleData: function (key)
{
if (!this.data)
{
this.data = new DataManager(this);
}
this.data.toggle(key);
return this;
},
/**
* Retrieves the value for the given key in this Game Objects Data Manager, or undefined if it doesn't exist.
*