代码之家  ›  专栏  ›  技术社区  ›  Vladimir Shevyakov

在Flex、ActionScript 3中动态显示文本

  •  0
  • Vladimir Shevyakov  · 技术社区  · 7 年前

    我在使用Flex SDK 4.6在ActionScript 3中显示文本时遇到问题。我尝试的任何方法都不会导致任何变化或出现黑屏;这是我的项目代码和尝试。

    我的项目。mxml非常简单 mx:Application 标记相关零件

    <mx:Script>
    <![CDATA[
      include "MyProject.as"; //simply running my project file
    ]]>
    </mx:Script>
    

    <mx:Canvas id="gamePanel" x="0" y="0" width="100%" height="100%"/> //defining the canvas
    

    在MyProject中。就像我一样

    import flash.display.*; 
    import flash.events.*;
    import mx.events.*;
    import mx.controls.*;
    
    import Game;
    
    public static const SCREEN_WIDTH:int = 960;
    public static const SCREEN_HEIGHT:int = 720;
    private var initializationCompleted:Boolean = false;
    public var screenBuffer:BitmapData;
    public var game:Game;
    
    public function setup():void {
        screenBuffer = new BitmapData(SCREEN_WIDTH, SCREEN_HEIGHT, false, 0x00000000);
        game = new Game(SCREEN_WIDTH, SCREEN_HEIGHT, screenBuffer);
        initializationCompleted = true;
    }
    
    private function updateFrame():void {
        if (!initializationCompleted) {
            return;
        }
    
        draw();
    
        gamePanel.graphics.clear();
        gamePanel.graphics.beginBitmapFill(screenBuffer, null, false, false);
        gamePanel.graphics.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        gamePanel.graphics.endFill();
    }
    
    private function draw():void {
        game.update();
    }
    

    在游戏中。正如所示,我只需使用BitmapData类绘制所有内容,然后将所有内容复制到screenBuffer:

    screenBuffer.copyPixels(myBitmap, new Rectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), new Point(0,0));
    

    (这只是相关的代码-我尽可能地删减了代码,留下了一个“最小、完整和可验证的示例”)

    现在,我在项目中显示文本时遇到问题。我知道 TextField 是的子类 flash.display.Sprite 可以添加到画布中。每当我尝试使用

    var txtHello:TextField = new TextField();
    txtHello.text = "Hello World";
    gamePanel.addChild(txtHello)
    

    这或不改变任何内容(如果在中使用 setup() ,我假设我正在绘制它,否则它将永远不会显示)或导致黑屏(如果在中的任何位置使用 updateFrame() ,我假设我正在创建无限精灵)。

    相反,我尝试了创建一个名为“TextWithImage.as”的新文件,其中包含以下内容

    //this is ripped off the adobe help page
    package { 
        import flash.display.Sprite; 
        import flash.text.*; 
    
        public class TextWithImage extends Sprite { 
            private var myTextBox:TextField = new TextField(); 
            private var myText:String = "Hello World"; 
    
            public function TextWithImage() { 
                addChild(myTextBox); 
                myTextBox.text = myText; 
            } 
        } 
    }
    

    将其导入MyProject。作为,然后使用它作为

    gamePanel.addChild(new TextWithImage());
    

    与我上次尝试的效果相同。

    在Flex/AS3中显示文本的最简单方法是什么?感谢您的帮助,并提前向您表示感谢!

    1 回复  |  直到 7 年前
        1
  •  2
  •   Organis    7 年前

    这是个诡计。Flex组件,尽管具有相同的 显示列表 方法派生自 DisplayObjectContainer 类,实际上无法直接添加常规Flash内容-形状、精灵、MovieClip、文本字段、位图。更重要的是,它们不会产生任何运行时错误,我个人认为它们完全不会让新手感到困惑。

    Flex组件只能 显示列表 扩展基本 UIComponent组件 班同时 UIComponent组件 可以 显示列表 常规Flash内容。因此,您可以按照以下步骤进行操作:

    var proxyContainer:UIComponent = new UIComponent;
    var txtHello:TextField = new TextField;
    
    txtHello.text = "Hello World";
    proxyContainer.addChild(txtHello);
    
    gamePanel.addChild(proxyContainer);