0

My goal is to write educational software to run in Flash or AIR. (Probably mostly AIR on mobile devices.) I'm envisioning that the stage will have an area with controls like Flex provides, but another area with animations and graphics that would be done in pure ActionScript. I have been told that if I use Flex I will need to inherit UIComponent in any new components that I create, but I was also told alternatively I could put a big UIComponent on the screen and add Sprite-based objects to it. So I am trying to code the latter, and I'm stuck. I created an MXML file with a button and a UIComponent. Then I would like to write a class in a seperate .AS file that takes the UIComponent in its constructor and handles adding objects. I don't know how to reference the UIComponent in the script code in the MXML file. Here's what I have:

<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">
  <fx:Script>
    <![CDATA[
      import exper AddSprites;
      private var e:AddSprites;  
      public function addStuff():void {
        ???? next line is question ???
        e = new AddSprites(..UIComponent?..);
      }
    ]]>

  </fx:Script>

  <s:layout>
    <s:VerticalLayout/>
  </s:layout>

  <s:Button click="addStuff()" />

  <mx:UIComponent id="uic"/>
</s:Application>
4

1 回答 1

1

You can reference your UIComponent with its id, uic inside your script block. You can then pass it to your addStuff function. Since UIComponent inherits from Sprite, you can perfectly have a addSprite function that takes a Sprite as a parameter and doesn't know anything about Flex classes and be pure AS3 code.

So basically:

e = new AddSprites(uic);

With: function AddSprites(holder:Sprite)for your constructor.

于 2013-07-19T14:26:29.810 回答