【问题标题】:For each string in Array对于 Array 中的每个字符串
【发布时间】:2011-09-21 21:32:18
【问题描述】:

正如名称所说,我希望为数组中的每个特定名称添加一个值到 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++),但这似乎不起作用。

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

感谢您的帮助,并在此先感谢您!

【问题讨论】:

  • 循环应该在哪里?我们不知道您的脚本应该如何工作。是否所有代码都需要解决问题?

标签: arrays string unity3d unityscript


【解决方案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); }
    }
}

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多