0

I'm building an arcade game with plain javascript. I built bricks using images and not with canvas, I want to build a function that stops the character from moving also I want him to be able to jump over the objects, and if the character is on top of one of the bricks I want him to not dropdown.

I built an interval that checks that character top && left and also the bricks' left (still haven't built the top )

also each time I placed a brick in the level I also inserted them in an array that I could refer to them in a loop later.

**here's the code: **

if (
  characterLeft >= 1000 ||
  (characterLeft >= BrickLeftArray[b] - 160 &&
    characterLeft <= BrickLeftArray[b] + 80)
) {
  /*|| characterLeft == BrickLeftArray[b] - 100)*/
  clearInterval(IntervalMoveRight);
  flagfortherunning = false;
}

notes** clear interval move right clears the interval of the character from moving. flagfortheruning is a flag that if it's true the moving function is on.

if there's a need to load more code please say.

Inigo
  • 12,186
  • 5
  • 41
  • 70
meytal
  • 1
  • 3
  • Does this answer your question? [Can you tell if one element is touching another using JavaScript?](https://stackoverflow.com/questions/29916874/can-you-tell-if-one-element-is-touching-another-using-javascript) – ulou Dec 06 '21 at 15:36
  • sorry couldnt understand how it can help me – meytal Dec 06 '21 at 15:43
  • I strongly recommend using WebGL over plain JavaScript (https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API) – ControlAltDel Dec 06 '21 at 15:52
  • i looked into the linkes above , got an idea but i dont think i can write it .. can someone help me out with this please – meytal Dec 06 '21 at 16:59
  • still havnt got an answer , i looked into the linkes above and it's very different from what i need. whenever i run the function (that is shown in the link) there's an error – meytal Dec 07 '21 at 09:59
  • This should help: https://stackoverflow.com/questions/2440377/javascript-collision-detection/7301852#7301852 . You could check for collision after every step the player moves and if there is collision then undo the last step or just set the player position to be just right next to the object so that it is touching it with no overlap. – aerial Dec 12 '21 at 13:56

0 Answers0