0

I'm sure this is a simple question but I'm asking it anyways. Basically I'm trying to do the following in javascript (no jquery) with less code:

var Ground = [];

function gameLoop()
{
    Gravity += 0.2

    if(Ground[0].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }
    if(Ground[1].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }
    if(Ground[2].isCollided(Player))
    {
        Player.dy = 0;
        Gravity = 0;
    }

    Player.dy = Gravity;
}

I've simplified the code extremely from the code in my game I'm making using javascript and the html5 canvas. What you see is my Ground mechanic as it stands. When the player collides with the Ground Block, the Player's .dy value will no longer change along with the Gravity.

What you saw above is how I would normally do it however this can take up a large amount of space if I have 50 FallingM variables. Is it possible to do something like:

if(Ground[i].isCollided(Player))
{
    Player.dy = 0;
    Gravity = 0;
}

So that the function will still run no matter how many Ground variables I add? Thanks ahead of time!

cs95
  • 379,657
  • 97
  • 704
  • 746
Nathan Martin
  • 295
  • 1
  • 3
  • 13
  • 2
    You're looking for the wonders of loops. – SLaks Jun 06 '17 at 22:11
  • 2
    Far far beyond the land of the loops lies the mystic kingdom of `Array.prototype.some`... – le_m Jun 06 '17 at 22:14
  • Possible duplicate of [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript?rq=1) – cs95 Jun 06 '17 at 22:19

2 Answers2

2

You should use a loop:

for(var x of Ground) {
   if(x.isCollided(Player)) {
      Player.dy = 0;
      Gravity = 0;
      break;
   }
}
cs95
  • 379,657
  • 97
  • 704
  • 746
  • @SterlingArcher Sorry, feel free to downvote. It's just that, it's a little odd to be developing games with javascript if you don't know what a loop is. – cs95 Jun 06 '17 at 22:17
  • 2
    While that might be true, there's no room for snark in Stack Overflow answers. Stay informative and professional. Also, today's JS devs don't use for loops like that to iterate arrays, we use `for..of`. – Madara's Ghost Jun 06 '17 at 22:21
  • Just remember that at one point, we were all developers that didn't know what a loop is. – Sterling Archer Jun 06 '17 at 22:21
  • Hmm it doesn't seem to be working. Give me a second. – Nathan Martin Jun 06 '17 at 22:23
  • @SOSenpais, Edited. – cs95 Jun 06 '17 at 22:24
  • Woops. I was actually editing something in the game and forgot to finish it before I asked this XP. I'll let you know once I finish it. – Nathan Martin Jun 06 '17 at 22:30
  • It works! Thanks a bunch! I've seen other people use loops but I never actually understood how they worked (probably because I didn't need to yet). But after looking at this. I think it makes sense now! – Nathan Martin Jun 06 '17 at 22:33
  • @NathanMartin Glad to hear. Apologies for the snarkiness. We're all learners here. All the best. – cs95 Jun 06 '17 at 22:34
  • It's perfectly fine! I have pretty thick skin anyways. I'm just glad you were willing to help. (I mean I did say that I knew it had a basic answer. I just didn't know what it was) – Nathan Martin Jun 06 '17 at 22:36
0

Remember, a Jedi's strength flows from the loops. But beware. Map, reduce, some. The dark side are they. Once you start down the dark path, forever will it dominate your destiny.

if (grounds.some(ground => ground.isCollided(player))) {
  player.dy = 0;
  gravity = 0;
}
le_m
  • 19,302
  • 9
  • 64
  • 74