Health bar in script
by Maurice Ribble · in Torque Game Builder · 12/25/2006 (6:26 pm) · 7 replies
At first I thought I could just use SetSizeX on a sprite. Then I realized that doesn't do exactly what I want because I want my health bar to be a smooth gradient from red to green and scaling the sprite wouldn't look correct. Also I would have to reposition the bar every time my camera moved.
I thought maybe a gui control would somehow help, but I don't know much about TGB GUIs. Can anyone help me?
I thought maybe a gui control would somehow help, but I don't know much about TGB GUIs. Can anyone help me?
#2
I did a quick test with some junk image maps and it seems to be doing what I wanted. Now I just need to make sure it looks right with real textures. One little thing I noticed is since my game moves the camera around I will need to adjust the window position every time I move the camera, but that shouldn't be much of a problem. If there is some way to lock this new t2DSceneWindow to window coordinates on the parent window that would be nice, but if it can only be specified in world coordinates (as I suspect) then adding the offset code to my camera will be just a little more overhead.
I recently added some text to track my health so now this health bar isn't at the very top of my list, but I'll probably get back to it in a week or two. When I do get back to it this code of yours will be a huge help.
Thanks again!
12/26/2006 (7:36 pm)
Thanks!I did a quick test with some junk image maps and it seems to be doing what I wanted. Now I just need to make sure it looks right with real textures. One little thing I noticed is since my game moves the camera around I will need to adjust the window position every time I move the camera, but that shouldn't be much of a problem. If there is some way to lock this new t2DSceneWindow to window coordinates on the parent window that would be nice, but if it can only be specified in world coordinates (as I suspect) then adding the offset code to my camera will be just a little more overhead.
I recently added some text to track my health so now this health bar isn't at the very top of my list, but I'll probably get back to it in a week or two. When I do get back to it this code of yours will be a huge help.
Thanks again!
#3
12/27/2006 (8:39 pm)
No problem! Glad you find it useful. I haven't had to move the camera yet in my project so I haven't seen the issue you speak of with locking coordinates.. I'm sure there is a command that makes that (relatively) easy (hopefully).
#4
It's not exactly what you were asking for, but it works well and looks good in game I think. :)
Cheers!
--clint
12/28/2006 (9:12 am)
I don't use a gradient for my health bar in my game -- rather, I just have a solid gray box for a sprite that I stretch to be the size of my health bar, then I set its blending color to indicate the health of my character.%this.healthBar.setBlendColor( 1.0 - %this.health / %this.maxHealth , %this.health / %this.maxHealth, 0, 0.5 );
It's not exactly what you were asking for, but it works well and looks good in game I think. :)
Cheers!
--clint
#5
12/29/2006 (4:17 am)
Or you may put on your bar another sprite(with background) and distort them (change size and position)
#6
12/29/2006 (5:27 am)
I appreciate these alternate ideas. It gets my creative juices flowing :)
#7
The problem is that the code I added to update the bars position based on camera position doesn't work very well. The bar basically stays in the same spot, but when I get the camera position (I'm using getWorldPoint() which is basing it's position off off the camera) the position I get back lags by one frame. This results in the bar being jerking around by the amount of velocity change in the camera. Having a health bar jerk around even if only by a few pixels looks bad. Can anyone think of a solution to this? Is there a way to keep an object motionless even as the camera moves?
Here is my health bar code in case someone wants it.
12/30/2006 (10:51 am)
I got back to the health bar issue. After hearing all your great ideas I came up with my own :). My solution is a single scrolling texture. The texture is 256x1. The left half of the texture is my gradient. The right half of the texture is my background (in my case I wanted it clear so I set the alpha to 0). Then I set the RepeatX for the scrolling sprite to 0.5. When the I want to reduce the health I just scroll the texture to the texture to the right and move the sprite to the left by the same amount. This gives the effect I wanted.The problem is that the code I added to update the bars position based on camera position doesn't work very well. The bar basically stays in the same spot, but when I get the camera position (I'm using getWorldPoint() which is basing it's position off off the camera) the position I get back lags by one frame. This results in the bar being jerking around by the amount of velocity change in the camera. Having a health bar jerk around even if only by a few pixels looks bad. Can anyone think of a solution to this? Is there a way to keep an object motionless even as the camera moves?
Here is my health bar code in case someone wants it.
function SBar::OnAdd(%this)
{
$healthBar = %this;
}
function SBar::OnLevelLoaded( %this, %scenegraph )
{
// Used to place the bar relative the camera.
%worldPos = %this.GetPosition();
%this.m_windowPos = sceneWindow2D.getWindowPoint( %worldPos );
%this.m_maxVal = 100;
%this.m_curVal = 100;
%this.UpdateScrollBar();
}
function SBar::UpdateScrollBar( %this )
{
%this.m_scrollVal = -%this.GetSizeX() * ( %this.m_maxVal - %this.m_curVal ) / %this.m_maxVal;
%this.SetScrollPositionX( %this.m_scrollVal );
%this.SetPositionX( %this.GetPositionX() + %this.m_scrollVal );
}
function SBar::Init( %this, %max )
{
%this.m_maxVal = %max;
%this.m_curVal = %max;
%this.UpdateScrollBar();
}
function SBar::UpdateCameraPos( %this )
{
%worldPos = sceneWindow2D.getWorldPoint( %this.m_windowPos );
%xPos = getWord( %worldPos, 0 );
%yPos = getWord( %worldPos, 1 );
%this.SetPositionX( %xPos + %this.m_scrollVal );
%this.SetPositionY( %yPos + %this.m_offsetY );
}
// Negative %amount are damage and positive %amounts are health
function SBar::AddHealth( %this, %amount )
{
%this.m_curVal += %amount;
%this.m_curVal = mClamp( %this.m_curVal, 0, %this.m_maxVal );
%this.UpdateScrollBar();
}
function SBar::SetHealth( %this, %val )
{
%this.m_curVal = %val;
%this.m_curVal = mClamp( %this.m_curVal, 0, %this.m_maxVal );
%this.UpdateScrollBar();
}
Torque Owner Joe Rossi
Indri Games
www.garagegames.com/mg/forums/result.thread.php?qt=51871