2011-06-24 4 views
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++)を試してみましたが、うまくいかなかったようです。

私が今考えている唯一のことは、ブーリアンを使用して1回追加することだけです。しかし、それは記憶に優しいものではありません。

ご協力いただきありがとうございます。

+1

、どこループは次のようになります。あなたはその方法を持っていない場合、あなたはこのようにそれを実装することができ

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

を?あなたのスクリプトがどのように動作するはずです。問題にはすべてのコードが必要ですか? –

答えて

1

あなたは標準.forEach(callback)方法使用できます。

if (!Array.prototype.forEach) { 
    Array.prototype.forEach = function (callback) { 
     for(var i = 0; i < this.length; i++) { callback(this[i], i, this); } 
    } 
} 
関連する問題