2011-09-03 54 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

感謝您的回覆。 我添加了代碼來設置寬度和高度,但沒有任何顯示。 我爲我的問題添加了一個新代碼。你能檢查代碼嗎? –

相關問題