Search code examples
apache-flexactionscript-3actionscriptflex4papervision3d

How can I add a flex component programmatically from within a Papervision Class without using this.parent...?


I have a flex application and a papervision BasicView. I would like to add a new flex UIComponent (such as a button) from within the papervision class. I have posted the full example code below. It works but I would like to be able to accomplish my goal without the "(this.parent.parent as Group).addElement(button);" line.

<!--Application MXML-->
<?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="50" minHeight="50"
        creationComplete="Init()" applicationComplete="Start()">
    <fx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            import spark.components.Button;
            public var start:QuickStart;
            public function Init():void
            {
                start = new QuickStart();
                var uicomp:UIComponent = new UIComponent();
                addElement( uicomp );
                uicomp.addChild( start );
            }

            public function Start():void
            {
                start.GoTime();
            }
        ]]>
    </fx:Script>
</s:Application>


//QuickStart.as
package{
    import org.papervision3d.view.BasicView;
    public class QuickStart extends BasicView
    {
        public function QuickStart()
        {
            super(500, 500, true, true);
        }

        public function GoTime():void
        {
            var button:Button = new Button;

            //this is the offending line
            (this.parent.parent as Group).addElement(button);
        }
    }
}

The version I have does work so please excuse any minor typos.


Solution

  • Logically you would dispatch an event inside your BasicView, listen for it in your main application, and create the button from up there. In a prefect OOP world, every class should be a black box sending events :)