So I've made a object array(?) like this:
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"
}
}
I need to iterate through the choices of part1
.
I am currently trying this:
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
}
but I only get 1 textfield and that says 2 (even though there should be 2 text fields that go 1, 2)
Your issue is here:
for (var s:String in object[curPart]["choices"]) i++
The i++
is executed in the for
-loop, everything else is just run once.
Move i++
inside the braces of your loop, and everything will work fine.
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
}