Jump to content
  • Advertisement
Sign in to follow this  
DavitosanX

Sprite movement lag in XNA

This topic is 2297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm starting out in game programming and programming in general. I had been using C++/SFML for some time, but I decided to try C#/XNA and so far I'm happy with it. I'm using Rob Miles' book (Introduction to programming through game development), as I was kinda lost in C#. So far I think I understand XNA's base classes, and set out to make a small Pong-like game.

So far I've managed to draw the sprites on the screen, get the ball to move around and bounce around the edges of the viewport. Collision with the paddle is still not perfect, but it's functional enough for now.

As an exercise, I tried to add a feature: If you press Space, the paddle becomes "sticky" and you can move the ball along with the paddle, and releasing Space releases the ball as well. It almost works as expected, except that the ball seems to lag a little behind the paddle, and only catches up when the paddle stops moving.

I'm trying to figure out just where this is happening, but I haven't spotted it yet. Any help would be appreciated. Here is the relevant code:



public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Texture2D barra;
Rectangle barraRect;

Texture2D bola;
Rectangle bolaRect;

int barraYpos;

int bolaX;
int bolaY;

bool bolaHaciaArriba;
bool bolaHaciaAdelante;
bool bolaCanMove;

public void MoverBola()
{
if (bolaHaciaArriba && bolaCanMove)
{
bolaY -=3 ;
}
else if (!bolaHaciaArriba && bolaCanMove)
{
bolaY += 3;
}

if (bolaHaciaAdelante && bolaCanMove)
{
bolaX +=3;
}
else if (!bolaHaciaAdelante && bolaCanMove)
{
bolaX -= 3;
}
}

public void DeterminarDireccionDeLaBola()
{
if (bolaX <= 0)
{
bolaHaciaAdelante = true;
}
else if (bolaX >= 780)
{
bolaHaciaAdelante = false;
}

if (bolaY <= 0)
{
bolaHaciaArriba = false;
}
else if (bolaY >= 460)
{
bolaHaciaArriba = true;
}
}

public void HandleInput()
{
KeyboardState kboard = Keyboard.GetState();

if (kboard.IsKeyDown(Keys.Escape))
{
this.Exit();
}

if (kboard.IsKeyDown(Keys.Down) && barraYpos < 350 && bolaCanMove)
{
barraYpos += 7;
}
else if (kboard.IsKeyDown(Keys.Up) && barraYpos > 10 && bolaCanMove)
{
barraYpos -= 7;
}

if (kboard.IsKeyDown(Keys.Down) && barraYpos < 350 && !bolaCanMove)
{
barraYpos += 7;
bolaY += 7;
}
else if (kboard.IsKeyDown(Keys.Up) && barraYpos > 10 && !bolaCanMove)
{
barraYpos -= 7;
bolaY -= 7;
}

if (kboard.IsKeyDown(Keys.Space) && barraRect.Intersects(bolaRect))
{
bolaCanMove = false;
}
else if (kboard.IsKeyUp(Keys.Space))
{
bolaCanMove = true;
}
}

public void HandleCollision()
{
if (barraRect.Intersects(bolaRect))
{
bolaHaciaAdelante = true;

if ((bolaY+20) < (barraYpos + 3))
{
bolaHaciaArriba = true;
}

if (bolaY > (barraYpos+97))
{
bolaHaciaArriba = false;
}
}
}

protected override void Initialize()
{
barraYpos = 10;
bolaX = 300;
bolaY = 200;
bolaCanMove = true;
barraRect = new Rectangle(10, barraYpos, 35, 110);
bolaRect = new Rectangle(bolaX,bolaY,20,20);
base.Initialize();
}

protected override void Update(GameTime gameTime)
{
bolaRect = new Rectangle(bolaX, bolaY, 20, 20);
DeterminarDireccionDeLaBola();
MoverBola();
HandleInput();
HandleCollision();

barraRect = new Rectangle(10, barraYpos, 35, 110);


// TODO: Add your update logic here

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(barra, barraRect, Color.White);
spriteBatch.Draw(bola, bolaRect, Color.White);
spriteBatch.End();

// TODO: Add your drawing code here

base.Draw(gameTime);
}

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if it's your problem, but it seems that in your Update() function, you initialize the bolaRect before updating its position.

Share this post


Link to post
Share on other sites

I'm not sure if it's your problem, but it seems that in your Update() function, you initialize the bolaRect before updating its position.


Bingo! Update should be either:


protected override void Update(GameTime gameTime)
{
DeterminarDireccionDeLaBola();
MoverBola();
HandleInput();
HandleCollision();

barraRect = new Rectangle(10, barraYpos, 35, 110);
bolaRect = new Rectangle(bolaX, bolaY, 20, 20);


// TODO: Add your update logic here

base.Update(gameTime);
}



or:


protected override void Update(GameTime gameTime)
{
barraRect = new Rectangle(10, barraYpos, 35, 110);
bolaRect = new Rectangle(bolaX, bolaY, 20, 20);

DeterminarDireccionDeLaBola();
MoverBola();
HandleInput();
HandleCollision();



// TODO: Add your update logic here

base.Update(gameTime);
}



The problem was that I wasn't Updating the value of bolaRect and of barraRect in synchrony.

Thanks a lot, Faelenor biggrin.png Edited by DavitosanX

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!