Jump to content

Recommended Posts

Hey, so I have this code and when apple falls from a tree, if it touches a person the screen should say true, if it doesn't touch a person it says false. But, nothing is happening. Please help. Here is my code:

 

<script>    function collision($div1, $div2) {      var x1 = $div1.offset().left;      var y1 = $div1.offset().top;      var h1 = $div1.outerHeight(true);      var w1 = $div1.outerWidth(true);      var b1 = y1 + h1;      var r1 = x1 + w1;      var x2 = $div2.offset().left;      var y2 = $div2.offset().top;      var h2 = $div2.outerHeight(true);      var w2 = $div2.outerWidth(true);      var b2 = y2 + h2;      var r2 = x2 + w2;              if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) return false;      return true;    }$(document).ready(function(){    window.setInterval(function() {        $('#result').text(collision($('#div1'), $('#div2')));    }, 200);});<p>Colliding? <span id="result">false</span>

 

P.S. I didn't include the entire code, just what is important to my question.

Link to post
Share on other sites

jQuery is not really optimized for videogames. You'll need to use plain Javascript.

 

Determine the position and size of all the sprites you want to interact with. The X and Y position can be found with the offsetLeft and offsetTop properties of the image, the width and height would be with offsetWidth and offsetHeight.

 

Once you know that, you can use them in a rectangle collision detection. You can learn about that here: https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...