Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I started to develop a game for windows phone 7. This is the video of the game

So, I want to do a push-up. In the middle of the game you will be able to tap the screen and give an additional force to the ball. But the push up doesn't work when the ball is falling.

here is the code I did.

    public Ball(Vector2 pos, Texture2D image)
    {
        this.ballPos = pos;
        this.ballImage = image;
        this.ballSpeed = 0.0f;
        this.gravity = 0.2f;
        this.initialPos = ballPos.Y;
        this.jumping = false;
        this.force = 0.0f;
        this.boundingBox = new BoundingBox(new Vector3(this.ballPos.X, this.ballPos.Y, 0.0f),
            new Vector3(this.ballPos.X + this.ballImage.Width, this.ballPos.Y + this.ballImage.Height, 0.0f));
    }

    public void UpdateBall(paralax_manager floorBoundaries)
    {
        ballJump(floorBoundaries);
    }

    private void ballJump(paralax_manager floor)
    {
        boundingBox.Min = new Vector3(this.ballPos.X, this.ballPos.Y, 0.0f);
        boundingBox.Max = new Vector3(this.ballPos.X + this.ballImage.Width, this.ballPos.Y + this.ballImage.Height, 0.0f);


        if (this.jumping)
        {
            this.ballPos.Y += this.ballSpeed;
            this.ballSpeed += this.gravity;

            if (floor.getBounding().Intersects(this.boundingBox))
            {
                this.ballPos.Y = this.initialPos;
                this.force /= 1.2f;
                this.jumping = false;
            }
        }
        else
        {
            if (this.ballSpeed >= 0)
            {
                this.jumping = true;
                this.ballSpeed = this.force / 2;
            }
            else this.ballSpeed = 0;
        }
    }

    public void drawBall(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(ballImage, ballPos, Color.White);
    }

    public void pushUp()
    {
        if (jumping)
        {
            this.force -= 1.0f;
            this.ballSpeed -= 1.0f;
        }
        else
        {
            this.ballSpeed -= 30.0f;
        }
    }

I can't think how to make it work. May you help me.

PS: thanks for your help Gustavo! :D

--------- UPDATE BELLOW -----------

I'll try to wirte a better explanation.

The code bellow makes the ball jump. if 'this.jumping' is true I test the collision with the floor. When 'floor.getBounding().Intersects(this.boundingBox)' is true I change the ball position, I devide the force by 2 and 'this.jumping' become false. the 'else' makes the ball go up. the ball receives ths force I devided before.

private void ballJump(paralax_manager floor)
    {
        boundingBox.Min = new Vector3(this.ballPos.X, this.ballPos.Y, 0.0f);
        boundingBox.Max = new Vector3(this.ballPos.X + this.ballImage.Width, this.ballPos.Y + this.ballImage.Height, 0.0f);


        if (this.jumping)
        {
            this.ballPos.Y += this.ballSpeed;
            this.ballSpeed += this.gravity;

            if (floor.getBounding().Intersects(this.boundingBox))
            {
                this.ballPos.Y = this.initialPos;
                this.force /= 1.2f;
                this.jumping = false;
            }
        }
        else
        {
            if (this.ballSpeed >= 0)
            {
                this.jumping = true;
                this.ballSpeed = this.force / 2;
            }
            else this.ballSpeed = 0;
        }
    }

this code makes the push up.

When I tap the screen the ball will gain an additional force. The problem is when the ball is going up the push up works perfectly. But when the ball is falling it doesn't work, the ball does not go up, instead of that it stops on the mid air and fall on the floor.

public void pushUp()
{
    if (jumping)
    {
        this.force -= 1.0f;
        this.ballSpeed -= 1.0f;
    }
    else
    {
        this.ballSpeed -= 30.0f;
    }
}

This video shows the problem

I hope you understand!

UPDATE-----------------------------

I have found the solution, so thanks everyone!

share|improve this question
you probably want to comment this code, since it's not in english – dreta Mar 3 '12 at 21:59
I edited the question, so the code now is english and readable, just have to be peer revised. – Gustavo Maciel Mar 3 '12 at 22:09
@dyego I'm very tempted to answer, but i didnt get what doesnt work, and how it was supposed to work. Can you explain better? – Gustavo Maciel Mar 3 '12 at 23:05
I edit the post @Gustavo. I hope you understand. :D – Dyego Mar 5 '12 at 12:25
Where are you resetting jumping? Also -30.0 seems rather large. – Joshua Drake Apr 26 '12 at 12:32

closed as too localized by Tetrad Mar 8 at 0:11

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.