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.
2 Answers
+ 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.
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)