Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保持对象在picturebox C中移动#_C#_Game Physics_Picturebox_Bounds - Fatal编程技术网

C# 如何保持对象在picturebox C中移动#

C# 如何保持对象在picturebox C中移动#,c#,game-physics,picturebox,bounds,C#,Game Physics,Picturebox,Bounds,所以我有一个对象“player”,它也是一个picturebox。 它使用键左右移动。我希望它只能在图片框(屏幕)内移动,如果它接触到边缘就停止,而不能走出它 这是我的尝试 private void Game_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Right) { right = false; if (scr

所以我有一个对象“player”,它也是一个picturebox。 它使用键左右移动。我希望它只能在图片框(屏幕)内移动,如果它接触到边缘就停止,而不能走出它

这是我的尝试

        private void Game_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right)
        {
            right = false;

            if (screen.Bounds.IntersectsWith(player.Bounds))
            {
              ???

            }
        }
        if (e.KeyCode == Keys.Left)
        {
            left = false;

        }
    }

有人能帮忙吗。提前谢谢

如果我正确理解了这个问题,像这样的方法应该行得通

private void Game_KeyUp(object sender, KeyEventArgs e)
{            
    int leftChange = 0;
    if (e.KeyCode == Keys.Right)            
        leftChange = 10;     
    if (e.KeyCode == Keys.Left)            
        leftChange = -10;

    // check right bound
    if ((player.Right + leftChange) > screen.Right)
        leftChange = (screen.Right - player.Width) - player.Left;
    // check left bound
    if ((player.Left + leftChange) < screen.Left)
        leftChange = screen.Left - player.Left;

    // move the player
    player.Left += leftChange;
}
private void Game\u KeyUp(对象发送方,KeyEventArgs e)
{            
int leftChange=0;
if(e.KeyCode==Keys.Right)
leftChange=10;
if(e.KeyCode==Keys.Left)
leftChange=-10;
//检查右边界
如果((player.Right+leftChange)>screen.Right)
leftChange=(screen.Right-player.Width-player.Left);
//检查左边界
如果((player.Left+leftChange)