放置调用重复的位置

本文关键字:位置 调用 | 更新日期: 2023-09-27 18:34:21

我有以下 C# 代码:

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);
}

我收到以下消息错误:

错误 CS1519:类、结构或接口成员声明中出现意外符号"Enemyspawn"

公共变量(动画器和刚体2d)设置正确

应该在哪里移动调用重复?我搜索了一些答案;我将 InvokeRepeating 移到了 EnemyStack 的开始和结尾。结果是每帧的敌人数量不断增加。这个问题的解决方案是什么?

放置调用重复的位置

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;
    }
}

正如你所说,它增加了敌人的生成,然后使用大的重复率,即调用重复方法的第三个参数。

如果你执行 InvokeRepeating("Function", 1.0f, 1.0f),它将在调用 InvokeRepeating 调用后一秒调用函数,然后每隔一秒调用一次