I'm working on a 2D, top down, twin stick shooter. When the player moves I want the player position to change based on the background size, but to remain in the middle of the screen. Right now when the player moves they move on the actual screen position and I cant get the background to display correctly. The Update() function updates the player and then passes the player position to the background class. Wondering if any one can help.
The player class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace WindowsGame1.sprites
{
class PlayerSprite:BaseSprite
{
const int PLAYER_SPEED_X = 10;
const int PLAYER_SPEED_Y = 10;
const int SLIDE_RATE = 50;
const int MAIN_GUN_RATE = 75;
SpriteManager manager;
int timeSinceLastSlide = 0;
int timeSinceLastFire = 0;
bool firing = false;
float playerAngle;
float turretAngle;
float scale = .25f;
int xFrameSize = 0;
int yFrameSize = 0;
int playerCurrentSpeedX = 0;
int playerCurrentSpeedY = 0;
Vector2 sticksLeft = Vector2.Zero;
Vector2 sticksRight = Vector2.Zero;
Vector2 turretLocation = Vector2.Zero;
point turretPos;
Texture2D turretImage, bulletImage;
List<BaseSprite> bullets = new List<BaseSprite>();
public PlayerSprite(int xP, int yP, int xFS, int yFS, int sheetSizeX, int sheetSizeY, int animTime, Texture2D image, Texture2D turret, Texture2D bullet, SpriteManager manager)
:base(xP, yP,xFS, yFS, sheetSizeX, sheetSizeY, animTime, 0, image)
{
this.turretImage = turret;
turretPos.x = xP;
turretPos.y = yP;
this.xFrameSize = xFS;
this.yFrameSize = yFS;
this.bulletImage = bullet;
this.manager = manager;
}
public void Update(GameTime gameTime, Rectangle backGroundSize)
{
sticksLeft = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular).ThumbSticks.Left;
sticksRight = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular).ThumbSticks.Right;
if ((sticksLeft.X) != 0)
{
playerCurrentSpeedX = (int)(PLAYER_SPEED_X * sticksLeft.X);
playerAngle = (float)Math.Atan2(sticksLeft.X, sticksLeft.Y);
}
this.position.x += playerCurrentSpeedX;
if ((sticksLeft.Y) != 0)
{
playerCurrentSpeedY = (int)(PLAYER_SPEED_Y * sticksLeft.Y);
playerAngle = (float)Math.Atan2(sticksLeft.X, sticksLeft.Y);
}
this.position.y -= playerCurrentSpeedY;
//the following code is used to make the player slide after releasing left stick
if ((sticksLeft.Y) == 0 && (sticksLeft.X) == 0 && (playerCurrentSpeedX != 0 || playerCurrentSpeedY != 0))
{
timeSinceLastSlide += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastSlide >= SLIDE_RATE)
{
timeSinceLastSlide = 0;
if (playerCurrentSpeedY > 0)
{
playerCurrentSpeedY--;
}
else if (playerCurrentSpeedY < 0)
{
playerCurrentSpeedY++;
}
if (playerCurrentSpeedX > 0)
{
playerCurrentSpeedX--;
}
else if (playerCurrentSpeedX < 0)
{
playerCurrentSpeedX++;
}
}
}
if (this.position.x <= 0 + (frameSizes.x / 2))
{
//this.position.x = (frameSizes.x / 2);
}
if (this.position.y <= 0 + (frameSizes.y / 2))
{
//this.position.y = (frameSizes.y / 2);
}
//////////////////////////////////////////////////////
if (this.position.x >= 1300 - frameSizes.x / 2)
{
//this.position.x = 1300 - frameSizes.x / 2;
}
if (this.position.y >= 700 - frameSizes.y / 2)
{
//this.position.y = 700 - frameSizes.y / 2;
}
System.Diagnostics.Debug.Print(position.x + " " + position.y);
turretPos.x = this.position.x;
turretPos.y = this.position.y;
float bulletVelocityX = (sticksRight.X * 10);
float bulletVelocityY = (sticksRight.Y * 10);
if (Math.Abs(sticksRight.X) >= .5f || Math.Abs(sticksRight.Y) >= .5f)
{
firing = true;
turretAngle = (float)Math.Atan2(sticksRight.X, sticksRight.Y);
}
timeSinceLastFire += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFire >= MAIN_GUN_RATE && firing)
{
firing = false;
timeSinceLastFire = 0;
manager.addBullet(new BaseSprite(position.x - 25, position.y - 25, (int)bulletVelocityX, (int)bulletVelocityY, 20, 40, 0, 0, 0, (float)Math.Atan2(sticksRight.X,
sticksRight.Y), bulletImage));
}
foreach (BaseSprite s in bullets)
{
s.Update(gameTime);
}
base.Update(gameTime);
}
public override void Draw(SpriteBatch b)
{
b.Draw(image, new Vector2(position.x, position.y), new Rectangle(rowFrameIndex * sheetSize.x, colFrameIndex * sheetSize.y, frameSizes.x, frameSizes.y),
Color.White, playerAngle, new Vector2(xFrameSize/2, yFrameSize/2), .25f, SpriteEffects.None, 0);
b.Draw(turretImage, new Vector2(position.x - 25, position.y - 25), new Rectangle(1, 1, 100, 100),
Color.White, turretAngle, new Vector2(25, 25), .25f, SpriteEffects.None, 0);
}
public bool isFiring()
{
firing = false;
return firing;
}
public Vector2 getPosition()
{
return new Vector2(this.position.x, this.position.y);
}
}
}
The background class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace WindowsGame1.sprites
{
class BackgroundSprite:BaseSprite
{
const int SCREEN_HEIGHT = 800;
const int SCREEN_WIDTH = 1400;
const int BACKGROUNDHEIGHT = 1400;
const int BACKGROUNDWIDTH = 2600;
Vector2 pPosition = Vector2.Zero;
Vector2 currentPosition = new Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
Vector2 backgroundorigin = new Vector2(BACKGROUNDWIDTH / 2, BACKGROUNDHEIGHT / 2);
Texture2D backgroundImage = null;
public BackgroundSprite(int xP, int yP, int xFS, int yFS, int sheetSizeX, int sheetSizeY, int animTime, Texture2D image)
:base(xP, yP,xFS, yFS, sheetSizeX, sheetSizeY, animTime, 0, image)
{
backgroundImage = image;
}
public void Update(GameTime gameTime, Vector2 playerPosition)
{
pPosition = new Vector2(-playerPosition.X, -playerPosition.Y);
}
public override void Draw(SpriteBatch batch)
{
batch.Draw(backgroundImage, pPosition, null,
Color.White, 0, backgroundorigin, 1, SpriteEffects.None, 0f);
}
}
}