# Create object randomly around Player.

Get help using Construct 2

### » Thu Nov 01, 2012 8:28 am

Hi, I want to spawn stars dynamically around the player. So new stars spawn when he moves.

Problem is... Creating them at player.X/Y + (int(random)) is only going to spawn DOWN and RIGHT.

I would like to spawn all the way around him.

B
40
S
10
G
4
Posts: 665
Reputation: 8,333

### » Thu Nov 01, 2012 3:05 pm

You have to create a formula that allows the randomized number to be negative. I did something similar, like this:

(For this example, lets say our pixel range we're trying to spawn within is 50.)
Spawn Object at ((player.X + (int(random(100))) - 50), ((player.Y + (int(random(100))) - 50)

Since the random number will be between 0 and 100, when it subtracts 50 it gives us a value between -50 and 50. Hope this helps!
B
7
G
1
Posts: 27
Reputation: 991

### » Thu Nov 01, 2012 3:10 pm

@whiteheat
random(-50,50) works too.

spawn object at player.x+random(-50,50), player.y+random(-50,50)
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

### » Thu Nov 01, 2012 3:14 pm

Hahaha, it seems this is how my forum posts always go: I post some kludged-together method I busted my butt figuring out, and then some more skilled and experienced user shows me a simpler, more efficient way to do the same thing :D

Thanks @keepee, it's how I learn!
B
7
G
1
Posts: 27
Reputation: 991

### » Thu Nov 01, 2012 3:27 pm

@whiteheat

haha yep, i know the feeling..!
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

### » Thu Nov 01, 2012 5:25 pm

Thanks you two ^^

I think I go with the simple one for now :P

Although I don't quite get it.
First it randoms 0 to -50 and then 0 to 50 and finds a number in between those?

B
40
S
10
G
4
Posts: 665
Reputation: 8,333

### » Thu Nov 01, 2012 5:42 pm

Nope, it just returns a random number in between the two values.

and if you only put one number in like random(5) it will interpret that like random(0,5)
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

### » Thu Nov 01, 2012 6:14 pm

That makes sense duh ^^ Just like normal.

Well that solves that question for now.

I still have to find a way to create 500 pixels away from the player ^^ That is gonna get a bit tricky Heh.
B
40
S
10
G
4
Posts: 665
Reputation: 8,333