• Advertisement
Sign in to follow this  

[XNA] Dynamic Positions for Images

This topic is 2354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently have my health bar drawn to the screen at set coordinates.
I'm using a tile map and when my player moves the health bar's coordinates don't adjust.

Code for health bar

GameRef.SpriteBatch.Draw(
HealthBarNegative,
new Rectangle(150, 630, 150, 15),
Color.White);

GameRef.SpriteBatch.Draw(
HealthBarPositive,
new Rectangle(150, 630, 150 * (int)CurrentHealth / 100, 15),
Color.White);

GameRef.SpriteBatch.Draw(
HealthBar,
new Rectangle(150, 630, 150, 15),
Color.White);


Maybe I could have an update method that will adjust my image accordingly, but right now I'm still wondering how I will achieve that.

Share this post


Link to post
Share on other sites
Advertisement

I currently have my health bar drawn to the screen at set coordinates.
I'm using a tile map and when my player moves the health bar's coordinates don't adjust.

Code for health bar

GameRef.SpriteBatch.Draw(
HealthBarNegative,
new Rectangle(150, 630, 150, 15),
Color.White);

GameRef.SpriteBatch.Draw(
HealthBarPositive,
new Rectangle(150, 630, 150 * (int)CurrentHealth / 100, 15),
Color.White);

GameRef.SpriteBatch.Draw(
HealthBar,
new Rectangle(150, 630, 150, 15),
Color.White);


Maybe I could have an update method that will adjust my image accordingly, but right now I'm still wondering how I will achieve that.


you need to keep it consistent with your viewport. If you're moving the viewport when the player moves, and you dont adjust the healthbar's coordinates, it will move off screen when the viewport moves. Anotherway to compensate is to create a separate parallel game-loop for use of overlays (which is what your healthbar would be) I believe XNA calls it DrawableGameComponent (http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.drawablegamecomponent.aspx)

Add it to your game conent and it will be called like the normal game loop for Load, Unload, Initialize, Update, and Draw. You can place all your overlay content in that and just update it through the normal game loop.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement