2012-10-22 45 views
1

我想为一个最小的程序,可以画一条线,在一个新的Flex项目(使用Flash Builder 4.6):为什么这个ActionScript 3代码不能画出一条线?

但是,当它在Web浏览器(Chrome或Firefox),并且按钮运行myButton被点击,没有线被绘制 - 是否有人知道如何使它工作,以及如何使它可以绘制而不需要点击按钮?

<?xml version="1.0" encoding="utf-8"?> 
<s:Application 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"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

    <fx:Script> 

     <![CDATA[ 

      public function drawLine():void 
      { 
       var canvas:Shape = new Shape(); 
       canvas.graphics.lineStyle(3, 0xff); 
       canvas.graphics.moveTo (0,0); 
       canvas.graphics.lineTo(300,300); 
       this.addChild(canvas); 
      } 

     ]]> 

    </fx:Script> 

    <s:Button label="myButton" click="drawLine()" /> 

</s:Application> 
+1

你可能会尝试Spark [SpriteVisualElement](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/core/SpriteVisualElement.html)或[Graphic](http://help.adobe.com /en_US/FlashPlatform/reference/actionscript/3/spark/primitives/Graphic.html)而不是Shape, –

+0

正在使用Shape可能吗?在O'Reilly的Essential ActionScript 3.0中,p。 630,它使用形状,但只是它没有完整的程序列表 –

+0

那么,你的形状没有指定任何高度或宽度。我不相信形状有代码来自动调整大小。 – JeffryHouser

回答

4

在Flex应用程序中绘制简单线条(或者更复杂的线条,如果愿意的话)的更简单的方法是使用FXG图形。这个小片段应该在应用程序启动时立即画出与示例中相同的行:

<s:Path data="M 0 0 L 300 300 Z"> 
    <s:stroke> 
     <s:SolidColorStroke color="0xff" weight="3" /> 
    </s:stroke> 
</s:Path> 

完成!

现在,如果您绝对想坚持使用示例中的Shape类(我不建议在Flex应用程序中使用该类),则需要将其添加到Spark Flex 4应用程序的displayList中。 Shape不是IVisualElement,这是addElement()方法接受的唯一接口。
您可以使用SpriteVisualElement类,它完全适用于此目的。请注意,在mx(Flex 3)应用程序中,您不需要这样做,因为您可以简单地使用addChild()方法。 (我知道这可能对新手来说是混乱的。它是向后兼容的问题)

private function drawLine():void { 
    var shape:Shape = new Shape(); 
    shape.graphics.lineStyle(3, 0xff); 
    shape.graphics.moveTo (0,0); 
    shape.graphics.lineTo(300,300); 

    var sve:SpriteVisualElement = new SpriteVisualElement(); 
    sve.addChild(shape); 

    addElement(sve); 
} 

现在,为了避免点击按钮,只需调用drawLine()当应用程序的creationComplete事件触发:

<s:Application 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="drawLine()"> 
+0

,只需添加'import spark.core。*'?如果你不会在Flex应用中使用'Shape',你会在ActionScript项目中使用'Shape'吗? –

+0

@JeremyL我不明白你的“输入”问题。至于另一个:是的,在ActionScript项目中使用'Shape'并在Flex项目中使用FXG。 – RIAstar

+0

,因为如果'import'行不在那里,它会编译一个错误:'SpriteVisualElement'没有被定义或者是... –

1

的Spark应用标签延伸SkinnableComponent使用addChild()方法时将引发运行时错误。确保安装了Flash Player的调试版本,以便您可以看到该错误。它会帮助你调试很多东西。

下面是一些代码,工程:

<?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"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

    <fx:Script> 

     <![CDATA[ 

      public function drawLine():void 
      { 
       drawingArea.graphics.lineStyle(3, 0x000000); 
       drawingArea.graphics.moveTo (0,0); 
       drawingArea.graphics.lineTo(300,300); 
      } 

     ]]> 

    </fx:Script> 

    <s:Button label="myButton" click="drawLine()" /> 


    <s:Group width="100%" height="100%" id="drawingArea"> 

    </s:Group> 

</s:WindowedApplication> 

我给应用程序它自己的绘图区域,在MXML,并提请该区域。这是一个AIR应用程序;但相同的代码应该在基于web的应用程序中工作。

这是另一个示例,它不会在MXML中创建绘图区域;但是ActionSCript:

<?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"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

    <fx:Script> 

     <![CDATA[ 
      import mx.core.UIComponent; 

      public function drawLine():void 
      { 
       var canvas :UIComponent = new UIComponent; 
       canvas.graphics.lineStyle(3, 0xff); 
       canvas.graphics.moveTo (0,0); 
       canvas.graphics.lineTo(300,300); 
       this.addElement(canvas); 

      } 

     ]]> 

    </fx:Script> 

    <s:Button label="myButton" click="drawLine()" /> 


</s:WindowedApplication> 

注意;我在这里使用了UIComponent作为绘图元素。我相信UIComponent是实现IVisualElement的层次链上的“最高”组件,可以传递给addElement方法。

+0

这两个工作...但是可以像O'Reilly的Essential ActionScript 3.0一样使用'Shape'? (也是如何绘制线,而不需要按钮来调用'drawLine()'?) –

+0

是的,使用Shape应该是可能的。但是,您必须将该形状封装到Flex容器中才能用于Flex应用程序。 (不是Spark容器)。是的,你可以画一条线而不需要点击一个按钮;你可以在任何地方执行代码。也许作为Flex组件生命周期一部分的updateDisplayList()方法的一部分? – JeffryHouser