I am making a game, and it has platforming and animation. I am trying to make it so that when the player goes to the end of the screen, he stops and cannot go further. I put this code in:
if (player.playerRect.X <= 0)
{
player.playerRect.X = 0;
}
if (player.playerRect.X + player.playerTexture.Width >= 1800)
{
player.playerRect.X = 1800 - player.playerTexture.Width;
}
It didn't do anything. I am confused why it doesn't work.
If you need it, here is my Game1.cs and my Player.cs
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 Cubiez
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
Texture2D grass;
Rectangle grassRect;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
player = new Player(Content.Load<Texture2D>("cubewalk"), new Vector2(100, 100), 47, 44);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
grassRect = new Rectangle(0, 0, 1200,480);
grass = Content.Load<Texture2D>("grass");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (player.playerRect.X <= 0)
{
player.playerRect.X = 0;
}
if (player.playerRect.X + player.playerTexture.Width >= 1800)
{
player.playerRect.X = GraphicsDevice.Viewport.Width - player.playerTexture.Width;
}
player.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(grass, grassRect, Color.White);
spriteBatch.End();
player.Draw(spriteBatch);
base.Draw(gameTime);
}
}
}
Players.cs:
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 Cubiez
{
class Player
{
public Texture2D playerTexture;
public Rectangle playerRect;
public Vector2 position;
public Vector2 origin;
public Vector2 velocity;
public int currentFrame;
public int frameHeight;
public int frameWidth;
float timer;
float interval = 150;
bool hasJumped;
public Player(Texture2D newPlayerTexture, Vector2 NewPosition, int newFrameHeight, int newFrameWidth)
{
position = NewPosition;
playerTexture = newPlayerTexture;
frameHeight = newFrameHeight;
frameWidth = newFrameWidth;
hasJumped = true;
}
public void Update(GameTime gameTime)
{
position += velocity;
playerRect = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
origin = new Vector2(playerRect.Width / 2, playerRect.Height / 2);
position = position + velocity;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
velocity.X = 2f;
AnimateRight(gameTime);
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
velocity.X = -2f;
AnimateLeft(gameTime);
}
else
{
velocity.X = 0f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space) && hasJumped == false)
{
position.Y -= 3f;
velocity.Y = -4.5f;
hasJumped = true;
}
if (hasJumped == true)
{
float i = 1;
velocity.Y += 0.15f * i;
}
if (position.Y + playerRect.Height >= 450)
{
hasJumped = false;
}
if (hasJumped == false)
{
velocity.Y = 0f;
}
}
public void AnimateRight(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 3)
{
currentFrame = 0;
}
}
}
public void AnimateLeft(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 7 || currentFrame < 4)
{
currentFrame = 4;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(playerTexture, position, playerRect, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
spriteBatch.End();
}
}
}