How to do collision with an array

im trying to do collision detection for each block in the array i have a variable called canUp which changes the objectAbove variable if a player is below a block to false and if there isn’t a block above the player which will make it true, i have 2 blocks in my array but when i do console.log(canUp); it shows that canUp is also true and false at the same time each frame if you guys have any idea how to fix this here is my code:

<canvas id="ctx" width="572" height="572" style="border:1px solid #000000;"></canvas>

<script>
    var ctx = document.getElementById("ctx").getContext("2d"); 
ctx.font = '30px Arial';
    var HEIGHT = 572; 
    var WIDTH = 572; 
	objectAbove = false;
	objectBelow = false;
	objectLeft = false;
	objectRight = false;
	tile = 64;

blockList = {}

function entityDraw(entity){
ctx.fillStyle = entity.color;
ctx.fillRect(entity.x,entity.y,entity.width,entity.height);
}
//var test = new Array(); <-- temp
//test[0] = new obj(64,128,64,64,'white'); <-- temp
//block = function(x,y,width,height,id){
block = function(x,y,width,height,id) {
var block = {
x:x,
y:y,
width:width,
height:height,
id:id,
color:'white',
}
blockList[id] = block;
}
generateBlock = function(x,y,height,width){
var x = x;
var y = y;
var height = height;
var width = width;
var id = Math.random();
block(x,y,height,width,id);


}
//blocks[id] = block;
//}
/*generateBlock = function(){
var x = 64;
var y = 64;
var height = 64;
var width = 64;
var id = Math.random();
block(x,y,height,width,id);
}
*/
var player = {
x:256, 
y:256,
width:64,
height:64,
speed:2,
color:"blue",
pressingRight:false,
pressingUp:false,
pressingDown:false,
pressingLeft:false,
}

var background = {
x:0,
y:0,
width:640,
height:640,
color:"green",
}

//generateBlock(64,64,64,64,2);

playerInit(player);

generateBlock(128,64,64,64);
generateBlock(256,tile*3,64,64);

	document.onkeydown = function(key){
	if(key.keyCode == 87) //w
	player.pressingUp = true;
	
	else if(key.keyCode == 83)//s
	player.pressingDown = true;
	
	else if(key.keyCode == 65)//a
	player.pressingLeft = true;
	
	else if(key.keyCode == 68)//d
	player.pressingRight = true;
	}
	
	
	document.onkeyup = function(key){
	if(key.keyCode == 87)//w
	player.pressingUp = false;
	
	
	else if(key.keyCode == 83)//s
	player.pressingDown = false;
	
	else if(key.keyCode == 68)//d
	player.pressingRight = false;
	
	
	else if(key.keyCode == 65)//a
	player.pressingLeft = false;
	}
Update = function(){
entityDraw(background);
entityDraw(player);
for (var k in blockList) {
entityDraw(blockList[k]);
//var isAbove = checkUp(player,blockList[k]);
//console.log(isAbove);
//if (isAbove){
//console.log("above");
//}
var canUp = checkUp(player,blockList[k]);
console.log(canUp);
if (canUp == true){
objectAbove = false
}
else 
objectAbove = true;
}
playerUpdate(player);
}



function checkUp(player,entity){
if (player.y-64 == entity.y && player.x == entity.x) {
//objectAbove = true;
return false;
}
else {
return true;
//objectAbove = false;
}

}



function checkDown(){
if (player.y+64 == entity.y && player.x == entity.x) {
objectBelow = true;
}
else if(true) {
objectBelow = false;
}
}


function checkRight(){
if (player.x+64 == entity.x && player.y == entity.y) {
objectRight = true;
}
else if(true) {
objectRight = false;
}
}

function checkLeft(){
if (player.x-64 == entity.x && player.y == entity.Y) {
objectLeft = true;
}
else if(true) {
objectLeft = false;
}
}





function entityUpdate(entity){
entity.x = entity.x + entity.speed;
}
function playerInit(player){
target_y = player.y;
target_x = player.x;
moving = false;
}
function playerUpdate(player){

if (player.pressingUp == true && !moving && !objectAbove) {
target_y = target_y - 64
moving = true;
}
else if (player.pressingDown == true && !moving && !objectBelow) {
target_y = target_y + 64
moving = true;
}
else if (player.pressingRight == true && !moving && !objectRight) {
target_x = target_x + 64
moving = true;
}
else if (player.pressingLeft == true && !moving && !objectLeft) {
target_x = target_x - 64
moving = true;
}

if (target_y < player.y) { 
player.y = player.y - player.speed;
}
if (target_y > player.y) { 
player.y = player.y + player.speed;
}
if (target_x < player.x) { 
player.x = player.x - player.speed;
}
if (target_x > player.x) { 
player.x = player.x + player.speed;
}
if (target_x == player.x && target_y == player.y){
moving = false;
}
}
setInterval(Update,16);

 </script>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.