In my pong game, I have a ball and two paddles. I want it so that when ball.ballRect.x < 5
is true
, score.greenScore
increments like so: score.greenScore++;
And that works well, but I also want it so that the ball moves back to the center of the screen.
So in Game1.cs I did this:
public void ScoreAdder()
{
if (ball.ballRect.X < 5)
{
score.blueScore++;
ball.ballRect = new Rectangle((int)400, (int)250, ball.ballTexture.Width, ball.ballTexture.Height);
}
}
It goes back to the center and adds the score, but now it won't listen to collisions.
In my Ball.cs I only draw the rectangle, like:
spriteBatch.Draw(ballTexture, ballRect, Color.White);
Because when I use Vector2
position, the ball won't even appear on the screen.
Have you reset the Position
when you respawn the ball to the center?
You could ever take advantage of properties with this, and have the Position
point to the Rectangle
or vice-versa.
public Vector2 BallPosition
{
get
{
return ballPosition;
}
set
{
ballRectangle.X = value.X;
ballRectangle.Y = value.Y;
ballPosition = value;
}
}
private Vector2 ballPosition
I'm not sure on how you handle collision and everything, but whenever you set the Position it will set the rectangle, you could also try the opposite, where you set the rectangle and it will sync with the position.