2012-05-27 74 views
0

我想从ClassLiteLaser类BlueShip中安装事件,所以基本上这个事件将被触发每n个时间单位。
这里是类BlueShip这是在统一“连接”到一个精灵:不能挂钩事件

using UnityEngine; 
using System.Collections.Generic; 
using System.Timers; 
using System; 

    public class BlueShip : MonoBehaviour 
    { 
     enum LaserTypes { BASIC_LASER }; 
     private float speed_ = 100f; 
     private List<GameObject> laser_ = new List<GameObject>(); 
     private GameObject currentLaser_; 
     private Transform transform_; 
     private Vector3 currentPosition_; 
     private bool isElapsed_ = true; 
     // Use this for initialization 
     void Start() 
     { 
      transform_ = transform; 
      /*Basic laser is the default and it's guaranteed to be installed at the beginning of the game*/ 
      laser_.Add((GameObject)Resources.Load("BasicLaser")); 
      currentLaser_ = laser_[(int)LaserTypes.BASIC_LASER]; 
      laser_[0].GetComponent<BasicLaser>().timerElapsed += new TimerElapsed (BasicLaser_timerElapsed);  
//HERE I'M adding observer to this event but look for line named *2 in class BasicLaser 


     } 

     public void BasicLaser_timerElapsed(object sender, EventArgs e) 
     {/*this is supposed to react to event timerElapsed in BasicLaser*/ 
      isElapsed_ = true; 
     } 


     // Update is called once per frame 
     void Update() 
     { 
      var amtToMove = Input.GetAxis("Horizontal") * speed_ * Time.deltaTime; 
      transform_.Translate(Vector3.right * amtToMove); 
      /*Those below set the position for laser to be instantiated at*/ 
      currentPosition_.x = transform_.position.x; 
      currentPosition_.y = transform_.position.y + (this.transform.localScale.y/2) + currentLaser_.transform.localScale.y; 
      currentPosition_.z = transform_.position.z; 


      if (Input.GetKeyDown(KeyCode.UpArrow)) 
      { 
       if (isElapsed_) 
       { 
        var t = Instantiate(laser_[(int)LaserTypes.BASIC_LASER], currentPosition_, Quaternion.identity); 
        isElapsed_ = false; 
       } 

      } 

     } 
    } 


//BasicLaser class which is "connected" to a BasicLaser prefab in unity 
    using UnityEngine; 
    using System.Collections; 
    using System.Diagnostics; 
    using System; 
    public delegate void TimerElapsed(object sender,EventArgs e); 
     public class BasicLaser : MonoBehaviour 
     { 

      private float speed_ = 500f; 
      private static uint frequency_ = 2;//the frequency with which this laser can be fired 
      private static Stopwatch stopWatch_ = new Stopwatch(); 

      public event TimerElapsed timerElapsed;//HERE IS THE EVENT 

      public uint Frequency 
      { 
       get { return frequency_; } 
       set { frequency_ = value; } 
      } 

      private Transform transform_; 
      // Use this for initialization 
      void Start() 
      { 
       transform_ = transform; 
       stopWatch_.Start(); 

      } 



      // Update is called once per frame 
      void Update() 
      { 
       var amtToMove = speed_ * Time.deltaTime; 
       transform_.Translate(Vector3.up * amtToMove); 
      var t = stopWatch_.Elapsed.Milliseconds; 
       if (t > frequency_) 
       { 
        stopWatch_ = new Stopwatch(); 
        stopWatch_.Start(); 
        if (timerElapsed != null)//*2 THIS IS ALWAYS NULL!!! 
    even though I've hooked it in BlueShip class, what's going on? 

        { 
         timerElapsed(this, EventArgs.Empty);//raises the event 
        } 
       } 

       if (transform.position.y >= Screen.height) 
       { 
        Destroy(gameObject); 
       } 
      } 
     } 
+0

呃,你刚才的问题是什么? –

+0

#TonyHopkinson问题是,为什么我在BasicLaser中声明的timerElapsed事件总是为null,即使我在BlueShip类中将一个观察者连接到此事件。对不起,不清楚。 – smallB

+2

你有附加一个调试器,并通过这一步? – Paolo

回答

1

请检查控制台或日志文件,并告诉我们,如果你要在BlueShipStart方法的任何错误。同样使用Debug.Log并查看日志文件,以查看是否有任何对象在它们不应该为空时为空。 (如Debug.Log (currentLaser_);

我觉得这是你的问题:

(GameObject)Resources.Load("BasicLaser") 

这使你的目标在你的资产被称为“BasicLaser” /资源文件夹,(假设它存在的话)可能是一个预制。您可以在这里获得的GameObject与您在场景中可能拥有的GameObject不一样。您应该被实例化这种预制,然后连接到该实例化对象的事件,或者你应该是指已经存在于场景中,通过改变线路到这个BasicLaserlaser_.Add((GameObject)GameObject.Find ("BasicLaser"));(如果GameObjectBasicLaser组件名称是"BasicLaser")。