开发者

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();     
      } 

}   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜