0

I decided to try taking up monogame a few days ago and followed this tutorial coupled with visual studio 2013: http://blogs.msdn.com/b/tarawalker/archive/2012/12/04/windows-8-game-development-using-c-xna-and-monogame-3-0-building-a-shooter-game-walkthrough-part-1-overview-installation-monogame-3-0-project-creation.aspx

After a lot of messing about I managed to complete the 6 available lessons (the rest are (hopefully) still being written) but decided to give up venturing further in messing with the code as monogame seemed to have some very bad problems which after much research I found was supposed to be fixed in the latest developer branch, but still didn't work.

I then decided to install VS2010 and XNA and redo the tutorial using XNA instead. After much messing about having to manually convert texture files to XNB (no idea why they wasn't converting automatically) I have again completed the 6 lessons of the tutorial with one problem!

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ProjectSummer;

namespace Shooter
{
    class Player
    {
        public Animation PlayerAnimation;

        public Vector2 Position;        // Player Position Relative to the Upper Left of the Screen
        public bool Active;             // State of the player
        public int Health;              // Player HP


            public int Width    // Width of Player Ship
                {get { return PlayerAnimation.FrameWidth; }}

            public int Height   // Height of Player Ship
                {get { return PlayerAnimation.FrameHeight; }}

            public void Initialize(Animation animation, Vector2 position)
            {
                PlayerAnimation = animation;
                 Position = position;
                 Active = true;
                 Health = 100;}

            public void Update(GameTime gameTime)
            {
                PlayerAnimation.Position = Position;
             PlayerAnimation.Update(gameTime);
            }

            public void Draw(SpriteBatch spriteBatch)
            {

                PlayerAnimation.Draw(spriteBatch);

            }
    }
}

The problem seems to come from "PlayerAnimation.Position = Position;" code. it keeps returning

NullReferenceException was unhandled Object reference not set to an instance of an object.

I'm not sure why this problem is happening in XNA but not in Monogame. I've checked all of my code files and aside from obvious formatting differences they're the same.

Help on this would be greatly appreciated

cdlink
  • 1
  • from monogame to XNA? O-o – Weyland Yutani Jul 11 '14 at 11:39
  • Can we see a `PlayerAnimation` class? – Petr Abdulin Jul 11 '14 at 11:41
  • The reason why a null reference exception is occuring is that PlayerAnimation is null. So you could reask this question with the title "Why is PlayerAnimation null in the Update method" and no-one could mark it as duplicate. One reason why PlayerAnimation might be null is that the Initialize method has not been called. Another reason might be that the Initialize method was called but the animation parameter passed to it was null. – Weyland Yutani Jul 11 '14 at 11:46
  • Also you are better off staying with Monogame IMO. Microsoft have dropped support for XNA going forward. Also Monogame allows you to target more platforms. – Weyland Yutani Jul 11 '14 at 11:50

0 Answers0