【问题标题】:How to use VGroup or HGroup in pure actionscript3?如何在纯 actionscript 3 中使用 Group 或 Group?
【发布时间】:2026-02-01 00:25:03
【问题描述】:

我正在使用免费的 Flex SDK 和文本编辑器并在命令行中编译来开发一个 Flash 应用程序。

我想在我的动作脚本中使用 VGroup 或 HGroup 来管理 DisplayObjects 的位置。

我写了以下代码:

import spark.components.*
import flash.text.*


var group:VGroup = new VGroup;

var text:TextField = new TextField
text.text = 'abc';

var sprite = new Sprite;
sprite.graphics.lineStyle(2, 0x000000);
sprite.graphics.drawRect(0, 0, 100, 100);

stage.addChild(group);
group.addElement(sprite); // runtime error
group.addElement(text); // compile error


但是将 Sprite 添加到 VGroup 会导致运行时错误:

TypeError: Error #1034: Type Coercion failed:
cannot convert flash.display::Sprite to mx.core.IVisualElement.


并且将 TextField 添加到 VGroup 会导致编译错误:

Error: Implicit coercion of a value of type flash.text:
TextField to an unrelated type mx.core:IVisualElement.


如何在纯 AS3 中使用 VGroup 或 HGroup?
DisplayObject 和 IVisualElement 有什么区别?


更新:

我尝试了 www.Flextras.com 的答案的第一种方式,SpriteVisualElement 和 StyleableTextField。
我写了以下代码:

package {
    import flash.display.*
    import spark.core.SpriteVisualElement
    //import spark.components.supportClasses.StyleableTextField // compile error
    import spark.components.VGroup
    import flash.text.*

    [SWF(backgroundColor=0xffffff, width=500, height=500, frameRate=12)]

    public class VGroupTest extends Sprite {
        function VGroupTest() {
            //var text:StyleableTextField = new StyleableTextField
            //text.text = 'abc';

            var sprite1:SpriteVisualElement = new SpriteVisualElement;
            sprite1.graphics.lineStyle(2, 0x000000);
            sprite1.graphics.drawRect(0, 0, 100, 100);
            sprite1.width = 200
            sprite1.height = 200

            var sprite2:SpriteVisualElement = new SpriteVisualElement;
            sprite2.graphics.lineStyle(2, 0xff0000);
            sprite2.graphics.drawRect(0, 0, 200, 200);
            sprite2.width = 300
            sprite2.height = 300

            var group:VGroup = new VGroup;
            group.gap = 10
            group.width = 400
            group.height = 400
            this.stage.addChild(group);

            // the following codes show nothing
            //group.addElement(text);
            group.addElement(sprite1);
            group.addElement(sprite2);

            // the following codes show 2 rectangles
            //this.stage.addChild(sprite1)
            //this.stage.addChild(sprite2)
        }
    }
}


但是

import spark.components.supportClasses.StyleableTextField

导致以下错误

40 Error: Definition spark.components.supportClasses:StyleableTextField could not be found


并且屏幕上没有显示 SpriteVisualElement。
我错过了什么吗?

【问题讨论】:

    标签: actionscript-3 apache-flex layout actionscript flex-spark


    【解决方案1】:

    您使用了正确的概念方法。但是,组(或 VGroup 或 HGroup)中的元素必须实现 IVisualElementSpriteTextField 都没有实现。

    您有几个选择可以考虑:

    1. 使用SpriteVisualElement 代替Sprite;或使用 StyleableTextField 而不是 TextField。
    2. 将 Sprite 或 TextField 包装为 UIComponent 的子项;然后将该 UIComponent 用作组中的元素。
    3. 使用 MX 容器,例如 HBox 或 VBox。
    4. 使用 UIComponent 而不是组。您必须在 updateDisplayList() 中编写自己的布局代码才能使其正常工作。

    我的偏好是第一种方法,其次是第四种方法。方法 2 增加了很多额外的编码,方法 3 由于对 MX/Halo 架构的依赖而不可取。

    【讨论】:

    • 感谢您的回答。我尝试使用 SpriteVisualElement(第一种方法)。我在每个上创建了 2 个 SpriteVisualElements 和 drawRect。并将它们添加到 VGroup。并将 VGroup 添加到舞台上。运行时和编译时的错误都消失了。但不幸的是,屏幕上什么也没显示。我错过了什么吗?
    • 没有看到代码;这很难说。我很确定 SpriteVisualElement 不会自行调整大小。您是否为实例指定了高度和宽度?是不是发生了什么奇怪的事情,比如矩形与背景颜色相同?
    • 感谢您的回复。我添加了设置宽度和高度的代码,但仍然没有显示任何内容。我在我的问题中添加了一个新代码。你能检查一下代码吗?
    • 您使用的是什么版本的 Flex SDK?直到 Flex 4.5 才引入 StyleableTextField。你有一个主要的应用程序文件吗?将任何东西直接添加到舞台上对于 Fle 应用程序来说是不寻常的。您将项目作为子项添加到其父容器中;永远不要直接上台。
    • 感谢您的回复。我正在使用 Flex SDK 4.5.1。我添加到问题中的代码是我用来编译的所有代码。我在一个名为VGroupTest.as 的文件中编写了该代码。我使用命令mxmlc -debug=true -static-link-runtime-shared-libraries=true VGroupTest.as 编译它。而VGroupTest.swf 是由该命令创建的。 never to the stage directly 表示我应该使用this.addChild()(这是VGroupTest 的实例)而不是this.stage.addChild()?也许我应该放弃使用 VGroup/HGroup 并编写自己的 VGroup/HGroup。