简体   繁体   English

Flex 4 / AS3 addchild问题中的图形绘制

[英]Drawing Shapes in Flex 4/AS3 addchild issues

I am simply trying to draw a rectangle inside of a panel using flex4. 我只是想使用flex4在面板内部绘制一个矩形。 I am using spark instead of mx. 我正在使用spark而不是mx。 It complains about addchild being replaced by addelement; 它抱怨addchild被addelement代替; however, addelement expects type ivisualcomponent. 但是,addelement期望类型为ivisualcomponent。 I think sprite should be of that type; 我认为精灵应该是这种类型。 however, it reports an error when trying to use the below code... I have tried a few different ways. 但是,当尝试使用以下代码时,它报告一个错误...我尝试了几种不同的方法。 I think I am missing something very basic about flex 4. Any enlightenment would be much appreciated! 我想我缺少关于flex 4的一些非常基本的知识。任何启发将不胜感激! :-D :-D

private function drawRectangle(e:MouseEvent):void{
    var s:Sprite = new Sprite();
    s.graphics.beginFill(0x00ff00, 0.5);
    s.graphics.drawRect(e.localX,e.localY,50,50);
    s.graphics.endFill();
    canvas.addChild(s);
}

Sprite doesn't implement IVisualComponent. Sprite没有实现IVisualComponent。 (Check the docs: http://www.eonflex.com/flex/4.1/langref/flash/display/Sprite.html ) (检查文档: http : //www.eonflex.com/flex/4.1/langref/flash/display/Sprite.html

You need to add a UIComponent to hold the sprite. 您需要添加一个UIComponent来容纳精灵。 Something like: 就像是:

private function drawRectangle(e:MouseEvent) : void {
    var s:Sprite = new Sprite();
    var c:UIComponent = new UIComponent();

    c.addChild(s);
    canvas.addChild(c);
}

Yes we can make it using MXML Syntax also. 是的,我们也可以使用MXML语法来实现。 But in AS3, you need to use SpriteVisualElement class. 但是在AS3中,您需要使用SpriteVisualElement类。

var sp:SpriteVisualElement = new SpriteVisualElement();
this.addElement(sp);
sp.graphics.beginFill(0x00ff00,1);
sp.graphics.drawRoundRect(10,10,100,100,150,150);
sp.graphics.endFill();

It will work. 它会工作。

Note that you can also do this with MXML graphics: 请注意,您也可以使用MXML图形执行此操作:

<?xml version="1.0" encoding="utf-8"?>
<!-- fxg/GraphicCompMainMXML.mxml -->
<s:Application backgroundColor="0xFFFFFF"      
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns:s="library://ns.adobe.com/flex/spark">
    <s:Graphic>    
         <s:Rect id="rect1" width="200" height="200">
              <s:fill>
                 <s:SolidColor color="0xFFFFCC"/>
              </s:fill>
              <s:stroke>
                 <s:SolidColorStroke color="0x660099" weight="2"/>
              </s:stroke>
         </s:Rect>
    </s:Graphic>
</s:Application>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM