0

正如名称所说,我希望为数组中的每个特定名称添加一个值到 int。

例如:如果数组中有 3 个同名字符串,则将 3 乘以 50 加到该值中。

这是我现在拥有的脚本:

var lootList = new Array();
var interaction : Texture;
var interact = false;
var position : Rect;
var ching : AudioClip;
var lootPrice = 0;

function Update()
{
    print(lootList);

    if ("chalice" in lootList){
        lootPrice += 50;
    }
}

function Start()
{
    position = Rect( ( Screen.width - interaction.width ) /2, ( Screen.height - interaction.height ) /2, interaction.width, interaction.height );
}

function OnTriggerStay(col : Collider)
{   
    if(col.gameObject.tag == "loot")
    {
        interact = true;

        if(Input.GetKeyDown("e"))
        {
            if(col.gameObject.name == "chalice")
            {
                Destroy(col.gameObject);
                print("chaliceObtained");
                audio.clip = ching;
                audio.pitch = Random.Range(0.8,1.2);
                audio.Play();
                interact = false;
                lootList.Add("chalice");
            }

            if(col.gameObject.name == "moneyPouch")
            {
                Destroy(col.gameObject);
                print("moneyPouchObtained");
                audio.clip = ching;
                audio.pitch = Random.Range(0.8,1.2);
                audio.Play();
                interact = false;
                lootList.Add("moneyPouch");
            }

            if(col.gameObject.name == "ring")
            {
                Destroy(col.gameObject);
                print("ringObtained");
                audio.clip = ching;
                audio.pitch = Random.Range(0.8,1.2);
                audio.Play();
                interact = false;
                lootList.Add("ring");
            }

            if(col.gameObject.name == "goldCoins")
            {
                Destroy(col.gameObject);
                print("coldCoinsObtained");
                audio.clip = ching;
                audio.pitch = Random.Range(0.8,1.2);
                audio.Play();
                interact = false;
                lootList.Add("goldCoins");
            }

            if(col.gameObject.name == "plate")
            {
                Destroy(col.gameObject);
                print("plateObtained");
                audio.clip = ching;
                audio.pitch = Random.Range(0.8,1.2);
                audio.Play();
                interact = false;
                lootList.Add("plate");
            }
        }
    }
}

function OnTriggerExit(col : Collider)
{   
    if(col.gameObject.tag == "pouch")
    {
        interact = false;
    }
}

function OnGUI()
{
    if(interact == true)
    {
        GUI.DrawTexture(position, interaction);
        GUI.color.a = 1;
    }
}

这是我正在制作的一款游戏,您可以在其中窃取物品以获得额外的分数。

我试过使用,for(i = 0; i < variable.Length; i++)但似乎没有用。

我现在唯一能想到的就是使用布尔值添加一次。但这对内存不友好。

帮助表示赞赏,并提前感谢!

4

1 回答 1

1

您可以使用标准.forEach(callback)方法:

lootList.forEach(function(value, index, array)
{
    if (value === "chalice") { lootPrice += 50; }
});

如果你没有那个方法,你可以像这样实现它:

if (!Array.prototype.forEach) {
    Array.prototype.forEach = function (callback) {
        for(var i = 0; i < this.length; i++) { callback(this[i], i, this); }
    }
}
于 2011-06-24T09:50:28.020 回答