【问题标题】:How do I display children in succession, as3如何连续显示孩子,as3
【发布时间】:2010-03-29 02:21:06
【问题描述】:

如何连续显示子级?连续我的意思是 1,2,3,4 等等。也许用循环递增或使用计时器是我所追求的。

添加、删除、出现或消失孩子,都可以工作。一世 想要一种简单的方法来每秒显示 1 个孩子,直到我达到 10 岁。

尝试的方法
addChild、removeChild、AddChildAt、getChildAt、setChildIndex
可见!可见
for 循环

注意
我增加单个显示对象的其他答案很好,但我不明白这如何与多个孩子一起使用。我希望其他人觉得这个问题有帮助。

【问题讨论】:

  • 这不是和青蛙一起完成的吗..?

标签: flash actionscript-3


【解决方案1】:

就布局而言,我建议使用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;
    }

}

}

【讨论】:

  • 我有一个容器对象和孩子。我正在使用 Flash CS4。一些 as3 sn-ps 会有所帮助。
猜你喜欢
  • 2011-02-02
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多