简体   繁体   中英

Add extra life HEART texture 2D

I am working on 2D game.

When player collides with BOMBPrefab lose 1 heart (initial 3 heart), if collides 3 times = GameOver.

Thats is the code (works fine for BombPrefab):

using UnityEngine;
using System.Collections;

public class Heart : MonoBehaviour {
  public Texture2D[] initialHeart;
  private int heart;
  private int many;

  // Use this for initialization
  void Start () {
    GetComponent<GUITexture>().texture = initialHeart[0];
    heart = initialHeart.Length;
  }

  // Update is called once per frame
  void Update () {}

  public bool TakeHeart()
  {
    if (heart < 0) {
      return false;
    }
    if (many < (heart - 1)) {
      many += 1;
      GetComponent<GUITexture> ().texture = initialHeart [many];
      return true;
    } else {
      return false;
    }
  }
}

I have a second HeartPrefab , I want to check when player collides... IF I have 3 hearts do nothing, IF have 1 or 2 hearts ADD extra heart.

BombPrefab Scrip:

   using UnityEngine;
using System.Collections;

public class Bomb : MonoBehaviour
{


    public AudioClip clipBomba;

    private Heart heart;
    private Gerenciador gerenciador;

    void Awake ()
    {


    }

    // Use this for initialization
    void Start ()
    {
        gerenciador = FindObjectOfType (typeof(Gerenciador)) as Gerenciador;
    }

    // Update is called once per frame
    void Update ()
    {



    }

    void OnCollisionEnter2D (Collision2D colisor)
    {
        if (colisor.gameObject.tag == "floor") {

            Destroy (gameObject, 2f);

        } else {

            if (colisor.gameObject.tag == "Bee") {

                Som();

                heart= GameObject.FindGameObjectWithTag ("Heart").GetComponent<Vidas> () as Vidas;

                if (heart.TakeHeart ()) {
                    Destroy (gameObject);

                } else {

                    gerenciador.GameOver ("GameOver");

                }
            }
        }
    }


    void Som()
    {

        GetComponent<AudioSource> ().clip = clipBomb;
        AudioSource.PlayClipAtPoint (clipBomb, transform.position);
    }
}

First of all try not use GetComponent<>() in update and functions that are run often. It is better to cache components in Start() method. Using FindGameObjectWith...() is also not very efficient.

I would make it differently, each heart as a separate UI element (Toggle), with two states active and inactive. Then your player should have a list of those hearts and methods that allow to take damage and gain life.

int currentHeartsCount = 3;

int maxHeartsCount = 3;

List<Heart> hearts = new List<Heart>();

public void TakeDamage(int damage) {
    ChangeHearts(-damage);
} 

public void GainLife(int life) {
    ChangeHearts(life);   
}

private void ChangeHearts(int amount) {
    currentHeartsCount += amount;
    if(currentHeartsCount> maxHeartsCount)
        currentHeartsCount = maxHeartsCount;
    if(currentHeartsCount<=0) {
        // call player dead code
    }
    int i = 1;
    foreach(Heart heart in hearts) {
        heart.SetHeartActive(i<=currentHeartsCount);
        i++;
    }
}

This is a simplified solution to give you an idea. It is more robust than yours because you can easily change start heart count, and make things like heart "overcharge". Just add base hearts, set max to desired value, and now you can create some special power ups which increase hearts over the initial limit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM