【问题标题】:Problem with displaying AS3 code in Flex在 Flex 中显示 AS3 代码的问题
【发布时间】:2011-09-23 20:03:23
【问题描述】:

我有 AS3 脚本 bitfade.text.steel 返回 Sprite 类型。它适用于 Flash 环境,但不适用于 FLEX。在 Flex 中它什么也不显示。没有出现错误。看起来附加的代码也有效。我使用 Flex SDK 4.5

你能告诉我这段代码可能有什么问题吗?

英语不是我的母语。 如有错误请指正。

克里斯

<?xml version="1.0" encoding="utf-8"?>                                                                              
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"                                                     
                xmlns:s="library://ns.adobe.com/flex/spark"                                                         
                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"                            
                creationComplete="creationComplete();" >                                                            
    <fx:Declarations>                                                                                               
        <!-- Place non-visual elements (e.g., services, value objects) here -->                                     
    </fx:Declarations>                                                                                              
    <fx:Script>                                                                                                     
        <![CDATA[                                                                                                   
            import bitfade.text.steel;                                                                              
            import flash.display.Sprite;                                                                            
            import mx.core.UIComponent;                                                                             

            function creationComplete(){                                                                            
                var textSteel = new steel("config.xml");                                                            
                if(textSteel is Sprite){                                                                            
                    trace("it is a sprite");                                                                        
                }                                                                                                   
                //var textSteelName:String = getQualifiedClassName(textSteel);                                      
                //trace(textSteelName);                                                                             
                textSteel.x = 150;                                                                                  
                trace("this is visible on the screen");                                                             
                var sprite:Sprite = new Sprite();                                                                   
                sprite.graphics.beginFill(0xFFCC00);                                                                
                sprite.graphics.drawCircle( 40, 40, 40 );                                                           
                sprite.graphics.endFill();                                                                          

                var wrapper:UIComponent = new UIComponent();                                                        
                wrapper.addChild(sprite);                                                                           
                wrapper.addChild(textSteel);                                                                        
                animationStage.addElement(wrapper);                                                                 
            }                                                                                                       
        ]]>                                                                                                         
    </fx:Script>                                                                                                    
    <s:Group id="animationStage" visible="true" x="50" y="50">                                                      
        <s:Label text="test">                                                                                       

        </s:Label>                                                                                                  
    </s:Group>                                                                                                      
</s:WindowedApplication>                                                                                            

【问题讨论】:

    标签: apache-flex actionscript-3


    【解决方案1】:

    如果我将您的自定义类切换为 Sprite,并在其上绘制一些东西,它就会显示出来。因此,我相信你的钢铁类中有一个错误。一个精灵不会有任何大小,直到它里面有一个大小的东西。没有任何迹象表明您所展示的代码在您的 steel 类中发生了什么。

    我的样本:

    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="creationComplete();">
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
    
        <fx:Script>                                                                                                     
            <![CDATA[                                                                                                   
    //          import bitfade.text.steel;                                                                              
                import flash.display.Sprite;
    
                import mx.core.UIComponent;                                                                             
    
                public function creationComplete():void{                                                                            
                    var textSteel : Sprite = new Sprite();                                                            
                    if(textSteel is Sprite){                                                                            
                        trace("it is a sprite");                                                                        
                    }                                                                                                   
    
                    //var textSteelName:String = getQualifiedClassName(textSteel);                                      
                    //trace(textSteelName);                                                                             
                    textSteel.x = 150;                                                                                  
    
                    // JH Code added new
                    textSteel.graphics.beginFill(0xFFCC00);                                                                
                    textSteel.graphics.drawRect(0,0,100,100);                                                           
                    textSteel.graphics.endFill();                                                                          
    
                    trace("this is visible on the screen");                                                             
                    var sprite:Sprite = new Sprite();                                                                   
                    sprite.graphics.beginFill(0xFFCC00);                                                                
                    sprite.graphics.drawCircle( 40, 40, 40 );                                                           
                    sprite.graphics.endFill();                                                                          
    
                    var wrapper:UIComponent = new UIComponent();                                                        
                    wrapper.addChild(sprite);                                                                           
                    wrapper.addChild(textSteel);                                                                        
                    animationStage.addElement(wrapper);                                                                 
                }                                                                                                       
            ]]>                                                                                                         
        </fx:Script>                                                                                                    
        <s:Group id="animationStage" visible="true" x="50" y="50">                                                      
            <s:Label text="test">                                                                                       
    
            </s:Label>                                                                                                  
        </s:Group>      
    
    </s:WindowedApplication>
    

    【讨论】:

      【解决方案2】:

      我认为您需要设置wrapper UIComponent 实例的宽度和高度。

      UIComponents 不会自动调整其内容的大小,因此它以 0 的宽度和高度显示。

      【讨论】:

      • 我的 AS3 脚本,是一个商业 AS3 脚本 - 所以它应该可以工作......在上面的代码中,我添加了一些命令来生成一些“测试代码”。这是一个精灵变量,它被包装在 UIComponent 类型的包装器中,并且此测试代码可以正常工作!在这种情况下,包装器变量的宽度和高度都大于 0。你不觉得吗?
      • 将高度和宽度设置为大于 0 的值不起作用。
      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 2020-03-16
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多