# Create object randomly around Player.

0 favourites
• 8 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
• 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.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• 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!

• random(-50,50) works too.

spawn object at player.x+random(-50,50), player.y+random(-50,50)

• 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!

• haha yep, i know the feeling..!

• 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?

• 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)

• 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.

• 8 posts