我在 XNA 的第一场比赛(小问题)

发布于 2024-11-28 03:05:25 字数 2184 浏览 0 评论 0原文

这是我到目前为止的代码: Game1.cs 类:

 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.PreferredBackBufferHeight;
        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);
    }
}

玩家类(球):

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();
    }
}

当我尝试调试游戏时,出现以下错误:

此方法不接受此参数为 null。 参数名称:纹理 在那部分:

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

顺便说一句,我想问我是否可以使这段代码更好或类似的东西。 多谢!

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.PreferredBackBufferHeight;
        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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜还是长夜 2024-12-05 03:05:25

看起来您在加载 Ball 内容之前创建了 Player 对象,因此,player 持有 null 而不是纹理,但游戏中的 Ball 字段是真实的纹理。

在分配 Ball 后,我会将 Player 的创建移至 LoadContent。

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

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);
感情旳空白 2024-12-05 03:05:25

我将在回答前说,您应该在 Game1.cs Draw 函数中调用 spriteBatch.begin() 和 spriteBatch.end() 而不是 Player.cs Draw 函数。它很昂贵,并且每个并条机不应执行多次以上,除非绝对必要(在本例中并非如此)。

关于您的实际问题,您需要在 LoadContent 方法而不是 Initialize 方法中加载播放器。

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.

古镇旧梦 2024-12-05 03:05:25

初始化是在加载纹理之前发生的。

尝试将 MyPlayer = new Player(Ball, new Vector2(100, 100), Vector2.Zero); 移动到 LoadContent 方法中。

将纹理加载到 Ball 后,

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.

寂寞花火° 2024-12-05 03:05:25

看起来您正在使用“NULL”球纹理初始化 myPlayer 之后加载球纹理

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

别忘他 2024-12-05 03:05:25

这是因为 Initialize 在 LoadContent 之前调用,并且在创建 Player 时 Ball 纹理仍然为空。

加载球后,在 LoadContent 中创建 Player 对象,或者允许 Player 加载自己的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文