javascript - Stop Movement on Collision -
i'm creating little game in javascript , have created collision detection function detects when 2 images collide. have player image , enemy image , move player around arrow keys. when collide enemy image want player not able cross on image still able move, colliding wall or something. don't know how go cant supply , example code can give collide function , player objects;
//player objects
var playerimg = new image(); playerimg.src = "../images/player.png"; var playerready = false; playerimg.onload = function(){ playerready = true; }; var player = { x: 300, y: 150, speed: 200 };
//collide function
function collisioncheck(img1, img2, obj1, obj2, width){ var colliding = false; if(obj1.x < obj2.x + width && obj1.x + width > obj2.x && obj1.y < obj2.y + width && obj1.y + width > obj2.y){ colliding = true; }else{ colliding = false; } return colliding; }
maybe detect side collision on , stop player moving towards image whilst colliding?
i call function with:
if(collisioncheck(player, enemy, 32)){ }
function checktouching(img1, img2) { // checks if 2 images touching @ coordinate // given 2 objects x, y, width, , height var touching = img1.x + img1.width >= img2.x && img1.x <= img1.x + img1.width && img1.height + img1.y >= img2.y && img2.y <= img2.y + img2.y; if (touching) { // images touching } else { // images aren't touching } }
Comments
Post a Comment