How can I add a character and enemies to a game that uses Parallax Scrolling? [on hold]
- by Homer_Simpson
I use the following code to create Parallax Scrolling:
http://www.david-gouveia.com/portfolio/2d-camera-with-parallax-scrolling-in-xna/
Parallax Scrolling is working but I don't know how to add the player and the enemies. I tried to add a player class to the existing code, but if the player moves, then the camera isn't pointing at the player. The player leaves the camera viewport after a few seconds.
I use the following code(as described in the tutorial), but it's not working:
// Updates my camera to lock on the character
_camera.LookAt(player.Playerposition);
What can I do so that the player is always the center of the camera?
How should I add the character and the enemies to the game? Should I create a layer for the character and the enemies? For example:
new Layer(_camera) { Parallax = new Vector2(0.9f, 1.0f) }
At the moment, I don't use a layer for the player and I don't have implemented the enemies because I don't know how to do that.
My player class:
public class Player
{
Texture2D Playertex;
public Vector2 Playerposition = new Vector2(400, 240);
private Game1 game1;
public Player(Game1 game)
{
game1 = game;
}
public void Load(ContentManager content)
{
Playertex = content.Load<Texture2D>("8bitmario");
TouchPanel.EnabledGestures = GestureType.HorizontalDrag;
}
public void Update(GameTime gameTime)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
switch (gs.GestureType)
{
case GestureType.HorizontalDrag:
Playerposition.X += 3f;
break;
}
}
}
public void Render(SpriteBatch batch)
{
batch.Draw(Playertex, new Vector2(Playerposition.X - Playertex.Width / 2, Playerposition.Y - Playertex.Height / 2), Color.White);
}
}
In Game1, I update the player and camera class:
protected override void Update(GameTime gameTime)
{
// Updates my character's position
player.Update(gameTime);
// Updates my camera to lock on the character
_camera.LookAt(player.Playerposition);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
foreach (Layer layer in _layers)
layer.Draw(spriteBatch);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, _camera.GetViewMatrix(new Vector2(0.0f, 0.0f)));
player.Render(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}