0

JavaScript problem again (position of the character)

Hello, My partner and have a mistake in the check where the game character is. We express the position with mapX and mapY and we wrote in the code that mapX and mapY should be more than 0 and mapX<=level.map.length and mapY<=numberBlocksWidth where numberBlocksWidth=CanvasWidth/blockSizeWidth. But in the last two (I’m talking about mapX<=level.map.length, mapY<=numberBlocksWidth) we are making a mistake and aren’t checking what we need to check. Can somebody tell us where we are making a mistake and how to correct it? Thank you in advance.

4th Feb 2020, 9:37 PM
Betina
Betina - avatar
2 odpowiedzi
+ 6
Can you show us a minimal version of your code. Your question is not clear. Maybe you should multiply the numberBlocksWidth by the total number of blocks.
5th Feb 2020, 3:07 AM
Kevin ★
0
const canvasWidth = canvas.clientWidth; const canvasHeight = canvas.clientHeight; const blockSizeWidth = 50; const blockSizeHeight = 50; const numberBlocksWidth = canvasWidth / blockSizeWidth; const blockLeftFromCharacter = numberBlocksWidth / 3; ... const mapX = nx / level.squareSizeX(); const mapY = ny / level.squareSizeY(); ... if (map[mapY][mapX] != '*' && map[mapY][mapX] != 'q' && mapX >= 0 && mapY >= 0 && mapX <= level.map.length && mapY <= numberBlocksWidth) { x = nx; y = ny; ("*" stands for a barriere, "q" stands for an invisible barriere)
5th Feb 2020, 7:54 AM
Betina
Betina - avatar