Question

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>
Was it helpful?

Solution

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);

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top