javascript - JQuery Collision Detection -
not sure if i'm on right track here, figured should work:
function collides(a, b) { return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y; } function handlecollisions() { $('#player').foreach(function(player) { $('#powerup').foreach(function(powerup) { if(collides(player, powerup)) { $('#player').hide(); } }); }); } handlecollisions(); } basically have player running around game , object @ fix position. when player collides object should disappear. provide fiddle if needed figured there visibly wrong code. suggestions?
edit:
function collides(a, b) { return a.offset() < b.offset() + b.width() && a.offset() + a.width() > b.offset() && a.position() < b.position() + b.height() && a.position() + a.height() > b.position(); } function handlecollisions() { if (collides($('#player'), $('#powerup'))) { $('#player').append("<p>collided</p>"); // hide player oncollision } } handlecollisions(); } `
Comments
Post a Comment