XNA Sprite doesn't seem to be updating? - c#

I've looked through some other questions and haven't found my answer so I'm asking you lovely people as you have helped me before :)
I have a main class (RPG.cs) and a player class (Player.cs) and I want the Player class to be as self-contained as possible (because I don't want a huuuge main class) but here is my problem.
Problem
My Player sprite draws fine but when I want it to move, it won't update! At the moment I'm attempting to make it act like a cursor to test movement but eventually I'll have it bound to WASD or the arrow keys. So I want my sprite to follow my mouse at the moment but it just stays at 50, 50 (it's preset starting position) Have I missed something obvious? I'm new to XNA and I've spent half an hour on this problem!
RPG.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TestGame
{
public class RPG : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D PlayerTex;
Rectangle playerPos = new Rectangle(
Convert.ToInt32(Player.Pos.X),
Convert.ToInt32(Player.Pos.Y), 32, 32);
public RPG()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize() { base.Initialize(); }
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
PlayerTex = Content.Load<Texture2D>("testChar");
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime)
{
if
(
GamePad.GetState(PlayerIndex.One)
.Buttons.Back == ButtonState.Pressed
)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
spriteBatch.Draw(PlayerTex, playerPos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TestGame
{
/// This is a game component that implements IUpdateable.
public class Player : Microsoft.Xna.Framework.GameComponent
{
public static Vector2 Pos = new Vector2(50, 50);
MouseState ms = Mouse.GetState();
public Player(Game game) : base(game) { }
/// Allows the game component to perform
/// any initialization it needs to before
/// starting to run. This is where it can
/// query for any required services and load content.
public override void Initialize() { base.Initialize(); }
/// Allows the game component to update itself.
public override void Update(GameTime gameTime)
{
Pos.X = ms.X;
Pos.Y = ms.Y;
base.Update(gameTime);
}
}
}
Hope you can help! :)

As mentioned in the comment, I am not going to solve every problem. But the main errors.
Let the player be responsible for its position, not the game. Furthermore, I would make the player responsible for drawing itself, but that goes a bit too far for this answer.
The following code should at least work.
public class Player : Microsoft.Xna.Framework.GameComponent
{
public Vector2 Pos { get; set; }
public Player(Game game) : base(game)
{
this.Pos = new Vector2(50, 50);
}
public override void Initialize() { base.Initialize(); }
public override void Update(GameTime gameTime)
{
var ms = Mouse.GetState();
Pos.X = ms.X;
Pos.Y = ms.Y;
base.Update(gameTime);
}
}
Then use the player in your game:
public class RPG : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D PlayerTex;
Player player;
public RPG()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
player = new Player(this);
Components.Add(player);
}
protected override void Initialize() { base.Initialize(); }
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
PlayerTex = Content.Load<Texture2D>("testChar");
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime)
{
if
(
GamePad.GetState(PlayerIndex.One)
.Buttons.Back == ButtonState.Pressed
)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
spriteBatch.Draw(PlayerTex, player.Pos, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
I have removed the texture's size. Don't know if you really need this. If so, you can let Player expose a Rectangle, not just a Vector2.

The key solution which Nico included is just that you using the original rectangle coordinates you made when you draw:
Rectangle playerPos = new Rectangle(
Convert.ToInt32(Player.Pos.X),
Convert.ToInt32(Player.Pos.Y), 32, 32);
Here you make the rectangle using the players CURRENT starting time position.
Then you ALWAYS draw based on this rectangle you made ages ago and never updated:
spriteBatch.Draw(PlayerTex, playerPos, Color.White);
The right way as Nico mentioned is just to change the draw to this:
playerPos = new Rectangle(
Convert.ToInt32(Player.Pos.X),
Convert.ToInt32(Player.Pos.Y), 32, 32);
spriteBatch.Draw(PlayerTex, playerPos, Color.White);
Now you will be drawing at a new place every time. There are better ways to do this (like what nico did) but here is the core idea.

Related

How to move my sprite in MonoGame using C#

First off, i'm fairly new to programming, about 2 months in, i dabbled a bit in Console Application, WinForms, and still using the ugly console to get better at algorithms in general. But now, I wanna start digging into game programming, because that's the reason i wanted to learn programming. I stumbled upon MonoGame, and while it's harder than say Unity, I got an immense sense of achievement after creating something by just using code. I already made a Space Invaders and Pong but nothing related to sprite animation, using spritesheets and moving a player. So 2 days ago, I started a platformer, divided my spritesheet, got some animation down, and now that it's time to move my player, I'm completely lost. I tried reading some tutorials on vectors, but it doesn't help in my case. Maybe you can shed some light on the matter.
So, without further ado, here's the code:
Game.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace MafiaJohnny
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private JohnnyPlayer johnnyPlayer;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D texture = Content.Load<Texture2D>("JohnnyDone");
johnnyPlayer = new JohnnyPlayer(texture, 2, 4);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
johnnyPlayer.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
johnnyPlayer.Draw(spriteBatch, new Vector2(200, 200));
base.Draw(gameTime);
}
}
}
JohnnyPlayer.cs
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
namespace MafiaJohnny
{
class JohnnyPlayer
{
public Texture2D Texture { get; set; }
public int Rows { get; set; }
public int Columns { get; set; }
private int currentFrame;
private int totalFrames;
//Slow down frame animation
private int timeSinceLastFrame = 0;
private int millisecondsPerFrame = 400;
public JohnnyPlayer(Texture2D texture, int rows, int columns)
{
Texture = texture;
Rows = rows;
Columns = columns;
currentFrame = 0;
totalFrames = Rows * Columns;
}
public void Update (GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
KeyboardState keystate = Keyboard.GetState();
//Idle animation
if (keystate.GetPressedKeys().Length == 0)
currentFrame++;
timeSinceLastFrame = 0;
if (currentFrame == 2)
currentFrame = 0;
//Walking Animation
if (keystate.IsKeyDown(Keys.Left))
{
}
}
}
public void Draw (SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width/Columns;
int height = Texture.Height / Rows;
int row = (int) ((float) currentFrame/Columns);
int column = currentFrame % Columns;
Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);
spriteBatch.Begin();
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
spriteBatch.End();
}
}
}
So here's my code, find me an answer minions! Thank you is what I mean :)
You just need to change "location" so the sprite moves left/right/up/down. Also I recommend moving this code from JohnnyPlayer to another "controller" class.
Here:
http://www.gamefromscratch.com/post/2015/06/15/MonoGame-Tutorial-Creating-an-Application.aspx
They make a sprite and move it from left to right. In your case the texture on sprite changes in time (animation) but the movement is still the same.

xna Loading content from a non GameComponent class

I am a relative beginner in XNA and am trying to create a screenManager for an C# XNA game. Everything in my code seems to work fine until I reach the LoadContent method in my MainMenu class which derives from Screen which is a custom standalone class. This is where I get my GraphicsDevice component not found ContentLoad exception.
This is on the the statement: texture = c.Load<Texture2D>("Textures/Start");
where c is a ContentManager.
This statement and the program works okay if I put it in the Update method of my class. However, I only want to load this texture once, not with each call to Update(GameTime). I think I must be calling load before some Content info is being initialized but I'm not sure what. There are some commented out statements in MainMenu.LoadContent that do pretty much the same thing and all produce the same error. I'm posting my entire code here. Hope it isn't flooding the forum.
Here's Game1.cs:
namespace RoomGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
public static GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameController gameController;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
gameController = new GameController(this,this.Content);
this.Components.Add(gameController);
}
}
}
GameController.cs:
namespace RoomGame
{
public class GameController : DrawableGameComponent
{
public Game game;
public ContentManager content;
public ScreenController screenController;
public MainMenu menu;
public GameController(Game game,ContentManager c)
: base(game)
{
this.game = game;
content = c;
screenController = new ScreenController(this);
menu = new MainMenu(this,this.content);
screenController.AddScreen(menu);
}
public override void Initialize()
{
base.Initialize();
screenController.Initialize();
}
protected override void LoadContent()
{
base.LoadContent();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
public override void Update(GameTime gameTime)
{
screenController.Update(gameTime);
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
screenController.Draw(gameTime);
}
}
}
screenController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RoomGame
{
public class ScreenController
{
public List<Screen> screens = new List<Screen>();
GameController gameController;
SpriteBatch spriteBatch;
public ScreenController(GameController gc)
{
this.gameController = gc;
}
public void Initialize()
{
spriteBatch = new SpriteBatch(this.gameController.GraphicsDevice);
}
public void Update(GameTime gameTime)
{
foreach (Screen screen in screens)
{
if (screen.isUpdating)
screen.Update(gameTime);
}
}
public void Draw(GameTime gameTime)
{
foreach (Screen screen in screens)
{
if (screen.isDrawn)
{
spriteBatch.Begin();
screen.Draw(gameTime,spriteBatch);
spriteBatch.End();
}
}
}
public void AddScreen(Screen screen)
{
if (!screens.Contains(screen))
{
screen.LoadContent(this.gameController.game.Content);
screens.Add(screen);
//screen.Initialize();
}
}
}
}
Screen.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace RoomGame
{
public class Screen
{
public ScreenController screenController;
public bool isUpdating=true;
public bool isDrawn=true;
public GraphicsDeviceManager graphics;
/*public Screen(ScreenController sc)
{
this.screenController = sc;
this.isActive = true;
}*/
public virtual void Initialize()
{ }
public virtual void LoadContent(ContentManager c)
{ }
public virtual void Update(GameTime gameTime)
{ }
public virtual void Draw(GameTime gameTime,SpriteBatch sb)
{ }
}
}
MainMenu.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace RoomGame
{
public class MainMenu : Screen
{
GameController gameController;
Texture2D texture;
Vector2 position;
ContentManager content;
//public GraphicsDeviceManager graphics;
public MainMenu(GameController gc,ContentManager c)
{
this.gameController = gc;
this.content = c;
this.isUpdating = true;
this.isDrawn = true;
//this.graphics = this.gameController.game.graphics;
}
public override void LoadContent(ContentManager c)
{
texture = c.Load<Texture2D>("Textures/Start");
//texture = this.gameController.game.Content.Load<Texture2D>("Textures/Start");
//texture=this.gameController.content.Load<Texture2D>("Textures/Start");
//texture = this.gameController.Game.Content.Load<Texture2D>("Textures/Start");
}
public override void Update(GameTime gameTime)
{
//texture = this.gameController.content.Load<Texture2D>("Textures/Start");
position = new Vector2(100, 100);
}
public override void Draw(GameTime gameTime,SpriteBatch sb)
{
sb.Draw(texture, position, Color.White);
}
}
}
Thanks in advance....
EDIT
I found my error. I was calling ScreenController.Add before the LoadContent in GameController. I put it after the base.LoadContent in gameController and it works now. I hope this can help somebody but if anyone know a better way of approaching this please let me know.
You call MainMenu.LoadContent from the constructor of the ScreenController which in turn is called by the Game's constructor. LoadContent should be called at the appropriate time. So move the call to AddScreen to the LoadContent method of the GameController.

2D top down collision detection?

I have been trying to create a collision for a "top down view" like game. I have made a player class and a block class, in my Game1 class I have checked for the collision, don't know if this is a right way, but it's only a test right now. So if I intersect them I don't know what to do next.
This is my Game1.cs
using System; using System.Collections.Generic; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Collision_Testing {
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player = new Player(100, 100);
Block block = new Block(500, 500);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
block.LoadContent(Content);
player.LoadContent(Content);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
block.Update(gameTime);
player.Update(gameTime);
base.Update(gameTime);
if (player.boundingBox.Intersects(block.boundingBox))
{
player.playerPos.X -= player.speed;
player.playerPos.Y -= player.speed;
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
player.Draw(spriteBatch);
block.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
} }
This is my Player.cs
using System; using System.Collections.Generic; using System.Linq;
using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;
namespace Collision_Testing {
class Player
{
public Vector2 playerPos;
public Texture2D playerTex;
public int speed;
KeyboardState oldState;
public Rectangle boundingBox;
public Player(int positionX, int positionY)
{
playerPos.X = positionX;
playerPos.Y = positionY;
speed = 14;
}
public void LoadContent(ContentManager Content)
{
playerTex = Content.Load<Texture2D>("player");
boundingBox = new Rectangle((int)playerPos.X, (int)playerPos.Y, playerTex.Width, playerTex.Height);
}
public void Update(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();
if(newState.IsKeyDown(Keys.Right))
{
playerPos.X += speed;
}
if (newState.IsKeyDown(Keys.Left))
{
playerPos.X -= speed;
}
if (newState.IsKeyDown(Keys.Up))
{
playerPos.Y -= speed;
}
if (newState.IsKeyDown(Keys.Down))
{
playerPos.Y += speed;
}
oldState = newState;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(playerTex, playerPos, Color.White);
}
} }
This is my Block.cs
using System; using System.Collections.Generic; using System.Linq;
using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;
namespace Collision_Testing {
class Block
{
public Vector2 blockPos;
public Texture2D blockTex;
public Rectangle boundingBox;
public Block(int blockX, int blockY)
{
blockPos.X = blockX;
blockPos.Y = blockY;
}
public void LoadContent(ContentManager Content)
{
blockTex = Content.Load<Texture2D>("wall");
boundingBox = new Rectangle((int)blockPos.X, (int)blockPos.Y, blockTex.Width, blockTex.Height);
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(blockTex, blockPos, Color.White);
}
} }
I think I've done quite a good job for a beginner but don't know what to do next.
It looks to me like speed is a scalar. so when you intersect with the box,
if (player.boundingBox.Intersects(block.boundingBox))
{
player.playerPos.X -= player.speed;
player.playerPos.Y -= player.speed;
}
you're merely moving the player 14 units to the left, and 14 units up, regardless of where the player was.
It looks like you're intending to subtract the velocity. In order to do that, you first have to declare a velocity
public Vector2 playerVelocity;
make sure you initialize playerVelocity in your constructor.
and then you should modify the velocity instead of the position when measuring input
if(newState.IsKeyDown(Keys.Right))
{
playerVelocity.X = speed;
}
else if (newState.IsKeyDown(Keys.Left))
{
playerVelocity.X = -speed;
}
else
{
playerVelocity.X = 0;
}
...
playerPos += playerVelocity;
// i believe you can just add Vectors in XNA like this
// but if it makes you feel better you can do playerPos.X += playerVelocity.X
and then you can reverse your player's position when you collide
if (player.boundingBox.Intersects(block.boundingBox))
{
player.playerPos -= player.playerVelocity;
}
If your character doesn't stop moving I think you miss something like this:
if (newState.IsKeyUp(Keys.Up) && newState.IsKeyUp(Keys.Down) &&
newState.IsKeyUp(Keys.Left) && newState.IsKeyUp(Keys.Right))
{
playerVelocity = Vector2.Zero;
}

Severe Movement Lag - C#, XNA

I'm very new to programming C#, and well, programming anything. This is my third 2D game, I've encountered a problem. For some reason, the game gets laggy extremely fast when I'm attempting to move my sprite, and I can't figure out why.
Here's the Sheep class:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace IdeaLess
{
class Sheep
{
//-//-//-//-//-//
public Texture2D SheepTexture;
public const float SheepSpeed = 0.5f;
public Vector2 SheepPosition;
public int Width
{
get { return SheepTexture.Width; }
}
public int Height
{
get { return SheepTexture.Height; }
}
//-//-//-//-//-//
public void Initialize(Texture2D texture, Vector2 position)
{
SheepTexture = texture;
SheepPosition = position;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(SheepTexture, SheepPosition, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
}
}
}
And the Game1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace IdeaLess
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//CLASS OBJECTS
Sheep sheep;
Pig pig;
//SHEEP
float SheepMoveSpeed;
//PIG
float PigMoveSpeed;
//KEYBOARDSTATES
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
sheep = new Sheep();
pig = new Pig();
SheepMoveSpeed = 5f;
PigMoveSpeed = 4f;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Vector2 sheepPosition = new Vector2(20, 30);
Vector2 pigPosition = new Vector2(20, 400);
sheep.Initialize(Content.Load<Texture2D>("Sheep"), sheepPosition);
pig.Initialize(Content.Load<Texture2D>("Pig"), pigPosition);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
UpdateSheep(gameTime);
UpdatePig(gameTime);
base.Update(gameTime);
}
private void UpdateSheep(GameTime gameTime)
{
if (currentKeyboardState.IsKeyDown(Keys.Left))
sheep.SheepPosition.X -= SheepMoveSpeed;
if (currentKeyboardState.IsKeyDown(Keys.Right))
sheep.SheepPosition.X += SheepMoveSpeed;
}
private void UpdatePig(GameTime gameTime)
{
if (currentKeyboardState.IsKeyDown(Keys.A))
pig.PigPosition.X -= PigMoveSpeed;
if (currentKeyboardState.IsKeyDown(Keys.D))
pig.PigPosition.X += PigMoveSpeed;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
sheep.Draw(spriteBatch);
pig.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
So far the game is made up of nothing but a player class (Sheep) and the Game1.cs. There is also a pig class; identical to the sheep one.
Basically, whenever I hold 'Right Arrow', the sprite moves jerky and unevenly, sometimes slowing down to almost stationary, and sometimes it moves normally for a moment before lagging again.
It's not just the sprite movement, but it's the FPS. I know this because I've encountered this lag on a previous game and it caused the background music to stop and the timer from ticking.
Any ideas what might be causing this?
Well good news, it looks like it is nothing major! A thing I always encourage new XNA programmers to do, is add Elapsed time! Basicly, depending on how fast your system is running at a given time, may effect how fast your sprite moves depending on how many frames per seconds you have. If you tried this on a different computer it may run at a completely different speed.
To correct this you need to modify your UpdateAnimal() Methods
private void UpdateSheep(GameTime gameTime)
{
//How much time has passed since the last frame, incase we lag and skip a frame, or take too long, we can process accordingly
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (currentKeyboardState.IsKeyDown(Keys.Left))
sheep.SheepPosition.X -= SheepMoveSpeed * elapsed; // Multiply by elapsed!
if (currentKeyboardState.IsKeyDown(Keys.Right))
sheep.SheepPosition.X += SheepMoveSpeed * elapsed;
}
Now, depending on your computer specs, elapsed will be a very small fraction of 1 (second) so you will need to increase your SheepMoveSpeed until your sprite starts moving.
If this dosen't work, you can try using a profiler to see what is causing lag, or add an fps timer to see if it is really "laggy" or just a matter of not moving properly.
I would also encourage you to create an Animal class, and create other classes that inherit from it.

XNA Respawning "enemies" randomly

I am trying to make a game and I want "enemies" or meteors to spawn from the right side and going to the left side. I am working on some code and it seems to be working pretty good but the respawning fails. They end up spawning one and one REALLY slow and then after a while they don't spawn at all. All help would be really appreciated.
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame2
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Enemies> enemies = new List<Enemies>();
Random random = new Random();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
float spawn = 0;
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
spawn += (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach (Enemies enemy in enemies)
{
enemy.Update(graphics.GraphicsDevice);
}
LoadEnemies();
base.Update(gameTime);
}
public void LoadEnemies()
{
int randY = random.Next(100, 400);
if (spawn > 1)
{
spawn = 0;
if (enemies.Count() < 10)
enemies.Add(new Enemies(Content.Load<Texture2D>("meteor"), new Vector2(1110, randY)));
}
for (int i = 0; i < enemies.Count; i++)
{
if (!enemies[i].isVisible)
{
enemies.RemoveAt(i);
i--;
}
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (Enemies enemy in enemies)
{
enemy.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
and here's my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace WindowsGame2
{
class Enemies
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity;
public bool isVisible = true;
Random random = new Random();
int randX, randY;
public Enemies(Texture2D newTexture, Vector2 newPosition)
{
texture = newTexture;
position = newPosition;
randX = random.Next(-4, 4);
randY = random.Next(-4, -1);
velocity = new Vector2(randX, randY);
}
public void Update(GraphicsDevice graphics)
{
position += velocity;
if (position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height)
{
velocity.Y = -velocity.Y;
}
if (position.X < 0 - texture.Width)
{
isVisible = false;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
}
Your problem lies here. Some of your meteors are moving right and some are not moving at all.
randX = random.Next(-4, 4);
Try using
randX = random.Next(-4, -1);
Your enemies/meteors sometimes had a velocity on the X that was between 0 and 4, so they moved right.
I can see you changed the Y to this, maybe you got them mixed up?

Categories