Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Animation and Movement Problems

$
0
0
Hi,

My skills at XNA are just above beginner, however no matter what i try i cant get my animated sprite to move forward.
If any of you guys could help me I would really appreciate it.

I've tried a few tutorials with no luck, I feel like I'm missing something


So here's my code for the animation class(Sorry i created it with experimentation in mind and haven't cleaned it up yet)

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 Extreme_Fighter_Pilot
{
    public class AnimatedPlayerLeft
    {
        public Texture2D TextureLeft { get; set; }
        Rectangle spriteRectangle; //Dont know why i need this, it was just in the tutorial.
        public int Rows { get; set; }
        public int Columns { get; set; }
        private int currentFrame;
        private int totalFrames;


        //Sprite Movement, Everything here was from the last tutorial i was looking at.
        public Rectangle boundingBox;
        public bool isColliding;
        Vector2 spriteOrigin;

        Vector2 spritePosition;
        float rotation;

        Vector2 spriteVelocity;
        const float tangentialVelocity = 5f;
        float friction = 0.1f;

        public AnimatedPlayerLeft(Texture2D textureLeft, int rows, int columns)
        {
            TextureLeft = textureLeft;
            Rows = rows;
            Columns = columns;
            currentFrame = 0;
            totalFrames = Rows * Columns;
            isColliding = false;
            spritePosition = new Vector2(300, 300); //I messed around with this a lot
        }

        public void Update()
        {
            //The mess i left over from when i gave up on this approach.
            spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y,
                TextureLeft.Width, TextureLeft.Height);
            spritePosition = spriteVelocity + spritePosition;
            spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);

            //This is where i have been doing most of my experimenting

            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                spritePosition.X += tangentialVelocity; 
            }
            else if (spriteVelocity != Vector2.Zero)
            {
                Vector2 i = spriteVelocity;
                spriteVelocity = i -= friction * i;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.A)) //This is for the turning left animation.
            {
                rotation -= 0.1f; //I need help with this as well, it doesnt work.

                if (currentFrame < 19) //19 is the last frame of the animation, if the player holds down the button the animation will hold this frame
                {
                    currentFrame++;
                }
            }
            if (Keyboard.GetState().IsKeyUp(Keys.A)) //This is for a smooth transition back to frame 0.
            {
                if (currentFrame > 0) //To stop it from going into the minus.
                {
                    if (currentFrame < 20) //To stop it from interfering with the turning right animation.
                    {
                        currentFrame--;
                    }
                }

            }

            if (Keyboard.GetState().IsKeyDown(Keys.D)) //This is the turning right animation.
            {
                rotation += 0.1f; //Doesnt work either.

                if (currentFrame == 0) //To set the current frame to the first frame of the animation, i chose for it to only change once its at 0 so that the turning left animation must be finished
                {
                    currentFrame = 21;    
                }
                if (currentFrame < 39) //To hold it at the last frame of the animation.
                {
                    if (currentFrame > 20) //To stop it from using the wrong animation
                    {
                        currentFrame++;
                    }
                }

            }
            if (Keyboard.GetState().IsKeyUp(Keys.D))
            {
                if (currentFrame > 20)
                {
                    currentFrame--;
                }
            }

            

            if (currentFrame == 20) 
            {
                currentFrame = 0;
            }

            

        }

        public void Draw(SpriteBatch spriteBatch, Vector2 location) //I havent messed with anything here, its straight from the tutorial
        {
            int width = TextureLeft.Width / Columns;
            int height = TextureLeft.Height / Rows;
            int row = (int)((float)currentFrame / (float)Columns);
            int column = currentFrame % Columns;

            Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
            Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);

         
            spriteBatch.Draw(TextureLeft, destinationRectangle, sourceRectangle, Color.White);
          
        }
    }
}



The image is loaded in the Game1.cs class and i will post it if reqeusted but i don't think it will be relevant for now.

Thank you for reading
Mark.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>