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;
}
}
I hope you understand!
UPDATE-----------------------------
I have found the solution, so thanks everyone!