简体   繁体   中英

Don't understand what this code does

So I know this code is randomly removing to of my objects to create a hole so another object can go through but line by line I would like to go through and understand each part. Would be great if someone not so arrogant could help me because I'm new. I would appreciate any help. The area I don't understand is the last part which I have highlighted in bold. Thank you.

   // Add a pipe on the screen
add_one_pipe: function(x, y) {
    // Get the first dead pipe of our group
    var pipe = this.pipes.getFirstDead();

    // Set the new position of the pokeballs
    pipe.reset(x, y);

     // Add velocity to the pokeballs to make it move left
    pipe.body.velocity.x = -200; 

    // Kill the pokeballs when it's no longer visible 
    pipe.outOfBoundsKill = true;
},

**add_row_of_pipes: function() {
    var hole = Math.floor(Math.random()*5)+1;**

    **for (var i = 0; i < 8; i++)
        if (i != hole && i != hole +1) 
            this.add_one_pipe(400, i*60+10);**   

add_row_of_pipes will add 6 pipes at fixed intervals heights, but with a randomly placed gap of 2 missing pipes.

var hole = Math.floor(Math.random()*5)+1;

  • Take a random number (between 0 and 0.999),
  • Multiply by 5 (possible range is now 0-4.9999...),
  • Round down (0-4),
  • Add one (1-5)
  • This value is where the hole is

for (var i = 0; i < 8; i++)

  • For the whole numbers 0 to 7 inclusive, representing height, i ...

     if (i != hole && i != hole +1) 
  • If this height is not where the hole starts, nor the next value,

      this.add_one_pipe(400, i*60+10); 
  • Add a pipe at width 400, and height i*60+10.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM