Player not drawn during countdown before game scre

2019-08-08 16:31发布

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).

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.

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.

The relevent code is provided below:

code from the player class

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

code from the paddle class

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

code from game1 class

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...

    }

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).

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

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.

1条回答
我命由我不由天
2楼-- · 2019-08-08 16:37

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.

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).

A general piece of advice when debugging:

  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.
查看更多
登录 后发表回答