如何正确地遍历此对象AS3

时间:2015-11-23 08:08:35

标签: actionscript-3 actionscript

所以我做了一个这样的对象数组(?):

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)

2 个答案:

答案 0 :(得分:2)

您的问题在这里:

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
}

答案 1 :(得分:0)

根据你发布的代码,我猜测你需要实现一些循环:

  1. 循环通过" main" 对象,由"部分对象"。
  2. 组成
  3. 循环浏览每个"部分对象"
  4. 这就是我的意思:

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