开发者

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

开发者 https://www.devze.com 2023-02-20 20:40 出处:网络
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 wo

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();
            }
      开发者_StackOverflow  ]]>
    </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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消