0

名前が示すように、配列内の特定の名前ごとに値がintに追加されるようにします。

例:配列に同じ名前の文字列が3つある場合、50の3倍が値に追加されます。

これは私が今持っている私のスクリプトです:

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 に答える