2011-09-03 42 views
1

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

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

我写了下面的代码:如何在pure actionscript3中使用VGroup或HGroup?

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 


但添加雪碧到VGroup导致运行时错误:

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


并添加文本字段到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是显示在屏幕上。
我错过了什么吗?

回答

3

您正在使用正确的概念方法。但是,组(或VGroup或HGroup)中的元素必须实现IVisualElement,其中SpriteTextField均未实现。

您有几种选择可以考虑:

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

我喜欢的是第一种方法,接下来是第四种方法。方法2增加了许多额外的编码,而方法3由于依赖MX/Halo架构而不受欢迎。

+0

感谢您的回答。 我尝试使用SpriteVisualElement(第一种方法)。 我在它们的每一个上创建了2个SpriteVisualElements和drawRect。 并将它们添加到VGroup。 并将VGroup加入舞台。 运行时和编译时错误都消失了。 但不幸的是,屏幕上没有显示任何内容。 我错过了什么吗? –

+0

没有看到代码;这很难说。我很确定一个SpriteVisualElement不会自己调整大小。你是否指定了实例的高度和宽度?有什么奇怪的事情发生,比如矩形与背景颜色相同? – JeffryHouser

+0

感谢您的回复。 我添加了代码来设置宽度和高度,但没有任何显示。 我为我的问题添加了一个新代码。你能检查代码吗? –

相关问题