在其他类中使用代码

时间:2015-03-17 15:20:30

标签: c# class variables unity3d

我的代码在播放器类中完美运行,但是当我尝试将它放在不同的类中时,它什么都不做。我的球员不会失去生命,他也不会死。

这里不起作用:

Spikes class

using UnityEngine;
using System.Collections;

public class Spikes : MonoBehaviour
{
  SimplePlayer0 player = new SimplePlayer0();
  Animator anim;
  private bool isDead;

  void OnCollisionEnter2D(Collision2D other)
  {
    if (other.gameObject.tag == "spike" && !isDead && player.Lives <= 1 && !player.IsImmune)
    {
      player.Lives = 0;
      isDead = true;
    }
    else if (other.gameObject.tag == "spike" && player.Lives >= 1 && !player.IsImmune)
    {
      player.Lives--;
      anim.SetBool("Immume", true);
      player.IsImmune = true;
    }
  }




  void OnTriggerStay2D(Collider2D other)
  {
    if (other.tag == "spike" && !isDead && player.Lives <= 1 && !player.IsImmune)
    {
      player.Lives = 0;
      isDead = true;
    }

    else if (other.gameObject.tag == "spike" && player.Lives >= 1 && !player.IsImmune)
    {
      player.Lives--;
      anim.SetBool("Immume", true);
      player.IsImmune = true;
    }
  }


  void OnCollisionStay2D(Collision2D other)
  {
    if (other.gameObject.tag == "spike" && !isDead && player.Lives <= 1 && !player.IsImmune)
    {
      player.Lives = 0;
      isDead = true;
    }


    else if (other.gameObject.tag == "spike" && player.Lives > 1 && !player.IsImmune)
    {
      player.Lives--;
      anim.SetBool("Immune", true);
      player.IsImmune = true;
    }

    else if (other.gameObject.tag == "Underground")
    {
      isDead = true;
    }

  }

  void Dead()
  {
    if (isDead == true)
    {
      Application.LoadLevel(8);
    }
  }

}

玩家类

using UnityEngine;
using System.Collections;

public class SimplePlayer0 : MonoBehaviour
{
  Animator anim;



  //Lives
  private int lives = 3;
  private bool isImmune;
  public float immuneCounter;
  public float immuneTime;



  //PROPERTIES

  public int Lives 
  {
    get { return lives; }
    set { lives = value; }
  }

  public bool IsImmune
  {
    get { return isImmune; }
    set { isImmune = value; }
  }


  void Start ()
  {
    anim = GetComponent<Animator>();
  }

  void Update()
  {

    if (IsImmune)
    {
      immuneCounter -= Time.deltaTime;
    }
    if (immuneCounter <= 0)
    {
      IsImmune = false;
      immuneCounter = immuneTime;     
      anim.SetBool("Immume", false);
    }



}

这是它的原始代码:

using UnityEngine;
using System.Collections;

public class Player0 : MonoBehaviour
{
  Animator anim;


  //Lives
  public int lives = 3;
  public bool isImmune;
  public float immuneCounter;
  public float immuneTime;


  public bool isDead;



  void Start()
  {
    anim = GetComponent<Animator>();
  }


  void Update()
  {

    if (isImmune)
    {
      immuneCounter -= Time.deltaTime;
    }
    if (immuneCounter <= 0)
    {
      isImmune = false;
      immuneCounter = immuneTime;       
      anim.SetBool("Immume", false);
    }

    if (isDead == true)
    {

      Application.LoadLevel(8);

    }



  void OnCollisionEnter2D(Collision2D other)
  {
    if (other.gameObject.tag == "spike" && !isDead && lives <= 1 && !isImmune)
    {


      rigidbody2D.velocity = Vector2.zero;
      lives = 0;

      rigidbody2D.AddForce(new Vector2(0, 200)); // death animation
      isDead = true;


    }
    else if (other.gameObject.tag == "spike" && lives >= 1 && !isImmune)
    {
      lives--;
      anim.SetBool("Immume", true);
      isImmune = true;
    }
  }




  void OnTriggerStay2D(Collider2D other)
  {
    if (other.tag == "spike" && !isDead && lives <= 1 && !isImmune)
    {


      rigidbody2D.velocity = Vector2.zero;
      lives = 0;
      rigidbody2D.AddForce(new Vector2(0, 200));
      isDead = true;
    }
    else if (other.gameObject.tag == "spike" && lives >= 1 && !isImmune)
    {
      lives--;
      anim.SetBool("Immume", true);
      isImmune = true;
    }

    else if (other.gameObject.tag == "Underground")
    {
      rigidbody2D.AddForce(new Vector2(0, 200));
      isDead = true;
    }
  }




  void OnCollisionStay2D(Collision2D other)
  {
    if (other.gameObject.tag == "spike" && !isDead && lives <= 1 && !isImmune)
    {
      rigidbody2D.velocity = Vector2.zero;
      lives = 0;

      rigidbody2D.AddForce(new Vector2(0, 200));
      isDead = true;


    }
    else if (other.gameObject.tag == "spike" && lives > 1 && !isImmune)
    {
      lives--;
      anim.SetBool("Immune", true);
      isImmune = true;
    }
    else if (other.gameObject.tag == "Underground")
    {
      rigidbody2D.velocity = Vector2.zero;
      lives = 0;


      isDead = true;
    }

  }

}

2 个答案:

答案 0 :(得分:0)

我的猜测是你的&#34;播放器&#34;不会失去生命,因为你减去生命的对象不是你的玩家。在您的Spikes类中,您将创建一个全新的Player对象并减少该对象的生命,而不是您已有的对象。

我会做的是,有一个GameController脚本,它实例化播放器(以及可能需要的任何其他内容)。这样,你知道你只有一个玩家对象可以使用。

例如:

public static class GameController
{
    public static Player myOnlyPlayer; // either drag in the editor, or instantiate
}

然后您可以使用以下内容访问该对象:

public class Spike
{
    void OnCollisionEnter2D(Collision2D other)
    {
        // Do something with:
        GameController.myOnlyPlayer.ReduceLifeCount();
    }
}

希望这有帮助!

答案 1 :(得分:0)

首先 - 通过MonoBehaviour实例化new的子类本身就是一个错误 - 我指的是SimplePlayer0 player = new SimplePlayer0();中的Spikes.cs。不要那样做。

其次--LokiSinclair的答案可行,但使用全局变量来存储玩家并始终对其施加伤害是非常有限的。我们应该检查与尖峰相撞的内容,然后决定如何处理它(请注意,我的代码也不是真正优雅的解决方案):

using UnityEngine;
using System.Collections;

public class Spikes : MonoBehaviour
{
  private void ResolveDamage(Collision2D other){
    if (other.gameObject.tag == "player"){
      var player = other.gameObject.getComponent<SimplePlayer0>();
      if(!player.IsImmune){
        player.Lives--;  
      }
    }
  }

  void OnCollisionEnter2D(Collision2D other)
  {
    ResolveDamage(other);
  }

  void OnTriggerStay2D(Collision2D other)
  {
    ResolveDamage(other);
  }

  void OnCollisionStay2D(Collision2D other)
  {
   ResolveDamage(other);
  }
}

你的玩家逻辑应该包含在Player类中 - 这只是一个很好的设计原则:

using UnityEngine;
using System.Collections;

public class SimplePlayer0 : MonoBehaviour
{
  Animator anim;

  //Lives
  private int lives = 3;
  private bool isImmune;
  public float immuneCounter;
  public float immuneTime;

  //PROPERTIES


  //putting some logic in getters-setters of your properties
  public int Lives 
  {
    get { return lives; }
    set {
        if(lives != value){
            lives = value;
            if(IsDead){
                Dead();
            }else{
                IsImmune = true;                
            }
        }
    }
  }

  private void Dead()
  {
    Application.LoadLevel(8);
  }

  public bool IsImmune
  {
    get { return isImmune; }
    set { 
        if(isImmune != value){
            isImmune = value; 
            anim.SetBool("Immume", isImmune);
            //ternary operator - a shorthand for if-else statement
            immuneCounter = isImmune ? immuneTime : 0;
        }
    }
  }

  public bool IsDead{
    get{
        return Lives <= 0;
    }
  }

  void Start ()
  {
    anim = GetComponent<Animator>();
  }

  void Update()
  {
    if (IsImmune)
    {
      immuneCounter -= Time.deltaTime;
    }

    if (immuneCounter <= 0)
    {
      IsImmune = false; 
    }
}