此方法不接受此参数的null。我怎样才能解决这个问题?

时间:2012-10-22 15:40:12

标签: c# xna

我总是收到此错误消息:

  

此方法不接受此参数的null。   参数名称:texture

MyTexture类中的

Bullet为空,但我不知道如何更改它。
请问有人帮帮我吗?

public class Map
{
    Texture2D myEnemy;
    Player Player;
    List<Enemy> enemieslist = new List<Enemy>();    

    float fNextEnemy = 0.0f;
    float fEnemyFreq = 3.0f;
    int fMaxEnemy = 3;

    Vector2 Startposition = new Vector2(200, 200);
    GraphicsDeviceManager graphicsDevice; 

    public Map(GraphicsDeviceManager device) 
    { 
        graphicsDevice = device;
    } 

    public void Load(ContentManager content)
    {
    myEnemy = content.Load<Texture2D>("gegner");
    Player = new Player(graphicsDevice);
    Player.Load(content);
    }

    public void Update(GameTime gameTime)
    {
        Player.Update(gameTime);
        float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

         for(int i = enemieslist.Count - 1; i >= 0; i--) 
        {
        // Update Enemy
        Enemy enemy = enemieslist[i];
        enemy.Update(gameTime, this.graphicsDevice, Player.spielershape.Position, delta);
        // Try to remove an enemy
        if (enemy.Remove == true)
        {
            enemieslist.Remove(enemy);
            enemy.Remove = false;
        }
        }

        this.fNextEnemy += delta;
        //New enemy
        if (fMaxEnemy > 0)
        {
        if ((this.fNextEnemy >= fEnemyFreq) && (enemieslist.Count < 3))
        {
            Vector2 enemyDirection = Vector2.Normalize(Player.playershape.Position - Startposition) * 100f;
            enemieslist.Add(new Enemy(Startposition, enemyDirection, Player.playershape.Position));
            fMaxEnemy -= 1;
            fNextEnemy -= fEnemyFreq;
        }
        }        
  }

    public void Draw(SpriteBatch batch)
    {
        Player.Draw(batch);
        foreach (Enemy enemies in enemieslist)
        {
            enemies.Draw(batch, myEnemy);
        } 

    }      
}

public class Enemy
{
    List<Bullet> bulletslist = new List<Bullet>();
    Texture2D myBullet;

 private float nextShot = 0;
 private float shotFrequency = 2.0f;  

    Vector2 vPos;
    Vector2 vMove;
    Vector2 vPlayer;
    public bool Remove;
    public bool Shot;

    public Enemy(Vector2 Pos, Vector2 Move, Vector2 Player)
    {
        this.vPos = Pos;
        this.vMove = Move;
        this.vPlayer = Player;
        this.Remove = false;
        this.Shot = false;
    }

    public void Load(ContentManager content)
    {
        myBullet = content.Load<Texture2D>("bullet");
    }

    public void Update(GameTime gameTime, GraphicsDeviceManager graphics, Vector2 PlayerPos, float delta)
    {           
        nextShot += delta;

        for (int i = bulletslist.Count - 1; i >= 0; i--)
        {
            // Update Bullet
            Bullet bullets = bulletslist[i];
            bullets.Update(gameTime, graphics, delta);

            // Try to remove a bullet... Collision, hit, or outside screen.
            if (bullets.Remove == true)
                bulletslist.Remove(bullets);
            bullets.Remove = false;
        }

        if (nextShot >= shotFrequency)
        {
            this.Shot = true;
            nextShot -= shotFrequency;
        }

        // Does the enemy shot?
        if ((Shot == true) && (bulletslist.Count < 1))
        // New bullet
        {
            Vector2 bulletDirection = Vector2.Normalize(PlayerPos - this.vPos) * 200f;
            bulletslist.Add(new Bullet(this.vPos, bulletDirection, PlayerPos));
            Shot = false;
        }

        if (!Remove)
        {
            this.vMove = Vector2.Normalize(PlayerPos - this.vPos) * 100f;
            this.vPos += this.vMove * delta;

            if (this.vPos.X > graphics.PreferredBackBufferWidth + 1)
            {
                this.Remove = true;
            }

            else if (this.vPos.X < -20)
            {
                this.Remove = true;
            }

            if (this.vPos.Y > graphics.PreferredBackBufferHeight + 1)
            {
                this.Remove = true;
            }

            else if (this.vPos.Y < -20)
            {
                this.Remove = true;
            }
        }
    }

    public void Draw(SpriteBatch batch, Texture2D myTexture)
    {
        if (!Remove)
        {
            batch.Draw(myTexture, this.vPos, Color.White);
        }
        foreach (Bullet bullets in bulletslist)
        {
            bullets.Draw(batch, myBullet);
        } 
    }
}

public class Bullet
{
    Vector2 vPos;
    Vector2 vMove;
    Vector2 vPlayer;
    public bool Remove;

    public Bullet(Vector2 Pos, Vector2 Move, Vector2 Player)
    {
        this.Remove = false;
        this.vPos = Pos;
        this.vMove = Move;
        this.vPlayer = Player;
    }


    public void Update(GameTime gameTime, GraphicsDeviceManager graphics, float delta)
    {
            if (!Remove)
            {
                this.vPos += this.vMove * delta;                  

                if (this.vPos.X > graphics.PreferredBackBufferWidth +1)
                {
                    this.Remove = true;
                }

                else if (this.vPos.X < -20)
                {
                    this.Remove = true;
                }

                if (this.vPos.Y > graphics.PreferredBackBufferHeight +1)
                {
                    this.Remove = true;
                }

                else if (this.vPos.Y < -20)
                {
                    this.Remove = true;
                }
            }         
    }

    public void Draw(SpriteBatch spriteBatch, Texture2D myTexture)
    {
        if (!Remove)
        {
            spriteBatch.Draw(myTexture, this.vPos, Color.White);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这就是你的错误所在。因此,您需要添加一个空检查并绘制默认纹理或根本不绘制。

public void Draw(SpriteBatch spriteBatch, Texture2D myTexture)
{
    if (!Remove)
    {
        if (null == myTexture)
            myTexture = // some default texture, throw an error or skip the draw
        if (null != spriteBatch)
            spriteBatch.Draw(myTexture, this.vPos, Color.White);
    }
}

和/或你可以使用敌人Load()方法:

public void Load(ContentManager content)
{
    myBullet = content.Load<Texture2D>("bullet");
    Debug.Assert(null != myBullet);
}

每当你依赖一个值时,你应该使用asserts来永远是某种东西,即不是空的。