2011-06-24 3 views
0

Tout comme le nom l'indique, je souhaite que pour chaque nom défini dans un tableau, une valeur soit ajoutée à un int. Par exemple: s'il y a 3 chaînes du même nom dans le tableau, alors 3 fois 50 seront ajoutées à la valeur.Pour chaque chaîne dans Array

Ceci est mon script que j'ai maintenant:

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; 
    } 
} 

Il est un jeu que je fais où vous pouvez voler des objets pour marquer des points supplémentaires.

J'ai essayé d'utiliser le for(i = 0; i < variable.Length; i++) mais cela ne semblait pas fonctionner.

La seule chose que je peux penser maintenant est d'utiliser des booléens pour l'ajouter une fois. Mais ce n'est pas la mémoire amicale.

L'aide est appréciée et merci d'avance!

+1

Et où la boucle doit être? Nous ne savons pas comment votre script est censé fonctionner. Tout le code est-il nécessaire pour le problème? –

Répondre

1

Vous pouvez utiliser la méthode .forEach(callback) standard:

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

Si vous ne possédez pas cette méthode, vous pouvez le mettre en œuvre comme ceci:

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