Sign in to follow this  
Nicholas Kong

Removing heart from life bar the correct way

Recommended Posts

This is a part of the code that makes up the health system for my ship for an arcade shooter game in Java. Each heart represents one life to the ship. The ship has 3 lives which would mean 3 hearts shows up on the screen. If the ship lose all 3 lives, the ship is destroyed. The removeHeart method is called when the ship has a valid collision on an object. The code I commented out does not remove the hearts in real time but the one that set the hearts invisible does remove hearts in real time. I am confuse why the remove method does not animate the hearts in real time because I am specifying the correct ship heart label. The remove method winds up doing is remove all the hearts from the array when the ship is destroyed.

 

 

 
// remove the heart of the given current heart index
public void removeHeart(int currentHeartIndex)
{ 
 
// the following commented code apparently does not make the heart disappear when the ship takes a hit
//getHeartPanel().remove(shipHeartLabel[currentHeartIndex]);
 
// this is the current workaround to removing the hearts 
shipHeartLabel[currentHeartIndex].setVisible(false);
}

Share this post


Link to post
Share on other sites

You probably need to force the panel to redraw after removal... dunno how to do that in whatever Java GUI framework you are using.

 

I bet if you remove the heart label and force the window to redraw (e.g. by covering it up with another window and then moving the window away) it would be removed.

 

In Win32 style frameworks it's usally a call to Invalidate on the window (or InvalidateRect to invalidate a portion of a window) that kicks off redrawing.

 

EDIT: The invalidation would be done for you automatically by the setVisible call, you need to find the way to do it manually.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

You probably need to force the panel to redraw after removal... dunno how to do that in whatever Java GUI framework you are using.

 

I bet if you remove the heart label and force the window to redraw (e.g. by covering it up with another window and then moving the window away) it would be removed.

 

In Win32 style frameworks it's usally a call to Invalidate on the window (or InvalidateRect to invalidate a portion of a window) that kicks off redrawing.

 

EDIT: The invalidation would be done for you automatically by the setVisible call, you need to find the way to do it manually.

Thanks! Yeah you were right. I needed to tell the panel to update itself. Its built-in updateUI method worked beautifully.

 

The Java GUI Framework is javaxswing

Edited by warnexus

Share this post


Link to post
Share on other sites
warnexus; if you're using Swing or AWT, either repaint() (to request a repaint at the next opportunity) or revalidate() (to request re-layout of LayoutManager-arranged components, and repaint if necessary) are the correct methods to invoke on the container (the object retrieved by getHeartPanel()).

Share this post


Link to post
Share on other sites

warnexus; if you're using Swing or AWT, either repaint() (to request a repaint at the next opportunity) or revalidate() (to request re-layout of LayoutManager-arranged components, and repaint if necessary) are the correct methods to invoke on the container (the object retrieved by getHeartPanel()).

Thanks! I will use repaint.

Share this post


Link to post
Share on other sites

just a quick note,

 

in stead of 'removing' a heart, would it not be easier just to not draw it in the first place. Just loop through the max health the player has (3), painting it X pixlest apart each time, then break out of the loop at the itteration that equals the players health

Share this post


Link to post
Share on other sites

It looks like it is a GUI element on a canvas so there is no need to redraw it every frame.

That is correct. I also cannot draw graphics on a JPanel so doing it this way seems to be the solution I could come up with

Share this post


Link to post
Share on other sites

just a quick note,

 

in stead of 'removing' a heart, would it not be easier just to not draw it in the first place. Just loop through the max health the player has (3), painting it X pixlest apart each time, then break out of the loop at the itteration that equals the players health

I cannot draw images on a JPanel at least as far as I know. I can only add components to it. But I see what you mean with the loop, that would be easier.

Share this post


Link to post
Share on other sites

just a quick note,

 

in stead of 'removing' a heart, would it not be easier just to not draw it in the first place. Just loop through the max health the player has (3), painting it X pixlest apart each time, then break out of the loop at the itteration that equals the players health

I cannot draw images on a JPanel at least as far as I know. I can only add components to it. But I see what you mean with the loop, that would be easier.

fair enough :), just thought ild put my suggestion in,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this