Home » Javascript » Box Detecting Collision with border but not resetting in a certain location?

Box Detecting Collision with border but not resetting in a certain location?

Posted by: admin November 1, 2017 Leave a comment

Questions:

I want the char to re spawn 10px on the X axis and 10px on the Y axis when the char hits the border of the screen but it is not working.
Here is the code i’m using:

       if($('#char').offset().top < 0 || $('#char').offset().top > window.innerHeight || $('#char').offset().left < 0 || $('#char').offset().left > window.innerWidth ){
     console.log('Off Screen');
     left = 10;
     var reLeft = {'left':left + "px"};
     $('#char').css(reLeft);
     top = 10;
     var reTop = {'top':top + "px"};
     $('#char').css(reTop);
     }

Note: If you want to look at the full code to see if I did something wrong there, you can go to My Site and press Ctrl + U

Answers:

i dont really know if it work because i cant test it. But this is how i would do it

$("#char").parent().css({position: 'relative'});
$("#char").css({top: 10, left: 10, position:'absolute'});