# Random, velocity and center

## Recommended Posts

I created two variables:

X = game.world.randomX;

Y = game.world.randomY;

But I would like to create a random Min and Max

That I have a 600x400 game, I want the objects appear out of the game and go to the middle of the game (also close to downtown, random). With random speed.

I have also used:

ball.body.velocity.x = 100 + Math.random () * 200;

How does the math random?, Sorry too many questions but I have difficulties with English you can not find good guides.

##### Share on other sites

minX = this.rnd.integerInRange(0, 200);

maxX = this.rnd.integerInRange(200, 400);

The rnd will pick randomly between 2 given numbers. In the example above minX can be anything between 0 and 200.

##### Share on other sites

....

function creaAsteroide() {

X=game.world.randomX;

Y=game.world.randomY;

// asteroide.body.velocity.x=100+Math.random()*200;

// asteroide.body.velocity.y=100+Math.random()*50;

}

So the asteroid is random in the game, but outside? how can I? I did not understand. Excuse me.

##### Share on other sites

asteroide.body.velocity.x= this.rnd.integerInRange(minimum number, maximum number);

Its always a random amount with the rnd built in to phaser. So if you wanted the speed of it to be between 200 and 500, you could use integerInRange to decide randomly between the 2 numbers.

So the asteroid could come in at 356 velocity and the next could be 297 speed. It will use any number between the 2 given numbers you specify it to be.

##### Share on other sites

Ok  I understand but

##### Share on other sites

If:

`this.rnd`

doesn't work then use:

`game.rnd`
##### Share on other sites

Thaaaaanks Rich. You're doing a great job with Phaser, you are a great man.

Sorry if I did not understand but they are new, it is unfortunately not knowing English well, I have difficulty to understand the few guides.

##### Share on other sites

Yeah sorry I use states so a majority of my code is:

this.world or this.rnd

If this doesn't work then yeah as Rich said, use game.rnd or game.world. Glad you got it up and running though.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.