First off, makslane gets credit here for the Goober's initial graphic.
Kudos to Caaz for coming up with the simple healthbar routine displayed here.
Here is a complete tutorial on how to work with a healthbar, text representation of health, changing weapon when strike occurs, and showing damage to the enemy you are hitting.
I am also providing the files to look at.
Open GE and create the following actors:
Goober
DeadGoober
texthealth
mouseweapon
HealthBar
spelling in the actors names only matters if you are following this code with cut and paste.
For the animations, again do the following:
Goober: 4 frame animation, you can use my graphic or your own if you want. Name it:
pachurts_00
DeadGoober: single frame image. Name it whatever you want.
HealthBar:12 frame animation, Name it whatever you want.
mouseweapon:2 frame animation, Name it:
spikeball01
texthealth:click the text button, select what font you want, and enter 000% in the text area so you can see the size it will be for placement.
Variables:
You only need to make one, and it is an Actor variable. Name it:health, and make it an integer.
If you don't understand how to do it, select
[global code] from the script menu at the top, click on
[Variables] button. Then click on
[Add] button,
type: health next to
Name:, click
[Global] and change it to
[Actor variable], then click
[add] button at the bottom of the popup window. Then click
[Close] button at the bottom of the User Variables popup window. You can now click
[close] on the global code window or go ahead and add your global code in.
The syntax for the following code can be described as:
Actor -> Event
Using Actor Control window:
Select the actor next to "Name:" for the first part, click
[add] button next to
"Events:"
Then Select the Event stated in the second part. For the Collision events, the name and sides are listed in the () section.
Now for the
[add action], select script editor for all code sections below.
Example:
DeadGoober->Create Actor.
You would select DeadGoober next to "name:"
From the
[add] button next to
"Events:" you would select
[Create Actor],
and from the
[Add Actions] button, you would select
[Script Editor].
This should do it for the prep, you are ready to enter your coding now.
Put this in global code.
- Code: Select all
Goober.health=100;
sprintf(texthealth.text,"%d%%",Goober.health);
Save it as
primethem.
This sets the Actor:
Goober's initial
health to 100 and
prints it to the Text Actor:
texthealth in the visual form of: 100%
Place this in DeadGoober->Create Actor.
- Code: Select all
transp=.999;
This sets the actor used to show dead player to virtually invisible until needed.
Place this in Goober->Create Actor.
- Code: Select all
Goober.health=100;
ChangeAnimation("Event Actor", "pachurts_00", STOPPED);
This sets Actor:
Goober's
health back to 100 on respawn.(This demo as is doesn't respawn him)
The ChangeAnimation here Sets
Goober's initial Animation to the first frame and stopped.
****UPDATED***** This section was mistakenly ommited early.
Place this in HealthBar->Create Actor.
- Code: Select all
ChangeAnimation("Event Actor", "HB", STOPPED);
Sets the
HealthBar to not count down by itself.
Place this in HealthBar->Draw Actor.
- Code: Select all
DestroyActor("Goober");
DeadGoober.transp=0;
This code is used to Destroy the Actor:
Goober and make the dead actor:
DeadGoober visible
after HealthBar has reached frame 11.
Instead of clicking immediate action, choose wait for frame action.
Then in the popup window, enter: 11;
This tells it to wait until
HealthBar is displaying the 11th frame before executing this.
Place this in texthealth->Create Actor.
- Code: Select all
sprintf(texthealth.text,"%d%%",Goober.health);
This too prints to the text actor:
texthealth the
Goober's
health.
Place this in mouseweapon->Create Actor.
- Code: Select all
ChangeAnimation("Event Actor", "spikeball01", STOPPED);
FollowMouse("Event Actor", BOTH_AXIS);
This sets the initial animation of the spikeball actor:
mouseweapon to the clean frame.
The FollowMouse line makes the actor:
mouseweapon follow the mouse pointer, as if glued to it.
Place this in mouseweapon->Collision (Any Side of Goober).
- Code: Select all
mouseweapon.animpos=1;
HealthBar.animpos++;
if (Goober.health!=100)
{
Goober.health-=9;
}
else
{
Goober.health-=10;
}
sprintf(texthealth.text,"%d%%",Goober.health);
if(Goober.health>90)
{
Goober.animpos=0;
}
else if(Goober.health<91 && Goober.health>70)
{
Goober.animpos=1;
}
else if(Goober.health<71 && Goober.health>45)
{
Goober.animpos=2;
}
else
{
Goober.animpos=3;
}
There is alot here, so I will break it down in sections.
First: The
mouseweapon and
HealthBar animpos change the frames of both actors, the
HealthBar is simply incremented to the next while the
mouseweapon is set to a
specific one.
Second:The if.else statement for
Goober.health just decreases the health variable of Actor:
Goober.
I used this simply to make the health % display to come out evenly.
Third: The sprintf just updates the screen display of the textactor
texthealth with the new health value.
Fourth:The If.else if..else statements on the
Goober.health just change the
Goober's image to show damage
visually.
Place this in mouseweapon->Collision Finish(Goober).
- Code: Select all
mouseweapon.animpos=0;
This just sets the spikeball back to a clean image.(no blood and guts)
This should complete the detailed description of how to make a healthbar along with text display work,
along with making visual damage show on the attacked enemy. This is geared to all the new users who have had questions
on how to use the healthbar.
The actors used in this are:
mouseweapon(2 frame animation of type:weapon)
Goober (a 4 frame unfortunate soul who is doomed to die.)
DeadGoober (His one frame dead pool of goo.)
HealthBar (a 12 frame red,yellow,green indicator of certain doom for Goober.)
texthealth (a text equivalent to the HealthBar)
NOTE:Unfortunately, some Goobers were hurt to bring you this tutorial.