How do I display children in succession, as3
How do I display children in succession? By succession I mean 1,2,3,4, etc. Perhapse incrementing with a loop or using a timer is what I'm after.
Add, remove, appear, or disappear children, could all work. I want a simple way to display 1 child every second until I reach 10.
METHODS TRYED
addChild, removeChild, AddChildAt, getChildAt, setChildIndex visible !visible for loopNote
My other answers for incrementing a single display object are good, but I don't see h开发者_高级运维ow this works with multiple children. I hope others find this question helpful.As far as layout goes, I would recommend using a ViewStack. You would only have to add each child once, and then just cycle through the indices of the stack itself.
You could combine this with a Timer, re-starting it every time the stack index is changed. Count down and when the timer completes, increment the selected child index of the ViewStack. When you hit the last child, reset the index to 0, or do something different.
EDIT: here's a sample file I put together. It's not very interesting visually, since each child is simply a VBox with a bold label indicating its index in the ViewStack. If you wanted to see more visual differences in the children, you could have a different background color for each, and then you'd see a different background every time the ViewStack changes its state.
The app I put together is in Flex, so it embeds this class in the main MXML file, which has a button to invoke the "startTimer()" function. I'm not sure how you would leverage this CS4, but hopefully you can take it from here :)
package {
import flash.events.TimerEvent;
import flash.utils.*;
import mx.containers.Box;
import mx.containers.VBox;
import mx.containers.ViewStack;
import mx.controls.Label;
public class StackExample extends Box {
private var stack:ViewStack;
private var timer:Timer;
public function StackExample() {
super();
stack = new ViewStack();
stack.percentHeight = 100;
stack.percentWidth = 100;
//Add some sample children so we can watch
//the ViewStack increment. The numbering here
//is arbitrary
for(var i:int=0; i<10; i++) {
stack.addChild(createNewBox(i));
}
stack.selectedIndex = 0;
addChild(stack);
}
//Main application will invoke this function to show the ViewStack
//changing its children every second until the timer is exhausted
public function startTimer():void {
timer = new Timer(1000, 10);
//"false, 0, true" forces the listener to use weak references so
//the event will be cleaned up if this class is destroyed
timer.addEventListener(TimerEvent.TIMER, incrementStack, false, 0, true);
timer.start();
}
private function createNewBox(index:int):Box {
var newChildBox:VBox = new VBox();
newChildBox.setStyle("borderStyle", "solid");
var childLabel:Label = new Label();
childLabel.percentWidth = 100;
childLabel.text = "Child: " + index.toString();
childLabel.setStyle("fontWeight", "bold");
newChildBox.addChild(childLabel);
return newChildBox;
}
private function incrementStack(event:TimerEvent):void {
if(stack.selectedIndex < stack.numChildren)
stack.selectedIndex = stack.selectedIndex + 1;
}
}
}
精彩评论