开发者

My first game in XNA(Little problem)

this is my code so far: Game1.cs Class:

 public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player MyPlayer;
    Texture2D Ball;
    int GraphicsWidth,GraphicsHeight;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        GraphicsWidth = graphics.PreferredBackBufferWidth;
        GraphicsHeight= graphics.Pre开发者_开发技巧ferredBackBufferHeight;
        MyPlayer = new Player(Ball, new Vector2(100, 100), Vector2.Zero);
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Ball = Content.Load<Texture2D>("Images/ball");

    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        base.Update(gameTime);
    }

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

        MyPlayer.Draw(spriteBatch);

        base.Draw(gameTime);
    }
}

Player class(The ball):

class Player
{
    Texture2D Texture;
    Vector2 Positon,Velocity;
    public int Height
    {
        get { return this.Texture.Height; }
    }
    public int Width
    {
        get { return this.Texture.Width; }
    }


    public Player(Texture2D tex, Vector2 position,Vector2 velocity)
    {
        this.Texture = tex;
        this.Positon = position;
        this.Velocity = velocity;
    }
    public void Draw(SpriteBatch spriteBatch)
    {
       spriteBatch.Begin();
       spriteBatch.Draw(Texture, Positon, Color.White);
       spriteBatch.End();
    }
}

When I try to debug the game I have the following error:

This method does not accept null for this parameter. Parameter name: texture In that part:

public void Draw(SpriteBatch spriteBatch)
    {
       spriteBatch.Begin();
       spriteBatch.Draw(Texture, Positon, Color.White);
       spriteBatch.End();
    }

Btw, I'd like to ask if I can make this code better or something like that. Thanks alot!


Looks like you create the Player object before you've loaded the Ball content, and thus, the player holds null instead of a texture, but the Ball field in the Game is the real texture.

I would move the creation of Player into LoadContent, after you've assigned the Ball.

Ball = Content.Load<Texture2D>("Images/ball");
MyPlayer = new Player(Ball, new Vector2(100, 100), Vector2.Zero);


I'm going to preface my answer by saying you should call spriteBatch.begin() and spriteBatch.end() in your Game1.cs Draw function instead of your Player.cs Draw function. It's expensive and you shouldn't do it more than once per draw frame, unless it's absolutely necessary (it's not in this case).

With regards to your actual question, you need to load your player in the LoadContent method rather than your Initialize method.


Initialize is happening before your texture is loaded.

try moving MyPlayer = new Player(Ball, new Vector2(100, 100), Vector2.Zero);

into your LoadContent method after you load the texture into Ball.


It looks like you're loading the ball texture after you've already initialized myPlayer with the "NULL" Ball texture


This is because Initialize is called before LoadContent, and at the point you create your Player the Ball texture is still null.

Either create the Player object in LoadContent, after you load the ball, or allow Player to load its own content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜