在游戏画面 XNA 之前的倒计时期间未绘制玩家 [英] Player not drawn during countdown before game screen XNA

查看:52
本文介绍了在游戏画面 XNA 之前的倒计时期间未绘制玩家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上在我在我的游戏中实现继承之前,引入了 2 个新类,玩家和敌人,(不使用敌人 atm),在倒计时屏幕(关卡之前的倒计时)期间加载和绘制关卡(包括玩家桨)开始).

Basically before i implemented inheritence into my game, introducing 2 new classes, player and enemy, (not using enemy atm), the level was loaded and drawn (including the player paddle) during the countdown screen (the countdown before the level starts).

现在,当我在我的游戏中实现继承时,所有关卡都与玩家桨分离,从一个名为 Paddle 的类继承,其中 Player 和 Enemy 类是派生类.我已经尝试从基类/桨类覆盖播放器类中的 draw 方法,然后在基类中使用 draw 方法.

Now all of the level is drawn apart from the player paddle when i have implemented inheritence into my game, inheriting from a class called Paddle, where the Player and Enemy classes are derived classes. I have experimented between overriding the draw method in the player class from the base/paddle class and then using the draw method in the base class.

也许这与 draw 方法无关,但我所知道的是在倒计时屏幕期间没有绘制播放器,但是当倒计时屏幕完成时,绘制了播放器.这似乎是一个小问题或变化,但它真的令人沮丧,因为我无法弄清楚是什么导致了这种情况.

Maybe it's nothing to do with the draw method necessarily but all i know is that the player isnt being drawn during the countdown screen but when the countdown screen finishes, the player is drawn. This may seem like a small problem or change but it's really frustrating, as i cant work out what's causing this.

相关代码如下:

来自播放器类的代码

//constructor
public Player(PlayerIndex newID, Texture2D newTexture, Vector2 newPosition, Viewport theViewport, Color newColour)
        : base(newTexture, newPosition, theViewport, newColour)


public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(pTexture, pPosition, pRectangle, pColour);
        //base.Draw(gameTime, spriteBatch);
    }

桨类代码

//constructor
public Paddle(Texture2D newTexture, Vector2 newPosition, Viewport theViewport, Color newColour)
    {
        pTexture = newTexture;
        pPosition = newPosition;

        //theres more code here but it's assigning parameter values to properties in this class
    }

public virtual void Update(GameTime gameTime)
    {                        
        pRectangle = new Rectangle((int)pPosition.X, (int)pPosition.Y, pWidth, pHeight);            

    }

    public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(pTexture, pPosition, pRectangle, pColour);
    }

来自 game1 类的代码

public void DrawPreGameCountdownScreen(GameTime gameTime)
    {         
        DrawPlayingGameScreen(gameTime);

        //more code here but it's irrelevent....
    }


public void DrawPlayingGameScreen(GameTime gameTime)
    {

        DrawBlocks(gameTime);
        player1.Draw(gameTime, spriteBatch);

        //more code here... but irrelevent to this...

    }

player1 显然是玩家,它的类型是 Player 而不是 Paddle(因为我已经使 Paddle 类抽象......因为我不想允许直接创建 Paddle 对象).

The player1 is obviously the player and it's of type Player and not Paddle (as i have made the Paddle class abstract... as i don't want to allow for creating a Paddle object directly).

我已将 Paddle 类中的所有属性也设为受保护.

I have put all of the properties in the Paddle class as protected also.

我还使用游戏状态来区分游戏中的不同点,因此在倒计时屏幕的更新方法中,它基本上减少了计时器的值,然后在这样做之前检查计时器是否为 0,否则状态发生变化.

I am also using game states to differentiate between different points in the game, so in the update method for the countdown screen it basically decreases the value of the timer and before that checks that the timer isnt at 0 before doing so, otherwise the state changes.

正如我所说,这之前运行得很好,所以我认为这与播放器类有关或我做错了什么.

As i say, this worked perfectly before, so i assume that its something to do with the player class or something ive done wrong.

如果您需要更多信息,请随时提问,任何想法/解决方案等请告诉我:)

If you need any more info, please feel free to ask and any ideas/solutions, etc please let me know :).

非常感谢您在高级方面的帮助.

Thank you v much for the help in advanced.

推荐答案

在 paddle 的 Update 函数中,您可以初始化 pRectangle 成员(用于绘图).如果这个成员没有在其他地方(到起始位置)初始化,你很可能在倒计时期间绘制了一个零大小的矩形,因此它不会出现.

In the paddle's Update function you initialize the pRectangle member (used in drawing). If this member isn't initialized in another place (to the starting location), you are likely drawing a zero-size rectangle during the countdown, thus it doesn't appear.

您不只是得到异常的原因是 Rectangle 是一个结构体,因此除非您将其声明为 Rectangle?,否则不能为 null.相反,它的成员被初始化为默认值(数字类型为 0).

The reason you don't just get an exception is that Rectangle is a struct and so cannot not be null unless you declare it as a Rectangle?. Instead its members get initialized to default values (0 for numeric types).

调试时的一般建议:

  1. 检查以确保相关代码确实被调用.设置断点并确保它被命中.
  2. 如果是,检查断点处的数据是否正确.未初始化的值类型确实会咬你,所以如果看起来你得到了一堆默认值,请检查它们.
  1. Check to make sure the code in question is actually being called. Set a breakpoint and make sure it gets hit.
  2. If it is, check that the data at the breakpoint is correct. Uninitialized value types can really bite you, so check for those if it looks like you are getting a bunch of default values.

这篇关于在游戏画面 XNA 之前的倒计时期间未绘制玩家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆