0
votes

So I'm pretty new to XNA and C# in general but I'm running into a problem here. I'm just making this very basic "soccer game" and I have a collision event that when the player runs into the ball, the ball should stay in front of the player while they move around and what not.

But as soon as I run into the rectangle, the ball moves a little bit, but then the rectangle stays where it was and doesn't follow the ball..

Here's what I have..

Vector2 soccerBallPosition = new Vector2(0,0);

Update()

soccerBallBounds = new Rectangle(588, 338, 24, 24);

if (blueTeamCenter.blueTeamCenterBounds.Intersects(soccerBallBounds))
        {
            soccerBallPosition = new Vector2(blueTeamCenter.BTCmDirection.X + 32, blueTeamCenter.BTCmDirection.Y);
        }


Draw()

spriteBatch.Draw(soccerBall, soccerBallBounds, null, Color.White, 0, soccerBallPosition, SpriteEffects.None, 0);
1

1 Answers

0
votes

It may be that the snippet is incomplete but I can't pinpoint where the soccerBallBounds move to the new position.

Yous should initialize soccerBallBounds as soccerBallBounds = new Rectangle(0, 0, 24, 24);

Before checking for collisions call soccerBallBounds.Offset((int)soccerBallPosition.x,(int)soccerBallPosition.y ). This way your soccerBallBounds will be always updated with the latest ball position.

Check Rectangle.Offset for more information.