Creating a simple RPG game, first time using XNA.
Trying to get my character to face different ways whenever i move in different directions.
The problem is that when i start up i cant even see my texture, the weird thing is that when i walk over a specific position in the game the whole spritesheet becomes visible, same thing for my AI NPC:s.. It’s like they’re laying there behind the background and the rectangle i’m moving is transparant (so i can see through the background with it).
FrameWidth and FrameHeight is sent in when i create an instance of the class. (height = 0 (starting from the top of the spritesheet), width = spritesheetwidth / 4 (to get that single sprite out).)
Velocity is the speed of which the character moves.
public override void Update(GameTime gameTime)
{
ObjectRectangle = new Rectangle(CurrentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
Origin = new Vector2(ObjectRectangle.Width / 2, ObjectRectangle.Height / 2);
Position += Velocity;
ObjectRectangleX = (int)PositionX;
ObjectRectangleY = (int)PositionY;
#region movement
if (Keyboard.GetState().IsKeyDown(Keys.D) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.S))
{
AnimateRight(gameTime);
VelocityX = 2;
}
else if (Keyboard.GetState().IsKeyDown(Keys.A) && Pastkey.IsKeyUp(Keys.D) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.S))
{
AnimateLeft(gameTime);
VelocityX = -2;
}
else if (Keyboard.GetState().IsKeyDown(Keys.W) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.D) && Pastkey.IsKeyUp(Keys.S))
{
AnimateUp(gameTime);
VelocityY = -2;
}
else if (Keyboard.GetState().IsKeyDown(Keys.S) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.D))
{
AnimateDown(gameTime);
VelocityY = 2;
}
else
{
Velocity = Vector2.Zero;
}
Pastkey = Keyboard.GetState();
#endregion
}
#region Animations
public void AnimateDown(GameTime gameTime)
{
CurrentFrame = 2;
}
public void AnimateRight(GameTime gameTime)
{
CurrentFrame = 3;
}
public void AnimateLeft(GameTime gameTime)
{
CurrentFrame = 1;
}
public void AnimateUp(GameTime gameTime)
{
CurrentFrame = 0;
}
#endregion
}
Draw method looks like:
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(ObjectTexture, Position, ObjectRectangle, Color.White, 0f, Origin, 1.0f, SpriteEffects.None, 0f);
GameList.Add(this);
}
(Ignore the List.Add)
So what i need help with is “locking” the texture to the rectangle.
These lines look unneccessary
One thing you might look at is the final parameter in the .Draw call. You have it as 0f right now, that is the layer depth and if that value is lower than your terrain then the terrain will be drawn on top…I think, depends on your spritebatch.begin() arguments. You can read a bit more here where a few more bits are explained about layer depth.
https://gamedev.stackexchange.com/questions/23619/2d-layerdepth-not-working-xna-4-0