如何在纯actionscript3中使用VGroup或HGroup?

时间:2011-09-03 02:11:25

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

我正在开发一个Flash应用程序,使用免费的Flex SDK和文本编辑器,并在命令行中进行编译
我想在我的actionscript中使用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有什么区别?


UPDATE:

我尝试了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 我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

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

您可以考虑以下几个选项:

  1. 使用SpriteVisualElement代替精灵;或使用StyleableTextField而不是TextField。
  2. 将Sprite或TextField包装为UIComponent的子级;然后使用该UIComponent作为组中的元素。
  3. 使用MX容器,例如HBox或VBox。
  4. 使用UIComponent而不是Group。您必须在updateDisplayList()中编写自己的布局代码才能使其正常工作。
  5. 我的偏好是第一种方法,其次是第四种方法。方法2增加了许多额外的编码,由于依赖于MX / Halo架构,方法3是不可取的。