2015-11-23 25 views
0

所以我做了一個對象數組像這樣的(?):我如何通過這個對象遍歷正確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(即使有應該是去1 2個文本字段,2)

+0

請在此處顯示您的代碼,而不是在將來可能會消失的其他地方 – frankhermes

回答

2

你的問題就在這裏:

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

i++在執行for -loop,其他一切只運行一次。 將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 
} 
0

根據代碼你張貼,我大膽猜測,你需要實現幾個循環:

  1. 通過「主」 對象,其中包括「部分對象」的循環。
  2. 通過各個 「部件目標」

在這裏我的意思的循環:

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