XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Making a HUD class

Last post 11/19/2009 8:58 AM by MrSoundless. 6 replies.
  • 11/16/2009 8:03 PM

    Making a HUD class

    I have created a class to count the coins collected by a sprite in my platform game.  I have created a CoinCollection class which is called from the main game.cs file:

    Anyone have any idea why it isn't displaying on screen?  displayScoreTotal is initialised to "Coins " and this is all that shows up on screen

    Thanks

     

     

     

  • 11/16/2009 8:08 PM In reply to

    Re: Making a HUD class

    My guess is that you are not updating the bounding rectangles each frame, and instead relying on the initial values when passed to the constructor. Rectangle are structs and copied by value, meaning that changing the player position in your game classes does not change the position of the player in your coin tracking component. Are you updating the appropriate rectangles for your player in the coin collection class each frame?

    In either case, setting a breakpoint in your method and checking that the values are what you expect are your best bet - I'm just guessing wildly.
  • 11/18/2009 2:21 PM In reply to

    Re: Making a HUD class

    it seems you are right in that it is a problem with the rectangle positions not changing as the sprite moves.  Its original position is what is saved and this doesn't update.  The Coin Collection is initialised as:

    coinCount =

    new CoinCollection((Content.Load<SpriteFont>("GameFont")), new Vector2(0,0), coin.SourceRect, player.SourceRect);

    This goes and fetches the sourceRect from the Coin and Player classes, from a method which is called in the main Game.cs classes update method.  What changes should I make to this to ensure the bounding rectangles are changed with every movement?

     

  • 11/18/2009 4:10 PM In reply to

    Re: Making a HUD class

    Every time your player or coin position changes, you need to update those positions in your HUD class instance. You can do this a number of ways:

    1) Push the data from your game objects into your HUD. Add properties to your CoinCollection class to allow setting the player and coin rectangles. At the end of every update/frame set these values to be the current values in your player and coin objects.

    2) Pull the data from your game objects into your HUD. Instead of passing the rectangle for the player and coin objects pass the player and coins instances themselves. Assuming your player and coin are classes (not structs) you will be able to read the updated values from them when rendering. When you change the values in your game your component will detect them automatically.
  • 11/18/2009 10:21 PM In reply to

    Re: Making a HUD class

    you could also try this instead


    coinCount = new CoinCollection((Content.Load<SpriteFont>("GameFont")), new Vector2(0,0), ref coin.SourceRect, ref player.SourceRect); 

  • 11/18/2009 10:26 PM In reply to

    Re: Making a HUD class

    MrSoundless:
    you could also try this instead

    coinCount = new CoinCollection((Content.Load<SpriteFont>("GameFont")), new Vector2(0,0), ref coin.SourceRect, ref player.SourceRect); 


    There is no way to have a persistent reference to a struct in C#. Using ref above (if it even compiles) won't maintain a reference to the player or coin properties and won't reflect any later changes to their values.
  • 11/19/2009 8:58 AM In reply to

    Re: Making a HUD class

    oh I didn't know that :/ thank you for pointing that out, good I didn't have to use that yet...
Page 1 of 1 (7 items) Previous Next