2016-02-12 117 views
0

我有以下的C#代碼:哪裏可以放置InvokeRepeating?

using UnityEngine; 
using System.Collections; 

public class Hero : MonoBehaviour { 
    float speed = 2f; 
    bool hero_up = false; 
    bool hero_down = false; 
    bool hero_left = false; 
    bool hero_right = false; 
    public Animator animator; 
    public Rigidbody2D rbEnemy; 

    // Use this for initialization 
    void Start() 
    { 
     animator = GetComponent<Animator>(); 
     EnemySpawn(); 
    } 

    void EnemySpawn() 
    { 
     Rigidbody2D EnemyInstance; 
     EnemyInstance = Instantiate(rbEnemy, new Vector3(Random.Range (2f, 8f), Random.Range (-4f, 4f) ,0f), Quaternion.Euler(new Vector3(0f,0f,0f))) as Rigidbody2D; 

    } 

    // Update is called once per frame 
    InvokeRepeating("EnemySpawn", 3, 3); 
} 

我收到以下消息的錯誤:

error CS1519: Unexpected symbol `EnemySpawn' in class, struct, or interface member declaration

公共變量(動畫和Rigidbody2d)都設置正確

應該在哪裏移動InvokeRepeating?我搜索了一些答案;我在開始和EnemySpawn結束時移動了InvokeRepeating。結果是每幀的敵人越來越多。 這個問題的解決方案是什麼?

+0

其中'InvokeRepeating'其實叫什麼名字?它看起來不像它的方法。 –

+0

把它放在'Start()'裏面 – Fattie

回答

1
using UnityEngine; 
using System.Collections; 

public class Hero : MonoBehaviour { 
    float speed = 2f; 
    bool hero_up = false; 
    bool hero_down = false; 
    bool hero_left = false; 
    bool hero_right = false; 
    public Animator animator; 
    public Rigidbody2D rbEnemy; 

    // Use this for initialization 
    void Start() 
    { 
     animator = GetComponent<Animator>(); 

    // Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds. 
    InvokeRepeating("EnemySpawn", 3, 3); 

    } 

    void EnemySpawn() 
    { 
     Rigidbody2D EnemyInstance; 
     EnemyInstance = Instantiate(rbEnemy, new Vector3(Random.Range (2f, 8f), Random.Range (-4f, 4f) ,0f), Quaternion.Euler(new Vector3(0f,0f,0f))) as Rigidbody2D; 

    } 

} 

如u表示,那麼多增加的敵人產卵,然後,用大repeatedRate即InvokeRepeating方法的第三個參數。

如果你InvokeRepeating(「功能」,1.0F,1.0F),它會調用函數InvokeRepeating電話後一秒鐘,然後每隔一秒之後

相關問題