Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2015
    Location
    new york
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.
    Last edited by vinyl-junkie; 03-04-2015 at 02:04 PM. Reason: self-promotional link removed

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,508
    Thanks
    205
    Thanked 2,581 Times in 2,559 Posts
    So what is the question?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jan 2015
    Location
    new york
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why is random image spawner doesnt work after a while
    Thank's!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,508
    Thanks
    205
    Thanked 2,581 Times in 2,559 Posts
    Quote Originally Posted by fptquangngai View Post
    why is random image spawner doesnt work after a while
    Thank's!
    Because you have used all the random images?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Mar 2015
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,508
    Thanks
    205
    Thanked 2,581 Times in 2,559 Posts
    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.
    Last edited by Philip M; 03-05-2015 at 06:49 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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