代码之家  ›  专栏  ›  技术社区  ›  anon255058

如何连续显示子对象,as3

  •  3
  • anon255058  · 技术社区  · 16 年前

    如何连续显示子对象?我所说的连续是指1,2,3,4,等等。也许用循环递增或使用计时器是我想要的。

    想要一个简单的方法,显示一个孩子每秒钟,直到我达到10。

    尝试的方法
    addChild,removeChild,AddChildAt,getChildAt,setChildIndex
    看得见!看得见的
    for循环

    注意

    1 回复  |  直到 16 年前
        1
  •  0
  •   bedwyr    16 年前

    至于布局,我建议使用 ViewStack . 您只需将每个子级添加一次,然后循环遍历堆栈本身的索引。

    你可以把这个和一个 Timer ,每次更改堆栈索引时都重新启动它。倒计时并在计时器完成时,递增ViewStack的选定子索引。当您命中最后一个子对象时,请将索引重置为0,或者执行其他操作。


    编辑 :这是我整理的一个示例文件。这在视觉上不是很有趣,因为每个子对象只是一个VBox,带有一个粗体标签,指示其在ViewStack中的索引。如果希望在子对象中看到更多的视觉差异,可以为每个子对象使用不同的背景色,然后每次ViewStack更改其状态时都会看到不同的背景色。

    我放在一起的应用程序是在Flex中的,所以它将这个类嵌入到主MXML文件中,该文件有一个按钮来调用“startTimer()”函数。我不确定您将如何利用CS4,但希望您可以从这里开始:)

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

    }