使用for循环生成id来创建怪物

时间:2013-11-12 10:25:53

标签: c# for-loop unity3d

我是游戏编程和C#的新手,但我有一些javaScript和PHP的编程经验。

好的,我们去了,我有一个C#脚本,我想用它来生成怪物。我在Youtube上关注了Tom Adamson,直到他开始生成随机值:UNITY3D C# with Tom Adamson

这是我的剧本:

using UnityEngine;
using System.Collections;

public class myMonster : MonoBehaviour {

    public class aMonster {

        //The properties
        public int id;
        public int age;
        public string name;
        public string race;
        public int health;

        //A method
        public void monsterData() {
            print("ID: "        + id);
            print("Race: "      + race);
            print("Name: "      + name);
            print("Age: "       + age);
            print("Health: "    + health);
        }

    } // End of class definition

//-----------------------------------------------------------------------------------------

    void Start () {

        aMonster[] bigMonster = new aMonster[51];

        for (int i = 1; i <= 50;) {

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Gorky";
            bigMonster[i].race = "Orc";
            bigMonster[i].age = 320;
            bigMonster[i].health = 200;
            i++;

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Runathu";
            bigMonster[i].race = "Shaman";
            bigMonster[i].age = 670;
            bigMonster[i].health = 100;
            i++;

        }

        for (int i = 1; i <= 2; i++) {
            bigMonster[i].monsterData();
        }

    }

}

当我只有2个怪物时,这个工作正常,但是当我尝试添加第三个怪物时我得到了这个错误:

IndexOutOfRangeException:数组索引超出范围。 (wrapper stelemref)对象:stelemref(object,intptr,object) myMonster.Start()(在Assets / myMonster.cs:50)

我添加了第三个怪物:

bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Gorky";
            bigMonster[i].race = "Orc";
            bigMonster[i].age = 320;
            bigMonster[i].health = 200;
            i++;

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Runathu";
            bigMonster[i].race = "Shaman";
            bigMonster[i].age = 670;
            bigMonster[i].health = 100;
            i++;

            bigMonster[i] = new aMonster();
            bigMonster[i].id = i;
            bigMonster[i].name = "Tiny";
            bigMonster[i].race = "Spider";
            bigMonster[i].age = 90;
            bigMonster[i].health = 45;
            i++;

有谁能告诉我我做错了什么?我猜我认为i ++是错误的做法,因为第三个怪物导致错误。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:8)

aMonster[] bigMonster = new aMonster[51];

表示你有51个怪物(最大指数= 50),但你在一个for循环中递增两次,在最后一次迭代时i = 50,所以你试图达到aMonster[51]

FIX:

从i = 0开始你的循环并在i = 49结束,c#中的索引是从0开始而不是1

我还建议您将代码转换为:

    for (int i = 0; i < 50; i+=2) 
    {

        bigMonster[i] = new aMonster();
        bigMonster[i].id = i;
        bigMonster[i].name = "Gorky";
        bigMonster[i].race = "Orc";
        bigMonster[i].age = 320;
        bigMonster[i].health = 200;


        bigMonster[i+1] = new aMonster();
        bigMonster[i+1].id = i;
        bigMonster[i+1].name = "Runathu";
        bigMonster[i+1].race = "Shaman";
        bigMonster[i+1].age = 670;
        bigMonster[i+1].health = 100;

    }

for循环的递增应该在for循环的定义中完成,它看起来更干净。

编辑:

最优雅,最安全的解决方案,请使用List<aMonster>()

var bigMonster = new List<aMonster>();
var id = 0;
for(int i=0; i<30; i++)
{
    bigMonster.Add(new aMonster { id=id++,name="Gorky",race="Orc",age=320,health=200 });
    bigMonster.Add(new aMonster { id=id++,name="Runathu",race="Shaman",age=320,health=200 });
    //and so on
}

它将创造30种各种怪物,当然你可以通过modyfing for loop来改变这个数字

答案 1 :(得分:0)

从0开始迭代

for (int i = 1; i <= 50;) 

答案 2 :(得分:0)

为了使事情变得更容易,我将使用List而不是数组。符号也会更容易,例如:

var bigMonster = new list<aMonster>();

for(int i = 1; i <=50; )
{
  var mon1 = new aMonster{id = i, name = "Gorky", race = "orc", age = 320, health = 200};
        i++;
  var mon2 = new aMonster{id = i, name = "Runathu", race = "Shaman", age = 670, health = 100};
        i++;
  var mon3 = new aMonster{id = i, name = "Tiny", race = "Spider", age = 90, health = 45};
        i++;
  bigMonster.Add(mon1);
  bigMonster.Add(mon2);
  bigMonster.Add(mon3);
}

foreach(aMonster a in bigMonster)
{
  a.monsterData();
}