OK, so here's the problem. When i run the program if i control one sprite the other changes direction and vice verse. For example: player one moves left and player two turns left.
Another problem i had was that when i move my player two, when it gets to certain frames, my player one disappears but reappears after the frames begins to repeat. I personally think its because the player two sprite is much larger but i may be wrong. Please be descriptive if possible and give examples. Thanks in advance.
namespace PokemonMovement
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D pic;
Rectangle picRec, sourceRec;
Texture2D pic2;
Rectangle picRec2, sourceRec2;
Texture2D background;
Rectangle backgroundRec;
Texture2D Pokeball;
List<Rectangle> PokeballRecs;
const float speedFactor = 5;
float speed;
const int baseSpeed = 2;
float elapsed;
float delay = 200f;
int frames = 0;
int direction = 0;
Song PokemonRemix;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
this.graphics.PreferredBackBufferWidth = 800;
this.graphics.PreferredBackBufferHeight = 600;
Content.RootDirectory = "Content";
}
/// <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()
{
picRec = new Rectangle(100, 100, 64, 64);
picRec2 = new Rectangle(200, 200, 64, 64);
// 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);
pic = Content.Load<Texture2D>("PokemonSprite");
pic2 = Content.Load<Texture2D>("Knight2");
background = Content.Load<Texture2D>("Pokemon_Logo");
backgroundRec = new Rectangle(0, 0, 800, 600);
Pokeball = Content.Load<Texture2D>("PokeBall");
PokeballRecs = new List<Rectangle>();
Random random = new Random();
for (int i = 0; i < 50; i++)
{
PokeballRecs.Add(new Rectangle(random.Next(0, 600), random.Next(0, 600), Pokeball.Width / 4, Pokeball.Height / 4));
}
PokemonRemix = Content.Load<Song>("PokemonRemix");
MediaPlayer.Play(PokemonRemix);
// TODO: use this.Content to load your game content here
}
/// <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();
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
KeyboardState keys = Keyboard.GetState();
if (keys.IsKeyDown(Keys.Escape))
this.Exit();
GamePadState pad1 = GamePad.GetState(PlayerIndex.One);
GamePadState pad2 = GamePad.GetState(PlayerIndex.Two);
speed = baseSpeed + (pad1.Triggers.Right * speedFactor);
speed = baseSpeed + (pad2.Triggers.Right * speedFactor);
if (pad2.DPad.Up == ButtonState.Pressed || keys.IsKeyDown(Keys.W))
{
direction = 3;
if (elapsed >= delay)
{
if (frames >= 11)
{
frames = 1;
}
else
{
frames++;
}
elapsed = 0;
}
picRec2.Y--;
}
if (pad2.DPad.Down == ButtonState.Pressed || keys.IsKeyDown(Keys.S))
{
direction = 0;
if (elapsed >= delay)
{
if (frames >= 11)
{
frames = 1;
}
else
{
frames++;
}
elapsed = 0;
}
picRec2.Y++;
}
if (pad2.DPad.Left == ButtonState.Pressed || keys.IsKeyDown(Keys.A))
{
direction = 1;
if (elapsed >= delay)
{
if (frames >= 11)
{
{
frames = 1;
}
}
else
{
frames++;
}
elapsed = 0;
}
picRec2.X--;
}
if (pad2.DPad.Right ==ButtonState.Pressed || keys.IsKeyDown(Keys.D))
{
direction = 2;
if (elapsed >= delay)
{
if (frames >= 11)
{
frames = 1;
}
else
{
frames++;
}
elapsed = 0;
}
picRec2.X++;
}
if (pad1.DPad.Up == ButtonState.Pressed || keys.IsKeyDown(Keys.Up))
{
direction = 3;
if (elapsed >= delay)
{
if (frames >= 3)
{
frames = 1;
}
else
{
frames++;
}
elapsed = 0;
}
picRec.Y--;
}
if (pad1.DPad.Down == ButtonState.Pressed || keys.IsKeyDown(Keys.Down))
{
direction = 0;
if (elapsed >= delay)
{
if (frames >= 3)
{
frames = 1;
}
else
{
frames++;
}
elapsed = 0;
}
picRec.Y++;
}
if (pad1.DPad.Right == ButtonState.Pressed || keys.IsKeyDown(Keys.Right))
{
direction = 2;
if (elapsed >= delay)
{
if (frames >= 3)
{
frames = 1;
}
else
{
frames++;
}
elapsed = 0;
}
picRec.X++;
}
if (pad1.DPad.Left == ButtonState.Pressed || keys.IsKeyDown(Keys.Left))
{
direction = 1;
if (elapsed >= delay)
{
if (frames >= 3)
{
frames = 1;
}
else
{
frames ++;
}
elapsed = 0;
}
picRec.X --;
}
sourceRec = new Rectangle(64 *frames, 64 * direction, 64, 64);
for (int i = 0; i < PokeballRecs.Count(); i++)
if (picRec.Intersects(PokeballRecs[i]))
{
PokeballRecs.RemoveAt(i);
i--;
}
sourceRec2 = new Rectangle(64 * frames, 64 * direction, 64, 64);
for (int i = 0; i < PokeballRecs.Count(); i++)
if (picRec2.Intersects(PokeballRecs[i]))
{
PokeballRecs.RemoveAt(i);
i--;
}
base.Update(gameTime);
}
/// <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.White);
spriteBatch.Begin();
spriteBatch.Draw(background,backgroundRec, Color.White);
spriteBatch.Draw(pic, picRec, sourceRec, Color.White);
foreach (Rectangle rect in PokeballRecs)
spriteBatch.Draw(Pokeball, rect, Color.White);
spriteBatch.End();
spriteBatch.Begin();
spriteBatch.Draw(pic2, picRec2, sourceRec2, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}