开发者

Rendering MXML component only after actionscript is finished

开发者 https://www.devze.com 2022-12-25 17:35 出处:网络
In my mxml file, I\'m doing some calculations in the script tag, and binding them to a custom component.

In my mxml file, I'm doing some calculations in the script tag, and binding them to a custom component.

 <fx:Script>
  <![CDATA[

   [Bindable] public var calc1:Number;
   [Bindable] public var calc2:Number;

            private function init():void {
               calc1 = //calculation;
               calc2 = //calculation;
            }
  ]]>
 </fx:Script>

    <mycomp:Ball compfield1="{calc1}" compfield2="{calc2}"/>

The problem is that the mxml component is being created before the actionscript is run. So when the component is created, it actually doesn't get calc1 and calc2 and it fails from that point. I know that binding happens after that, but the component and its functions have already started and have run with the null or 0 initial values.

My solution was to create the component also in actionscript right after calc1 and calc2 have been created. This way I get to control precisely when it's created

 <fx:Script>
  <![CDATA[

   [Bindable] public var calc1:Number;
   [Bindable] public var calc2:Number;

            private function init():void {
               calc1 = //calculation;
               calc2 = //calculation;

               var Ball:Ball = new Ball(calc1, calc2);
            }
  ]]>
 </fx:Script>

but this is creating all kinds of other problems due to the way I've set up the component.

Is there a way I can still use mxml to create the component, yet 开发者_高级运维control that it the <myComp:Ball> gets created only after init() is run and calc1 calc2 evaluated?


Try setting your vars to zero values first:

   [Bindable] public var calc1:Number = 0;
   [Bindable] public var calc2:Number = 0;

Then call the function on creationComplete:

<mycomp:Ball 
   compfield1="{calc1}"
   compfield2="{calc2}"
   creationComplete="init()"/>
0

精彩评论

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