简体   繁体   中英

C# Collision, how to make the player not move if a boolean says he can't move right

So this is a game I am currently working on, and I got collisions to work and moving, but when I collide on either side of a wall (from the walls list) it stops moving my player, but if I keep holding down the key, the player will move completely through

This is the code for moving and collision:

    bool left = false;
    bool up = false;
    bool right = false;
    bool down = false;

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        #region Movement
        if (e.KeyCode == Keys.D && !right)
        {
            right = true;
        }

        if (e.KeyCode == Keys.A && !left)
        {
            left = true;
        }

        if (e.KeyCode == Keys.W)
        {
            up = true;
        }

        if (e.KeyCode == Keys.S)
        {
            down = true;
        }
        #endregion
    }

    public void Collision()
    {
        for (int x = 0; x < walls.Count; x++)
        {
            if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && right)
            {
                right = false;
            }

            if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && left)
            {
                left = false;
            }

            if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && up)
            {
                up = false;
            }

            if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && down)
            {
                down = false;
            }
        }
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.D)
        {
            right = false;
        }

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

        if (e.KeyCode == Keys.W)
        {
            up = false;
        }

        if (e.KeyCode == Keys.S)
        {
            down = false;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        #region True
        if (right)
        {
            Point loc = player.obj.Location;
            loc.X = loc.X + 4;
            player.obj.Location = loc;
        }

        if (left)
        {
            Point loc = player.obj.Location;
            loc.X = loc.X - 4;
            player.obj.Location = loc;
        }

        if (up)
        {
            Point loc = player.obj.Location;
            loc.Y = loc.Y - 4;
            player.obj.Location = loc;
        }

        if (down)
        {
            Point loc = player.obj.Location;
            loc.Y = loc.Y + 4;
            player.obj.Location = loc;
        }
        #endregion

        #region  NotTrue
        if (!right)
        {
            Point loc = player.obj.Location;
            loc.X = loc.X + 0;
            player.obj.Location = loc;
        }

        if (!left)
        {
            Point loc = player.obj.Location;
            loc.X = loc.X - 0;
            player.obj.Location = loc;
        }

        if (!up)
        {
            Point loc = player.obj.Location;
            loc.Y = loc.Y - 0;
            player.obj.Location = loc;
        }

        if (!down)
        {
            Point loc = player.obj.Location;
            loc.Y = loc.Y + 0;
            player.obj.Location = loc;
        }
        #endregion

        Collision();
    }
}
}

Reminder: Everything works, but my player, after colliding with the object and stopping for a few seconds, will proceed to move through the wall.

You're going to need another boolean like cantMoveRight to prevent the player moving right when they hit a wall, eg:

if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && right)
{
    right = false;
    cantMoveRight = true;
    cantMoveLeft = false; //Assuming when they cant move right they must move left, perhaps check this in situations when the player can only move forward or backward
}

if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && left)
{
    left = false;
    cantMoveRight = false;
    cantMoveLeft = true;
}

Then in the KeyUp event check that:

if (e.KeyCode == Keys.D && !cantMoveRight)
{
    right = false;
}

if (e.KeyCode == Keys.A && !cantMoveLeft)
{
    left = false;
}

This way when the timer1_Tick event is triggered/fired the player wont move through the wall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM