The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript problem with for loop?

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #275669

    this isn’t a very straight-forward question.

    I’m trying to build a game with javascript. in the game, the player can press buttons to move from room to room. The player’s position is tracked by x and y coordinates. For example, when the player pushes the forward button, their y coordinate goes down by 1.

    What I want to have happen is that if the player moves to a coordinates where they have not yet been, a room (javascript object) is created. If the player moves to coordinates that they have already been to, a new room should NOT be created.

    what is happening is that a new room is created regardless of whether or not the player has visited those coordinates – except for the original starting room.

    I think it may be an issue with a for loop that loops through each room object. I think it is exiting at the first iteration (starting room). This loop is at line 59 in my codepen. I need the loop to run through every room object and check the room’s coordinates against the player’s coordinates.


    I think the problem is that you are only checking for the first room if all the code is placed inside the loop. You might want to go through the entire array first and only then create a new room if it doesn’t exist yet.

    Posting the code itself seems to get firewalled at the moment…

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.