0

XNA の NullReferenceException に問題があります。Hero、Sprite、Fireball、Game1 の 4 つのクラスがあります。デバッグを通じて、ファイアボールがパイプを介してコンテンツをロードした後に問題が発生することがわかりました

    class Fireball: Sprite
    {
        const int MAX_DISTANCE = 500;

        public bool Visible = false;

        Vector2 mStartPosition;
        Vector2 mSpeed;
        Vector2 mDirection;

        public void LoadContent(ContentManager theContentManager)
        {
            base.LoadContent(theContentManager, "Fireball");
            Scale = 0.3f;
        }

次に、Sprite クラスで ContentManager を介してテクスチャをロードしようとしています

class Sprite
    {
        //The asset name for the Sprite's Texture
        public string AssetName;

        //The Size of the Sprite (with scale applied)
        public Rectangle Size;

        //The amount to increase/decrease the size of the original sprite. 
        private float mScale = 1.0f;

        //The current position of the Sprite
        public Vector2 Position = new Vector2(0, 0);

        //The texture object used when drawing the sprite
        private Texture2D myTexture;

        //Load the texture for the sprite using the Content Pipeline
        public void LoadContent(ContentManager theContentManager, string theAssetName)
        {
            myTexture = theContentManager.Load<Texture2D>(theAssetName);
            AssetName = theAssetName;
            Source = new Rectangle(0, 0, myTexture.Width, myTexture.Height);
            Size = new Rectangle(0, 0, (int)(myTexture.Width * Scale), (int)(myTexture.Height * Scale)); ;
        }

そして、 myTexture = theContentManager.Load(theAssetName); で NullReferenceException が返されます。ライン。デバッグ レポートで、アセット名に「Fireball」が含まれていることがわかりますが、ContentManager 自体は null になります。私は何を間違っていますか?私はC#が初めてなので、どの行をどこに追加すればよいか教えていただければ幸いです。完全なプロジェクトが必要な場合は、こちらhttps://www.dropbox.com/s/1e353e834rggj40/test.rarを参照してください。

4

1 に答える 1

1

Game1 から Fireball の LoadContent を呼び出していません。これは、ContentManager がないことを意味します。これを Sprite クラスに追加しました:

public static ContentManager Cm;

そして、Game1 の LoadContent の上部に

Sprite.Cm = this.Content;

後で Sprite クラスで使用するために ContentManager を保存したため、正常に動作するはずです。

于 2013-06-08T12:47:19.843 に答える