I am making a simple game. I managed to make my character move with arrow keys and fire. I added some walls to the map which are pictureboxes. I managed to detect the collision like this:
private void timer6_Tick(object sender, EventArgs e){
foreach (PictureBox pic in brick){
if (pic.Bounds.IntersectsWith(pb_sprite.Bounds)){
Console.WriteLine("Colision!");
switch (rotation){
case "up":
canup = false;
candown = true;
canleft = true;
canright = true;
break;
case "down":
canup = true;
candown = false;
canleft = true;
canright = true;
break;
case "left":
candown = true;
canup = true;
canleft = false;
canright = true;
break;
case "right":
candown = true;
canup = true;
canleft = true;
canright = false;
break;
}
}
else
{
Console.WriteLine("No colision?!");
}
}
}
It works as expected but soon as I hit the object (wall) I get "Colision!" echo which is ok, this also stops movement in the direction which the character was facing when the collision occurred. The thing is when I exit the collision one of these bools stays false which prevents my character to move again in the same direction. I tried setting all of these bools to true in else branch but since there are five walls even if there is collision else branch gets executed 4 times out of 5. I need some kind of "any" function to check if NO or ANY walls are being hit at one moment.
movement
function -assuming you have one- rather than inside yourTimer
and if you detect a collision, you do not allow that movement. – uTeisT