All Game Objects have a new method setRandomPosition which will randomly position them anywhere within the defined area, or if no area is given, anywhere within the game size.

This commit is contained in:
Richard Davey 2018-05-15 15:58:53 +01:00
parent 2f4358fed4
commit 5b0cb0faf3
2 changed files with 33 additions and 0 deletions

View file

@ -28,6 +28,7 @@ The new Plugin Manager and associated classes are 100% covered by JSDocs and the
* WebGLRenderer has a new property `compression` which holds the browser / devices compressed texture support gl extensions, which is populated during `init`. * WebGLRenderer has a new property `compression` which holds the browser / devices compressed texture support gl extensions, which is populated during `init`.
* When calling `generateFrameNames` to define an animation from a texture atlas you can now leave out all of the config properties and it will create an animation using every frame found in the atlas. Please understand you've no control over the sequence of these frames if you do this and it's entirely dictated by the json data (thanks @Aram19) * When calling `generateFrameNames` to define an animation from a texture atlas you can now leave out all of the config properties and it will create an animation using every frame found in the atlas. Please understand you've no control over the sequence of these frames if you do this and it's entirely dictated by the json data (thanks @Aram19)
* The keycodes for 0 to 9 on the numeric keypad have been added. You can now use them in events, i.e. `this.input.keyboard.on('keydown_NUMPAD_ZERO')` (thanks @Gaushao) * The keycodes for 0 to 9 on the numeric keypad have been added. You can now use them in events, i.e. `this.input.keyboard.on('keydown_NUMPAD_ZERO')` (thanks @Gaushao)
* All Game Objects have a new method `setRandomPosition` which will randomly position them anywhere within the defined area, or if no area is given, anywhere within the game size.
### Updates ### Updates

View file

@ -235,6 +235,38 @@ var Transform = {
return this; return this;
}, },
/**
* Sets the position of this Game Object to be a random position within the confines of
* the given area.
*
* If no area is specified a random position between 0 x 0 and the game width x height is used instead.
*
* The position does not factor in the size of this Game Object, meaning that only the origin is
* guaranteed to be within the area.
*
* @method Phaser.GameObjects.Components.Transform#setRandomPosition
* @since 3.8.0
*
* @param {number} [x=0] - The x position of the top-left of the random area.
* @param {number} [y=0] - The y position of the top-left of the random area.
* @param {number} [width] - The width of the random area.
* @param {number} [height] - The height of the random area.
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
setRandomPosition: function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.scene.sys.game.config.width; }
if (height === undefined) { height = this.scene.sys.game.config.height; }
this.x = x + (Math.random() * width);
this.y = y + (Math.random() * height);
return this;
},
/** /**
* Sets the rotation of this Game Object. * Sets the rotation of this Game Object.
* *