UI 组件:两个类——一个绘制另一个不绘制(Flex/AS)

发布于 2024-12-18 17:05:44 字数 2546 浏览 1 评论 0原文

我正在处理一个 Flex 项目,其中包含一个主 mxml 文件和两个绘制图形的 ActionScript 类。通过阅读大量的文章和帖子,我发现他们必须扩展 UIComponent,他们确实这样做了。第一个动作脚本类很好地绘制了图像。然后第一个类调用创建一个新对象。第二个对象不显示。在纯动作脚本版本中,我必须通过舞台作为第二个班级绘制的参考。

我还在 stackoverflow 上发现了类似的问题: UIComponent 扩展类未显示 UIComponent 扩展类。但我不确定如何将其应用到我的示例中。

应该重写哪个 UIComponent 方法才能显示第二个对象?在这个例子中我应该如何具体地实现它们?

如果这个问题有更好或更简单的答案,我们也将不胜感激。这是我在一个更复杂的项目中遇到的问题的简化示例。

// First class "Ball" draws a circle successfully then creates a second object "MyRect" 

        package dr {
        import flash.display.*;
        import flash.display.Sprite;

        import mx.core.*;

        public class Ball extends UIComponent {
            public function Ball()
            {
                var vCircle:Sprite = new Sprite();
                vCircle.graphics.beginFill(0xff0000);
                vCircle.graphics.drawCircle(100, 150, 40);
                vCircle.graphics.endFill();
                addChild(vCircle);

                var rect:MyRect = new MyRect();
            }   
        }
    }



    // Second class "MyRect" (does not display)

    package dr {
    import flash.display.*;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.geom.*;

    import mx.core.*;

    public class MyRect extends UIComponent
    {
        public function MyRect()
        {   

            var vRect:Sprite = new Sprite();
            vRect.graphics.beginFill(0x0000ff, 1);
            vRect.graphics.drawRect(300, 150, 150, 75);
            addChild(vRect);

            //var ui:UIComponent = new UIComponent();
            //addChild(ui);
            //ui.addChild(vRect);
            //UpdateDisplayList();  
        }

    }   
}

    //Here is the MXML main file:

    <?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"
                   xmlns:dr="dr.*"
                   minWidth="955" minHeight="600">
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <mx:Panel id="myPanel" width="700" height="600" paddingLeft="10" paddingTop="10">   
                <dr:Ball></dr:Ball>
        </mx:Panel>
    </s:Application>

I am working with a flex project with a main mxml file and two actionscript classes that draw graphics. From reading tons of articles and posts I discovered that they must extend UIComponent, which they do. The first actionscript class draws the image fine. The first class then makes the call to create a new object. The second object does not display. In the pure actionscript version I had to pass the stage as a reference for the second class to draw.

I also found a similar question here at stackoverflow:
UIComponent extended class isn't showing UIComponent extended class. But I was not sure how to apply it to my example.

Which, if any UIComponent methods should be overrided for the second object to display? How specifically should I implement them in this example?

If there is a better or simpler answer to this problem, that is also appreciated. This is a simplified example of a problem I'm having with a more complex project.

// First class "Ball" draws a circle successfully then creates a second object "MyRect" 

        package dr {
        import flash.display.*;
        import flash.display.Sprite;

        import mx.core.*;

        public class Ball extends UIComponent {
            public function Ball()
            {
                var vCircle:Sprite = new Sprite();
                vCircle.graphics.beginFill(0xff0000);
                vCircle.graphics.drawCircle(100, 150, 40);
                vCircle.graphics.endFill();
                addChild(vCircle);

                var rect:MyRect = new MyRect();
            }   
        }
    }



    // Second class "MyRect" (does not display)

    package dr {
    import flash.display.*;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.geom.*;

    import mx.core.*;

    public class MyRect extends UIComponent
    {
        public function MyRect()
        {   

            var vRect:Sprite = new Sprite();
            vRect.graphics.beginFill(0x0000ff, 1);
            vRect.graphics.drawRect(300, 150, 150, 75);
            addChild(vRect);

            //var ui:UIComponent = new UIComponent();
            //addChild(ui);
            //ui.addChild(vRect);
            //UpdateDisplayList();  
        }

    }   
}

    //Here is the MXML main file:

    <?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"
                   xmlns:dr="dr.*"
                   minWidth="955" minHeight="600">
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <mx:Panel id="myPanel" width="700" height="600" paddingLeft="10" paddingTop="10">   
                <dr:Ball></dr:Ball>
        </mx:Panel>
    </s:Application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-12-25 17:05:44

在 Ball 类中,实例化一个新的 MyRect 类;但是,您不将其添加到舞台中:

var rect:MyRect = new MyRect();
addChild(rect);

In your Ball class, you instantiate a new MyRect class; however, you don't add it to the stage:

var rect:MyRect = new MyRect();
addChild(rect);
深白境迁sunset 2024-12-25 17:05:44

vRect.graphics.endFill(); 丢失。

The vRect.graphics.endFill(); is missing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文