0

I was creating a little platforming game and i was stuck on the TOP and BOTTOM object collision for a block.

For some reason, the player would not stop falling when it overlapped the block. it would just reset jump to false and put force to 0, but not keep the player on top of the block.

Here is the code which i have done so far...

public partial class Form1 : Form
{
    bool right;
    bool left;
    bool jump;
    bool jumped;
    int G = 20;
    int force;
    bool nothing;
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        label1.Text = "Bottom: " + player.Bottom + " Top: " + player.Top + " Left: " + player.Left + " Right: " + player.Right;

       //Object Side Collision
        if (player.Right > block.Left && player.Left < block.Right - player.Width / 2 && player.Bottom > block.Top && player.Top < block.Bottom)
        {
            right = false;
        }
        if (player.Left < block.Right && player.Right > block.Left + player.Width / 2 && player.Bottom > block.Top && player.Top < block.Bottom)
        {
            left = false;
        }

        //Jumping and Gravity
        if (right == true)
            player.Left += 4;
        if (left == true)
            player.Left -= 4;

        if (jump == true)
            player.Top -= force; force -= 1;

        if (player.Top + player.Height >= screen.Height)
        {
            player.Top = screen.Height - player.Height;
            jump = false;
        }
        else
        {
            player.Top += 5;
            jumped = false;
            nothing = false;
        }

        //Top and Bottom Object Collision

        if (player.Left + player.Width - 1 > block.Left && player.Left + player.Width + 5 < block.Left + block.Width + player.Width && player.Top + player.Height >= block.Top && player.Top < block.Top)
        {
            player.Top = screen.Height - block.Height - player.Height;
            force = 0;
            jump = false;
        }

    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {




        if (e.KeyCode == Keys.Right)
            right = true;
        if (e.KeyCode == Keys.Left)
            left = true;
        if (e.KeyCode == Keys.Escape)
            this.Close();

        if (jump != true)
            if (e.KeyCode == Keys.Up)
            {
                if (jumped == true)
                { nothing = true; }
                else
                {if (player.Left + player.Width - 1 > block.Left && player.Left + player.Width + 5 < block.Left + block.Width + player.Width && player.Top + player.Height >= block.Top && player.Top < block.Top)
                    jumped = true;
                jump = true; force = G;}
            }


    }

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

    private void screen_Paint(object sender, PaintEventArgs e)
    {

    }
}

I have tried to figure out the reason why it would not make the player stop when it hit the top of the block for about an hour (trying different code) and nothing i have tried work.

1
  • 1
    (trying different code) - why not use the debugger and find out why versus trial and error? Commented May 9, 2015 at 21:34

1 Answer 1

1

First I want to offer a few tips.


I'd suggest focusing a bit more on your logic, improving comments or breaking some of the conditions out into helper methods.

A helper method for if 2 controls are colliding might be helpful.

Make use of the Right, Left, Bottom, Top properties instead of calculating them yourself:

player.Left + player.Width == player.Right

Also consider using curly braces around all your conditional statements.

1 issue I see is this:

if (jump == true)
    player.Top -= force; force -= 1;

In this case force -= 1; will always execute, the if statement is only applying to the next statement (up until the next semi-colon, not line).


Use the debugger - if you haven't before maybe search around on how to use it. It will help you figure out (debug) your problems :)


Given those tips, here's my understanding of your problem. You are letting the player move down regardless of if they collide with a block. You need to add the block collision condition into the previous if/else statements.

// if the player is at the bottom of the screen
if (player.Bottom >= screen.Bottom) 
{
    jump = false;
}
// if the player is hitting a block
else if (player.Right >= block.Left &&
    player.Left <= block.Right &&
    player.Bottom >= block.Top &&
    player.Top <= block.Bottom)
{
    force = 0;
    jump = false;
}
else
{
    player.Top += 5;
    jumped = false;
    nothing = false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the help. And i never used the debugger before, it never came into my mind! :)
i have a slight problem with the code, whenever player.Left touches block.Right, it teleports player ontop of block, and when player is ontop of block it does not allow player to jump again. Any help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.