Story Behind the Game
In my latest project, I am delving into the world of creating a captivating 2D side-scrolling game using HTML and JavaScript. To ensure smooth gameplay, I have opted to incorporate ES6 for efficient management of all game objects.
Coding Strategy
To keep track of the platforms in each level, I have structured 2 arrays - one for short platforms and another for long platforms.
shortPlatform
longPlatform
For every platform in the game, there exists a distinct loop responsible for detecting collisions between the player character and the specific platform in question.
However, an issue has surfaced where the loop is only registering collisions with the very last platform.
Outlined below are the snippets of code illustrating the two loops:
for (let i = 0; i < shortPlatform.length; i++)
{
shortPlatform[i].renderObject();
// check if collide with players
var dir = colCheck(player, shortPlatform[i]);
}
for (let x = 0; x < longPlatform.length; x++)
{
longPlatform[x].renderObject();
// check if collide with players
var dir2 = colCheck(player, longPlatform[x]);
};
Note of Concern
Despite combing through various suggestions on closures, I am still unable to implement any viable solutions from the existing answers.