6 Replies - 1121 Views - Last Post: 29 April 2013 - 08:27 AM

#1 DJLad16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-April 13

Why is my texture null?

Posted 28 April 2013 - 09:05 AM

I'm still noob at Xna so I'm not sure why this is happen. I have an sprite animation class in which I instantiate it in my player class. (I'm probably doing so many this wrong :L )

Animtion.cs
public class Animation
    {
        Texture2D texture;
        Rectangle rectangle;
        Vector2 position;
        Vector2 origin;
        Vector2 velocity;

        KeyboardState keyState;

        int currentFrame;
        int frameHeight;
        int frameWidth;

        float timer;
        float interval = 80;

        public Animation(Texture2D newTexture, Vector2 newPosition, int newFrameWidth, int newFrameHeight) 
        {
            texture = newTexture;
            position = newPosition;
            frameWidth = newFrameWidth;
            frameHeight = newFrameHeight;
        }
        public void Update(GameTime gameTime) 
        {
            keyState = Keyboard.GetState();

            rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
            origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
            position += velocity;

            if (keyState.IsKeyDown(Keys.D))
            {
                AnimateLeft(gameTime);//I know animate right should be here, but for some reason if I do put it here, the animation happens like he is walking left, but is actually moving right and vise versa

                velocity.X = 3;
            }
            else if (keyState.IsKeyDown(Keys.A))
            {
                AnimateRight(gameTime);
                velocity.X = -3;
            }
            else if (keyState.IsKeyDown(Keys.W))
                velocity.Y = -3;
            else if (keyState.IsKeyDown(Keys.S))
                velocity.Y = 3;
            else
                velocity = Vector2.Zero;
        }
        public void AnimateRight(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval) 
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 2) 
                    currentFrame = 0;
                
            }
        }
        public void AnimateLeft(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;
                timer = 0;
                if (currentFrame > 5 || currentFrame < 3)
                    currentFrame = 3;

            }
        }
        public void Draw(SpriteBatch spriteBatch) 
        {
            if (texture != null)
                spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);//This is where I am getting texture is null.
        }
    } 


Player.cs

 public class Player
    {
        Texture2D playerTexture;
        Vector2 velocity;
        int frameWidth;
        int frameHeight;

        Animation animation;

        public void Initialize() 
        {
            frameWidth = 32;
            frameHeight = 32;
            velocity = new Vector2(5, 5);
            animation = new Animation(playerTexture, velocity, frameWidth, frameHeight);
        }

        public void LoadContent(ContentManager Content) 
        {
            playerTexture = Content.Load<Texture2D>("Sprites/sprite sheet (one line)");
        }

        public void Update(GameTime gameTime) 
        {
            animation.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch) 
        {
            animation.Draw(spriteBatch);
        }
    }



Game1.cs

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        int screenWidth;
        int screenHeight;

        Player player;

        // Game Constructer
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 600;

            Content.RootDirectory = "Content";
        }
        // Intialize
        protected override void Initialize()
        {
            screenWidth = GraphicsDevice.Viewport.Width;
            screenHeight = GraphicsDevice.Viewport.Height;

            player = new Player();

            player.Initialize();

            base.Initialize();
        }

        // Load Content
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            player.LoadContent(Content);
        }

        // Unload Content
        protected override void UnloadContent()
        {
            
        }
        // Update
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            player.Update(gameTime);

            base.Update(gameTime);
        }

        // Draw
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            player.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my texture null?

#2 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Why is my texture null?

Posted 28 April 2013 - 10:08 AM

You need to call your players LoadContent method before calling the Player.Initialize method.
Was This Post Helpful? 0
  • +
  • -

#3 DJLad16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-April 13

Re: Why is my texture null?

Posted 28 April 2013 - 10:16 AM

I've just tried that by putting the LoadContent method in both player and game1 above the initialize methods so that the LoadContent is being called first, but it didn't do anything
Was This Post Helpful? 0
  • +
  • -

#4 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Why is my texture null?

Posted 28 April 2013 - 10:22 AM

The order you have the methods typed out in wont have any affect on which are called first. In your Game1 Initialize method, remove the line that is calling player.Initialize and paste it into the Game1 LoadContent method under the player.LoadContent call.
Was This Post Helpful? 0
  • +
  • -

#5 DJLad16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-April 13

Re: Why is my texture null?

Posted 28 April 2013 - 11:29 AM

Thanks, Liber. So my initialize, ContentLoad etc methods don't always have to be in the same name method in game1?
Was This Post Helpful? 0
  • +
  • -

#6 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Why is my texture null?

Posted 29 April 2013 - 04:18 AM

Nope, they don't have to be called in the same named method. I pretty sure the only difference between the Initialize and LoadContent methods is that the GraphicsDevice isn't created until after Initialize has been run. So the only time you would have to consider which method to call your own methods in is if you need the GraphicsDevice object to not be null.
Was This Post Helpful? 1
  • +
  • -

#7 DJLad16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-April 13

Re: Why is my texture null?

Posted 29 April 2013 - 08:27 AM

Ok, thanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1