【问题标题】:Where to place InvokeRepeating?在哪里放置 InvokeRepeating?
【发布时间】:2016-02-13 16:23:43
【问题描述】:

我有以下 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”

正确设置公共变量(Animator 和 Rigidbody2d)

应该将 InvokeRepeating 移到哪里?我搜索了一些答案;我将 InvokeRepeating 移到 Start 和 EnemySpawn 的末尾。结果是每帧的敌人数量越来越多。 这个问题的解决方法是什么?

【问题讨论】:

  • InvokeRepeating 实际调用在哪里?它看起来不像它的方法。
  • 放入Start()

标签: c# unity3d


【解决方案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;

    }

}

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

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

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 2018-03-27
    • 2011-02-15
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    相关资源
    最近更新 更多