This forum is read only and just serves as an archive. If you have any questions, please post them on

5 years ago by Brushbender

Is there any way of making entities spawn at random positions?

This is my code so far.
		var asteroidSettings;
		for (var i = 0; i < 20; i++) {
		asteroidSettings = {vel: {x: 100-Math.random()*200, y: 100-Math.random()*200}};
		this.spawnEntity(EntityAsteroid, ig.system.width/2, ig.system.height/2, asteroidSettings);

5 years ago by stahlmanDesign

Use instead of this.spawnEntity(...) unless you are in main.js

Otherwise it looks like it should be working.

5 years ago by Joncom

var x = Math.random() * 1000; // random x between 0 and 1000
var y = Math.random() * 1000; // random y between 0 and 1000, x, y);

5 years ago by lTyl

var random = Math.round( Math.random() * <Max_Value> ) + <min_Value>

5 years ago by lTyl

Also note, the code you provided:
        asteroidSettings = {vel: {x: 100-Math.random()*200, y: 100-Math.random()*200}};

If your random number is less than 100, you will be spawning an entity off the visible drawing area.

So you could use:
        asteroidSettings = {vel: {x: 100- (Math.random()*200)+100, y: (Math.random()*200)+100}};

Which will give you a random number between 100 and 200 (So the lowest possible value is 0)
Page 1 of 1
« first « previous next › last »