|
Java Code - Heart Bar
[Back]
Code:
//There are three scripts, you're going to need for this
//Code 1 will be on the heart actor
#pragma strict
var whatheart: int = 1; //What Heart
var sprite1: Sprite; //Hold a Sprite Image
var sprite2: Sprite;
function Start () {
}
function Update () {
//What Heart
if (whatheart == 1) {
if (Hp.health >= 1) {
GetComponent(SpriteRenderer).sprite = sprite1; //Change the sprite of the heart to red heart image
}
if (Hp.health <= 0) {
GetComponent(SpriteRenderer).sprite = sprite2; //Change the sprite of the heart to gray heart image
}
}
if (whatheart == 2) {
if (Hp.health >= 2) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 1) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
if (whatheart == 3) {
if (Hp.health >= 3) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 2) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
if (whatheart == 4) {
if (Hp.health >= 4) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 3) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
if (whatheart == 5) {
if (Hp.health >= 5) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 4) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
}
//Code 2 will be on the player actor, I named this script Hp, if you look on the top script you will see
//if (Hp.health) what the Hp.health means it's taking the var from the script Hp
//So if you were going to change the name to Healthbar you would need to change
//if (Healthbar.health) to make it work
#pragma strict
static var health = 5; //How much hp
//Here are some other codes you might want for script 2, it's good for test play
function Update () {
Debug.Log(health); //draw the health var in the Console
if (Input.GetKeyDown ("w")) //The w is what key is down
{
if (health <= 4) {
health = health +1; //Give you Hp 1 +
}
}
//The, if (health <= 4) {, means if health is less then or equal to 4 it will work if it passed 4 then it will not give you more hp
if (Input.GetKeyDown ("s")) //The w is what key is down
{
health = health -1; //Give you Hp 1 -
}
}
//Code 3 will be on the player
if (Hp.health <= 0) {
Destroy(gameObject);
}
//this will make it so if health is less then or equal to 0 the player will be destroyed
//Code 1 will be on the heart actor
#pragma strict
var whatheart: int = 1; //What Heart
var sprite1: Sprite; //Hold a Sprite Image
var sprite2: Sprite;
function Start () {
}
function Update () {
//What Heart
if (whatheart == 1) {
if (Hp.health >= 1) {
GetComponent(SpriteRenderer).sprite = sprite1; //Change the sprite of the heart to red heart image
}
if (Hp.health <= 0) {
GetComponent(SpriteRenderer).sprite = sprite2; //Change the sprite of the heart to gray heart image
}
}
if (whatheart == 2) {
if (Hp.health >= 2) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 1) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
if (whatheart == 3) {
if (Hp.health >= 3) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 2) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
if (whatheart == 4) {
if (Hp.health >= 4) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 3) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
if (whatheart == 5) {
if (Hp.health >= 5) {
GetComponent(SpriteRenderer).sprite = sprite1;
}
if (Hp.health <= 4) {
GetComponent(SpriteRenderer).sprite = sprite2;
}
}
}
//Code 2 will be on the player actor, I named this script Hp, if you look on the top script you will see
//if (Hp.health) what the Hp.health means it's taking the var from the script Hp
//So if you were going to change the name to Healthbar you would need to change
//if (Healthbar.health) to make it work
#pragma strict
static var health = 5; //How much hp
//Here are some other codes you might want for script 2, it's good for test play
function Update () {
Debug.Log(health); //draw the health var in the Console
if (Input.GetKeyDown ("w")) //The w is what key is down
{
if (health <= 4) {
health = health +1; //Give you Hp 1 +
}
}
//The, if (health <= 4) {, means if health is less then or equal to 4 it will work if it passed 4 then it will not give you more hp
if (Input.GetKeyDown ("s")) //The w is what key is down
{
health = health -1; //Give you Hp 1 -
}
}
//Code 3 will be on the player
if (Hp.health <= 0) {
Destroy(gameObject);
}
//this will make it so if health is less then or equal to 0 the player will be destroyed