I have been having some problems with my coding lately. I am trying to create a platform-based game and I have been having some troubles.
A friend of mine said that some update functions are out of order, so my collision is causing a jittery effect. It looks kind of weird so I am hoping that you guys could help me.
Here is the code for the update in game1.cs and player.cs
game1.cs -
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
player.update(gameTime);
if (f.rectangle.Intersects(player.rectangle))
{
player.isOnSurface = true;
player.position.Y = f.rectangle.Top - player.texture.Height;
}
else player.isOnSurface = false;
f.update(gameTime);
base.Update(gameTime);
}
player.cs -
public void update(GameTime gameTime)
{
position = position + velocity;
// walking
if (Keyboard.GetState().IsKeyDown(Keys.A) | Keyboard.GetState().IsKeyDown(Keys.Left)) velocity.X = -4f;
else if (Keyboard.GetState().IsKeyDown(Keys.D) | Keyboard.GetState().IsKeyDown(Keys.Right)) velocity.X = 4f;
else velocity.X = 0;
// jumping
if (Keyboard.GetState().IsKeyDown(Keys.W) | Keyboard.GetState().IsKeyDown(Keys.Up) | Keyboard.GetState().IsKeyDown(Keys.Space) && isOnSurface == true)
{
isOnSurface = false;
position.Y -= 10;
velocity.Y = -5;
}
// gravity
rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
if (!isOnSurface)
{
float i = 1;
velocity.Y += 0.15f * i;
}
else
{
velocity.Y = 0f;
}
}
I would just like to know if my functions are in order, or how to get rid of the jittery effect (and therefore a good method for collision).