Removing heart from life bar the correct way

Started by
8 comments, last by dsm1891 11 years ago

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

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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()).
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

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.

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

Mobile Developer at PawPrint Games ltd.

(Not "mobile" as in I move around a lot, but as in phones, mobile phone developer)

(Although I am mobile. no, not as in a babies mobile, I move from place to place)

(Not "place" as in fish, but location.)

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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.

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,

Mobile Developer at PawPrint Games ltd.

(Not "mobile" as in I move around a lot, but as in phones, mobile phone developer)

(Although I am mobile. no, not as in a babies mobile, I move from place to place)

(Not "place" as in fish, but location.)

This topic is closed to new replies.

Advertisement