0

i m trying to make a webpage that looks like a game. I used html, css, javascript, jquery and php in it. I want my Mario to touch my other objects such as shops (which comes from databse) and a popup box should appear on touching shops. I am using collision function of javascript but its not working. can anybody help me with that. here is my code:

        function collision($store, $mario) {
        alert("hello");
        var x1 = $("$store").offset().left;
        var y1 = $("$store").offset().top;
        var x2 = $("$mario").offset().left;
        var y2 = $("$mario").offset().top;
        var h1 = $("$store").outerHeight(true);
        var w1 = $("$store").outerWidth(true);
        var h2 = $("$mario").outerHeight(true);
        var w2 = $("$mario").outerHeight(true);
        var b1 = y1 + h1;
        var r1 = x1 + w1;
            var b2 = y2 + h2;
            var r2 = x2 + w2;
            if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) {
              return false;}
            else{
              return true;
            }
      };
});  
var state =document.getElementById('state');
      if (collision($('.store'),$('.mario'))){
        state.innerText="Touching !!!";
      }  
      else {
        state.innerText="No collision";
      }
      alert(state.innerText);

    }
Shivali
  • 1
  • 1
  • Welcome to Stack Overflow! Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+div+collision+site:stackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Jun 15 '21 at 07:46
  • I assume you got the code from [this](https://stackoverflow.com/questions/5419134/how-to-detect-if-two-divs-touch-with-jquery) answer? Are you querying for the collision at an interval? – Ian H. Jun 15 '21 at 07:49
  • [Eloquent Javascript's "Platform Game"](https://eloquentjavascript.net/16_game.html) project page walks us through a (relatively) simple collision-detection technique. (Scroll about halfway down to find the **Motion and Collision** section.) – Cat Jun 15 '21 at 08:11

0 Answers0