-1
\$\begingroup\$

I'm trying to make a little Zelda clone.

I'm just starting with MonoGame, following tutorials, and I have a problem when loading the sprite for my player character: it loads the whole spritesheet over him. In this image, you can see what i mean:enter image description here

Any help? Animation and Sprite classes should be ok, I think. Sorry if this is too long for such a little thing. Here's my code:

namespace Game1
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private BaseObject _player;
        private ManagerInput _managerInput;
        public Game1()
        {

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.graphics.PreferredBackBufferHeight = 240;
            this.graphics.PreferredBackBufferWidth = 320;
            _player = new BaseObject();
            _managerInput = new ManagerInput();

        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }


        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            _player.AddComponent(new Sprite(Content.Load<Texture2D>("player"), 50, 50, new Vector2(50, 50)));
            _player.AddComponent(new PlayerInput());
            _player.AddComponent(new Animation(16,16));

        }


        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }


        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            _managerInput.Update(gameTime.ElapsedGameTime.Milliseconds);
            _player.Update(gameTime.ElapsedGameTime.Milliseconds);

            // TODO: Add your update logic here

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(new Color(196,207,161));

            spriteBatch.Begin();
            _player.Draw(spriteBatch);
            spriteBatch.End();
                    base.Draw(gameTime);
        }
    }
}

Animation class:

 class Animation : Component
    {
        public override ComponentType ComponentType
        {
            get { return ComponentType.Animation; }
        }

        private int _width;
        private int _height;
        public Rectangle TextureRectangle { get; private set; }
        public bool LockDirection { get; set; }
        public bool LockAnimation { get; set; }

        public Direction CurrentDirection;
        public State CurrentState { get; private set; }
        private double _counter;
        public int AnimationIndex { get; private set; }
        private int _animationFrames;
        private int _animationSpeed;

        private bool _loop;
        private int _count;
        private int _current;

        public Animation(int width, int height, int animationFrames = 1, int animationSpeed = 200)
        {
            _width = width;
            _height = height;
            _counter = 0;
            AnimationIndex = 0;
            CurrentState = State.Standing;
            _animationFrames = animationFrames;
            _animationSpeed = animationSpeed;
            TextureRectangle = new Rectangle(0, 0, width, height);
        }


        public override void Update(double gameTime)
        {
            if (LockAnimation)
                return;

            if (!_loop && _current > _count - 1)
            {
                CurrentState = State.Standing;
                return;
            }

            _counter += gameTime;
            if (_counter > _animationSpeed)
            {
                switch (CurrentState)
                {
                    case State.Walking:
                        ChangeState(0, _animationFrames);
                        _counter = 0;
                        break;
                }
            }
        }

        public void PlayAnimation(State state, Direction direction, int count = 1, bool loop = false, bool forceReset = false)
        {
            if ((CurrentDirection != direction && !LockDirection) || forceReset)
            {
                _counter = 1000;
                AnimationIndex = 0;
            }

            CurrentState = state;
            if (!LockDirection)
                CurrentDirection = direction;
            _count = count;
            _current = 0;
            _loop = loop;
        }

        public void StopAnimation()
        {
            _current = _count + 1;
            _loop = false;
        }

        private void ChangeState(int y = 0, int animationFrames = 2)
        {
            if (AnimationIndex + 1 > animationFrames)
            {
                AnimationIndex = 0;
                _current++;
            }

            switch (CurrentDirection)
            {
                case Direction.Down:
                    TextureRectangle = new Rectangle(_width * AnimationIndex, y, _width, _height);
                    break;
                case Direction.Up:
                    TextureRectangle = new Rectangle(_width * AnimationIndex, y + _height, _width, _height);
                    break;
                case Direction.Left:
                    TextureRectangle = new Rectangle(_width * AnimationIndex, y + _height * 2, _width, _height);
                    break;
                case Direction.Right:
                    TextureRectangle = new Rectangle(_width * AnimationIndex, y + _height * 3, _width, _height);
                    break;
            }


            if (AnimationIndex + 1 <= animationFrames)
                AnimationIndex++;

        }

        public override void Draw(SpriteBatch spritebatch)
        {

        }

    }
}

`

Sprite class:

   class Sprite : Component
    {
        private Texture2D _texture;
        private int _width;
        private int _height;
        private Vector2 _position;

        public Sprite(Texture2D texture,int width,int height,Vector2 position)
        {
            _texture = texture;
            _width = width;
            _height = height;
            _position = position;
        }
        public override ComponentType ComponentType
        {
            get { return ComponentType.Sprite; }
        }
        //Los sprites se mueven por la pantalla para generar acción 
        public void Move(float x,float y)
        {
            _position = new Vector2(_position.X + x, _position.Y + y);

            var animation = GetComponent<Animation>(ComponentType.Animation);
            if (animation == null)
                return;
            if (x > 0)
            {
                animation.PlayAnimation(State.Walking, Direction.Right);
            }
            else if (x<0)
            {
                animation.PlayAnimation(State.Walking, Direction.Left);
            }
            else if (y > 0)
            {
                animation.PlayAnimation(State.Walking, Direction.Down);
            }
            else if (x < 0)
            {
                animation.PlayAnimation(State.Walking, Direction.Up);
            }
        }

        public override void Draw(SpriteBatch spritebatch)
        {
            var animation = GetComponent<Animation>(ComponentType.Animation);
            if (animation != null)
            {
                spritebatch.Draw(_texture, new Rectangle((int)_position.X, (int)_position.Y, _width, _height),animation.TextureRectangle, Color.White);
            }
            spritebatch.Draw(_texture, new Rectangle((int)_position.X, (int)_position.Y, _width, _height), Color.White);
        }

        public override void Update(double gameTime)
        {

        }
    }
}
`

Thank you.

\$\endgroup\$
  • \$\begingroup\$ What tutorials are you following? \$\endgroup\$ – dot_Sp0T Jun 1 '17 at 9:40
0
\$\begingroup\$
public override void Draw(SpriteBatch spritebatch)
{
    var animation = GetComponent<Animation>(ComponentType.Animation);
    if (animation != null)
    {
        spritebatch.Draw(_texture, new Rectangle((int)_position.X, (int)_position.Y, _width, _height),animation.TextureRectangle, Color.White);
    }
    spritebatch.Draw(_texture, new Rectangle((int)_position.X, (int)_position.Y, _width, _height), Color.White);
}

Your Draw method here contains two lines that draw the texture. The first one draws your player correctly underneath with the animation source rectangle, and the second one draws the whole texture to the position.

I think you want to place the second spritebatch.Draw(...) inside an else statement, so that your Sprite object can render just single sprites to the screen in combination with animated objects.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.