创建数组?

时间:2014-11-03 20:35:02

标签: arrays xna storage

我有这个课,但我想把它变成2D维数组。而且我不知道该怎么做...每个人都告诉我创建2D数组,这样我就可以轻松地调用它并读取数组......但我发现这种制作方式。 现在存储这个时间的数组或网格将是有帮助的。 将此类传输到数组的任何代码都会有所帮助。

武器基础是继承2个额外的类。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace RPG
    {
    class WeaponList
    {

        private WeaponBase WoodenSword;
        private WeaponBase RustySword;
        private WeaponBase IronSword;
        private WeaponBase SteelSword;
        private WeaponBase BoardSword;
        private WeaponBase MythrilSword;
        private WeaponBase BloodSword;
        private WeaponBase CoralSword;
        private WeaponBase AncientSword;


        public void CreateWeapon()
        {
            WoodenSword = new WeaponBase();
            WoodenSword.ItemName = "Wooden Sword";
            WoodenSword.ItemDescription = "A basic traning sword...can do some damage";
            WoodenSword.IsStackable = true;    
            WoodenSword.Attack = 4;
            WoodenSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            WoodenSword.ItemID = 1;
            WoodenSword.Price = 10;

            RustySword = new WeaponBase();
            RustySword.ItemName = "Rysty Sword";
            RustySword.ItemDescription = "Old rusty sword....still better than wooden one";
            RustySword.Attack = 5;
            RustySword.IsStackable = true;
            RustySword.WeaponType = WeaponBase.WeaponTypes.Sword;
            RustySword.ItemID = 2;
            RustySword.Price = 50;

            IronSword = new WeaponBase();
            IronSword.ItemName = "Iron Sword";
            IronSword.ItemDescription = "This sword has a broad and sturdy blade, but its iron construction makes it very heavy.";
            IronSword.IsStackable = true;
            IronSword.Attack = 6;
            IronSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            IronSword.ItemID = 3;
            IronSword.Price = 100;

            SteelSword = new WeaponBase();
            SteelSword.ItemName = "Steel Sword";
            SteelSword.ItemDescription = "Hardend version of iron sword";
            SteelSword.IsStackable = true;
            SteelSword.Attack = 7;
            SteelSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            SteelSword.ItemID = 4;
            SteelSword.Price = 500;

            BoardSword = new WeaponBase();
            BoardSword.ItemName = "Borad Sword";
            BoardSword.ItemDescription = "This broad-bladed sword is suited for large slashing strokes. It is inexpensive, but not particularly powerful. ";
            BoardSword.IsStackable = true;
            BoardSword.Attack = 8;
            BoardSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            BoardSword.ItemID = 5;
            BoardSword.Price = 800;

            MythrilSword = new WeaponBase();
            MythrilSword.ItemName = "Mythril Sword";
            MythrilSword.ItemDescription = "A sword forged from the metal known as mythril. Its brilliantly shining blade is incredibly lightweight.";
            MythrilSword.IsStackable = true;
            MythrilSword.Attack = 10;
            MythrilSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            MythrilSword.ItemID = 6;
            MythrilSword.Price = 1600;

            BloodSword = new WeaponBase();
            BloodSword.ItemName = "Blood Sword";
            BloodSword.ItemDescription = "The blade of this sword is a deep crimson, as if it were drenched in blood. It is cruelly sharp.";
            BloodSword.IsStackable = true;
            BloodSword.Attack = 8;
            BloodSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            BloodSword.ItemID = 7;
            BloodSword.Price = 1400;
            BloodSword.specialAttack = "Drains Foe's HP";

            CoralSword = new WeaponBase();
            CoralSword.ItemName = "Coral Sword";
            CoralSword.ItemDescription = "The handle of this single-edged sword has been decorated with intricate coral piecework.";
            CoralSword.IsStackable = true;
            CoralSword.Attack = 11;
            CoralSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            CoralSword.ItemID = 8;
            CoralSword.Price = 2200;
            CoralSword.specialAttack = "Element: Thunder";

            AncientSword = new WeaponBase();
            AncientSword.ItemName = "Ancient Sword";
            AncientSword.ItemDescription = "A sword constructed using ancient techniques that have long since perished from the world.";
            AncientSword.IsStackable = true;
            AncientSword.Attack = 11;
            AncientSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            AncientSword.ItemID = 9;
            AncientSword.Price = 3300;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

嗯,我不确定你做了什么,但制作2D数组非常简单:

int[,] MyArray = new int[3,4];
// Access like this:
MyArray[0,0] = 1;
MyArray[0,1] = 2;
MyArray[0,2] = 3;
// Etc...

// To Populate using a For Loop:
int i = 0;
for(int x=0; x < 3; x++)
    for(int y=0; y < 4; y++)
    {
        i++;
        MyArray[x, y] = i;
    }

这就是你制作2D阵列的方法。

编辑:

为课堂制作它是一个类似的过程:

MyClass[,] MyArray = new MyClass[3,4];
// Access like this:
MyClass[0,0] = new MyClass();
MyClass[0,1] = new MyClass();
MyClass[0,2] = new MyClass();
// Etc...

// To Populate using a For Loop:
for(int x=0; x < 3; x++)
    for(int y=0; y < 4; y++)
    {
        MyClass[x, y] = new MyClass();
    }

正如您所看到的,为类创建2D数组几乎是一样的。

答案 1 :(得分:0)

我建议这样(vb-pseudo code)。

Class WeaponBase
    ItemName
    ItemDescription
    IsStackable
    ...
End Class

Class Weapons
    inherits list (of WeaponBass)
    Sub CreateWeapon
        ' here i will read data from XML or JSON instead of this below

         w = new WeaponBase();
         w.ItemName = "Wooden Sword";
         w.ItemDescription = "A basic traning sword...can do some damage";
         w.IsStackable = true; 
         add(w)   

         w = new WeaponBase();
         w.ItemName = "Metal Sword";
         w.ItemDescription = "A basic traning sword...can do some damage";
         w.IsStackable = true; 
         add(w)

    End Sub

    Sub CreateNewItem(_name, _desc, _stackable)
         w = new WeaponBase();
         w.ItemName = _name;
         w.ItemDescription = _desc;
         w.IsStackable = _stackable; 
         add(w)
    End Sub

End Class

现在你有一份武器清单,你可以轻松访问并按照自己的意愿行事。