I'm making a 2D platformer in XNA 4.0. I have created a working tile engine, which works well for my purposes, and Horizontal Scrolling works flawlessly, however I am having great trouble with Vertical scrolling.
I Basically want the camera to scroll up (world to scroll down) when the player reaches a certain Y co-ordinate, and I would also like to automatically scroll back down if coming down, and that co-ordinate is passed. My biggest problem is I have no real way of detecting the direction the player is moving in using only the Y Co-ord.
Here Is My Code Code For The Camera Class (which appears to be a very different approach to most camera classes I have seen).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace Marvin
{
class Camera : TileEngine
{
public static bool startReached;
public static bool endReached;
public static void MoveRight(float speed = 2)
{
//Moves The Position of Each Tile Right
foreach (Tile t in tiles)
{ if(t!=null) { t.position.X -= speed; } }
}
public static void MoveLeft(float speed = 2)
{
//Moves The Position of Each Tile Right
foreach (Tile t in tiles)
{ if(t!=null) { t.position.X += speed; } }
}
public static void MoveUp(float speed = 2)
{
foreach (Tile t in tiles)
{ if(t!=null) { t.position.Y += speed; } }
}
public static void MoveDown(float speed = 2)
{
foreach (Tile t in tiles)
{ if(t!=null) { t.position.Y -= speed; } }
}
public static void Restrain()
{
if(tiles.Last().position.X<Main.graphics.PreferredBackBufferWidth-tiles.Last().size.X)
{ MoveLeft(); endReached = true; }
else
{ endReached = false; }
if(tiles[1].position.X>0)
{ MoveRight(); startReached = true;}
else
{ startReached = false; }
}
}
}
Here is My Player Code for Left and Right Scrolling/Moving
if (Main.currentKeyState.IsKeyDown(Keys.Right))
{
Camera.MoveRight();
if(Camera.endReached)
{ MoveRight(2); }
else
{
if(marvin.GetRectangle().X!=Main.graphics.PreferredBackBufferWidth-(marvin.GetRectangle().X+marvin.GetRectangle().Width))
{ MoveRight(2); Camera.MoveLeft(); }
}
}
if(Main.currentKeyState.IsKeyDown(Keys.Left))
{
Camera.MoveLeft();
if(Camera.startReached)
{ MoveLeft(2); }
else
{
if(marvin.GetRectangle().X!=Main.graphics.PreferredBackBufferWidth-(marvin.GetRectangle().X+marvin.GetRectangle().Width))
{ MoveLeft(2); Camera.MoveRight(); }
}
}
Camera.Restrain();
if(marvin.GetRectangle().X>Main.graphics.PreferredBackBufferWidth-marvin.GetRectangle().Width)
{ MoveLeft(2); }
if(marvin.GetRectangle().X<0)
{ MoveRight(2); }
And Here Is My Player Jumping/Falling Code which may cause some conflicts with the vertical camera movement.
if (!jumping)
{
if(!TileEngine.TopOfTileCollidingWith(footBounds))
{ MoveDown(5); }
else
{
if(marvin.GetRectangle().Y != TileEngine.LastPlatformStoodOnTop()-marvin.GetRectangle().Height)
{
float difference = (TileEngine.LastPlatformStoodOnTop()-marvin.GetRectangle().Height) - (marvin.GetRectangle().Y);
marvin.SetRectangle(marvin.GetRectangle().X,(int)(marvin.GetRectangle().Y+difference));
armR.SetRectangle(armR.GetRectangle().X,(int)(armR.GetRectangle().Y+difference));
armL.SetRectangle(armL.GetRectangle().X,(int)(armL.GetRectangle().Y+difference));
eyeL.SetRectangle(eyeL.GetRectangle().X,(int)(eyeL.GetRectangle().Y+difference));
eyeR.SetRectangle(eyeR.GetRectangle().X,(int)(eyeR.GetRectangle().Y+difference));
}
}
}
if (Main.currentKeyState.IsKeyDown(Keys.Up) && Main.previousKeyState.IsKeyUp(Keys.Up) && TileEngine.TopOfTileCollidingWith(footBounds))
{ jumping = true; }
if(jumping)
{
if(TileEngine.LastPlatformStoodOnTop()>0 && (TileEngine.LastPlatformStoodOnTop() - footBounds.Bottom)<120)
{ MoveUp(5); }
else
{ jumping = false; }
}
All player code I have tried for vertical movements has failed, or caused weird results (like falling through platforms), and most have been a variation on the method I described above, hence I have not included it. I would really appreciate some help implementing a simple vertical scrolling into this game, Thanks.