5
\$\begingroup\$

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();
        }


    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Is this running every frame? \$\endgroup\$
    – tyjkenn
    Mar 12, 2012 at 23:17
  • 2
    \$\begingroup\$ Sorry if this comes off wrong, but is your render area really 1800 wide? Should it maybe be 1080? \$\endgroup\$
    – James
    Mar 12, 2012 at 23:53
  • \$\begingroup\$ Show the code where you draw the player, it might help. \$\endgroup\$ Mar 12, 2012 at 23:56
  • \$\begingroup\$ It is in the Update() method, and it is 800, just I forgot to change the 1800 to 800. I changed it, and still same problem. I will post the code now :) \$\endgroup\$
    – CJ Cohorst
    Mar 12, 2012 at 23:57

1 Answer 1

5
\$\begingroup\$

You want to modify the player's position, not its source rectangle.

As I understand it, you use playerRect to represent the rectangle inside the image to draw, in order to animate it properly. That is the source rectangle and it does not affect where the player is drawn.

What you want is to modify the player's position field in order to really clamp its position within the screen boundaries.

Here is how you would do:

if (player.position.X <= 0)
{
  player.position.X = 0;
}

if (player.position.X + player.playerTexture.Width >= 1800)
{
  player.position.X = 1800 - player.playerTexture.Width;
}

Even better, use the existing MathHelper.Clamp function:

player.position.X = MathHelper.Clamp(player.position.X, 0, 1800 - player.playerTexture.Width);

As a side note, you want to block the player's movement after updating its position. Right now you are doing it inside your Update method before updating the player. This will make it that when you'll reach the border you'll have your position set back to the limit, but your position will still be updated directly afterwards, making you move a little off the screen.

\$\endgroup\$
1
  • 4
    \$\begingroup\$ To avoid this kind of problem later on, you might want to make the playerRect field private, so that you can not use it outside the Player class (since you most likely don't need it except for animating anyway). This will prevent you from using it by mistake. \$\endgroup\$ Mar 13, 2012 at 0:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .