我如何正确地遍历这个对象 AS3

How do I iterate through this object properly AS3

所以我制作了一个对象数组(?),如下所示:

public var object:Object = 
{
    "part1" : {
        "text" : "Your name is Richard, a 20-something year old construction worker who just returned home from a night of heavy drinking. The clock above your sofa reads 9:37PM. What do you do?",
        "choices" : {
            "response1" : {
                "text" : "Go to sleep", 
                "nextPart" : "part2" 
            },
            "response2" : {
                "text" : "Watch TV",
                "nextPart" : "part3" 
            }
        }
    },
    "part2" : {
        "text" : "You go to sleep. You are awoken by the sound of glass smashing outside your house. The time on your alarm reads 12:30AM",
        "nextPart" : "part3"
    },
    "part3" : {
        "text" : "You sit down and start watching TV. All channels seem to be the same thing, a breaking news report. You tune in.",
        "nextPart" : "part4"
    },
    "part4" : 
        {
            "text" : "The reporter on the screen has a panicked tone, which you seem to think is unprofessional. You are horrified by what you hear; people are turning into disgusting deformed mutants that possess super human abilities. You are interrupted by someone jumping through your front window. How rude.",
            "nextPart" : "part5"
        },
    "part5" : {
        "text" : "You get up from your sofa and go towards the dickhead who jumped through your window. He turns out to be a dead body that was acutally thrown through the window. He's missing an eye and large portion of his skull. You look up to see a disfigured man standing outside your window. His face has swollen to 3x it's original size and covers his neck. He climbs through your window. You noticed acidic green liquid dripping from orifices on his face.",
        "nextPart" : "part6"
    }
}

我需要遍历 part1 的选择。

我目前正在尝试这个:

for (var s:String in object[curPart]["choices"]) i++
{
    trace(i)
    textFinished = false
    var txt:TextField = new TextField();
    txt.defaultTextFormat = new TextFormat('Verdana',15,0xFFFFFF);
    txt.text = String(i)
    txt.filters = [stroke];
    txt.autoSize = TextFieldAutoSize.LEFT;
    txt.selectable = false;
    txt.width = 400
    txt.height = 25
    var btn:Sprite = new Sprite();
    btn.mouseChildren = false;
    btn.addChild(txt); 
    btn.buttonMode = true;
    btn.x = stage.stageWidth / 10
    btn.y = stage.stageHeight / 2 - 50 * (i * .5)
    btn.name = "part" + String((Number(Number(curPart.substring(4))+1) + (i+1)))
    buttonHolder.addChild(btn);
    btn.addEventListener(
        MouseEvent.CLICK, 
        function m(zen:MouseEvent):void // when button is clicked 
        {
            //trace(buttonHolder.numChildren)
            //choice(zen.currentTarget.name,buttonHolder);
        } 
    )

    // Each button is put in the list,
    // to be able to work with the button after
}

但我只得到 1 个文本字段,上面写着 2(尽管应该有 2 个文本字段为 1、2)

根据您发布的代码,我猜测您需要实现几个循环:

  1. 通过 "main" 对象 的循环,它由 "part objects".
  2. 组成
  3. 循环遍历每个 "part objects"

我的意思是:

var object:Object = 
{
    "part1" : {
        "text" : "Your name is Richard, a 20-something year old construction worker who just returned home from a night of heavy drinking. The clock above your sofa reads 9:37PM. What do you do?",
        "choices" : {
            "response1" : {
                "text" : "Go to sleep", 
                "nextPart" : "part2" 
            },
            "response2" : {
                "text" : "Watch TV",
                "nextPart" : "part3" 
            }
        }
    },
    "part2" : {
        "text" : "You go to sleep. You are awoken by the sound of glass smashing outside your house. The time on your alarm reads 12:30AM",
        "nextPart" : "part3"
    },
    "part3" : {
        "text" : "You sit down and start watching TV. All channels seem to be the same thing, a breaking news report. You tune in.",
        "nextPart" : "part4"
    },
    "part4" : 
        {
            "text" : "The reporter on the screen has a panicked tone, which you seem to think is unprofessional. You are horrified by what you hear; people are turning into disgusting deformed mutants that possess super human abilities. You are interrupted by someone jumping through your front window. How rude.",
            "nextPart" : "part5"
        },
    "part5" : {
        "text" : "You get up from your sofa and go towards the dickhead who jumped through your window. He turns out to be a dead body that was acutally thrown through the window. He's missing an eye and large portion of his skull. You look up to see a disfigured man standing outside your window. His face has swollen to 3x it's original size and covers his neck. He climbs through your window. You noticed acidic green liquid dripping from orifices on his face.",
        "nextPart" : "part6"
    }
}

var partObject:Object;
// The first loop gives us an access to "part objects",
// which exists inside of the main object
for(var propName:String in object)
{
    partObject = object[propName];
    // For getting access to the data inside of "part objects",
    // we need to initiate the second loop
    for(var partObjectPropName:String in partObject)
    {
        trace("partObject[\"" + partObjectPropName + "\"]: " + partObject[partObjectPropName]);
    }
}

您的问题在这里:

for (var s:String in object[curPart]["choices"]) i++

i++是在for循环中执行的,其他都是运行一次。 将 i++ 移到循环的大括号内,一切都会正常进行。

for (var s:String in object[curPart]["choices"])
{
    i++; // Increase counter here
    trace(i)
    textFinished = false
    var txt:TextField = new TextField();
    txt.defaultTextFormat = new TextFormat('Verdana',15,0xFFFFFF);
    txt.text = String(i)
    txt.filters = [stroke];
    txt.autoSize = TextFieldAutoSize.LEFT;
    txt.selectable = false;
    txt.width = 400
    txt.height = 25
    var btn:Sprite = new Sprite();
    btn.mouseChildren = false;
    btn.addChild(txt); 
    btn.buttonMode = true;
    btn.x = stage.stageWidth / 10
    btn.y = stage.stageHeight / 2 - 50 * (i * .5)
    btn.name = "part" + String((Number(Number(curPart.substring(4))+1) + (i+1)))
    buttonHolder.addChild(btn);
    btn.addEventListener(
        MouseEvent.CLICK, 
        function m(zen:MouseEvent):void // when button is clicked 
        {
            //trace(buttonHolder.numChildren)
            //choice(zen.currentTarget.name,buttonHolder);
        } 
    )

    // Each button is put in the list,
    // to be able to work with the button after
}