PictureBox Intesect
I'm working on a simple 2d game with pictureboxes but I´m struggling with collision detection.
I've been looking around and came up with this:
public bool ObstacleHit()
{
if (pbPlayer.Bounds.IntersectsWith(pbObstacle1.Bounds))
{
return false;
}
else
{
return true;
}
}
which is called here:
if (e.KeyChar == 'w'开发者_C百科)
{
ObstacleHit();
if(ObstacleHit() == true)
{
moveUp();
}
}
but this ain't working.
Hm, see if this works. For various key selection rather than the if-statement, you may as well implement the use of a switch-case statement.
if (e.KeyCode == Keys.W)
{
bool hit = ObstacleHit();
if(hit == true)
{
moveUp();
}
}
Use below code to check KeyChar
if (e.KeyChar == (char)Keys.W)
{
ObstacleHit(); // unnecessary call of method here
if(ObstacleHit()) // need not to compare a bool value
{
moveUp();
}
}
精彩评论