Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Question about random image spawner

Dear all member!

I have a random image spawner
after a while the same images keep going and no other images come

this is the code i use

Code:
```//enemies
var enemy;
var enemyTotal = 5;
var enemies = [];
var enemy_x = 50;
var enemy_y = -45;
var enemy_w = 50;
var enemy_h = 50;
var speed = 3;

var explosionimage = new Image();
explosionimage.src = "explosion.png";

//Build enemy array of x/y coordinate, width, height, and speed
for (var i = 0; i < enemyTotal; i++) {
//var speed=Math.floor(Math.random()*5) // verschillende snehlehden
for (var i = 0; i < enemyTotal; i++) {
var enemy = new Image();
enemy.src = 'enemy'+ Math.floor(Math.random() * 6) +'.png';

enemies.push([enemy_x, enemy_y, enemy_w, enemy_h, speed, enemy]);
enemy_x += enemy_w + 60;
}

console.log(enemies);

//Iterate through array of enemies and draw them on the canvas
function drawEnemies() {
for (var i = 0; i < enemies.length; i++) {
ctx.drawImage(enemies[i][5], enemies[i][0], enemies[i][1]);
}
}

//Iterate through array of enemies and update their position
function moveEnemies() {
for (var i = 0; i < enemies.length; i++) {
if (enemies[i][1] < height) {
enemies[i][1] += enemies[i][4];
} else if (enemies[i][1] > height - 1) {
//after the enemies scroll off screen, this value is where they get re-spawned
enemies[i][1] = -45;
}
}
}

//Check for laser collision
function hitTest() {
var remove = false;
for (var i = 0; i < lasers.length; i++) {
for (var j = 0; j < enemies.length; j++) {
//laser's y position is less than or equal to the enemy's y position plus its height -AND-
//laser's x position is greater than or equal to the enemy's x position -AND-
//laser's x position is less than or equal to the enemy's x position plus it's width
if (lasers[i][1] <= (enemies[j][1] + enemies[j][3]) && lasers[i][0] >= enemies[j][0] && lasers[i][0] <= (enemies[j][0] + enemies[j][2])) {
remove = true;

//draw();//hier explosion afspelen
enemies.splice(j, 1); //enemy at this array position no longer gets drawn
// enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed]);
enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed, enemy]);	//refill a new enemy in a random x position between 50 and 500
}
}
if (remove == true) {
lasers.splice(i, 1);  //laser at this array position no longer gets drawn
remove = false;
score += 10;
}
}
}

}```
Have a nice day.

2. So what is the question?

3. why is random image spawner doesnt work after a while
Thank's!

4. Originally Posted by fptquangngai
why is random image spawner doesnt work after a while
Thank's!
Because you have used all the random images?

5. number = Random.Range(1.0, 4.0);

if(number == 1)
{
//spawn enemy 1
}else if(number >1 && number <3)
{
//spawn enemy 2
}else if(number == 4)
{
//spawn enemy 3
}

6. You are trying to generate a random integer number - not a float. The random number can never be 4. Random.Range returns a random integer number between min [inclusive] and max [exclusive]

So should be

number = Random.Range(1, 5); // integer 1,2,3 or 4

But Random.Range() is Java, not Javascript. They are competely different languages.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•