2 Replies Latest reply on Mar 4, 2010 1:45 PM by mrk.las

    Creating components at runtime

    mrk.las

      Hello,

       

      My question may not be related as much to Flex as more to Actionscript / CS4 that is being used in Flex, so my apologies if this is the wrong forum.

       

      The application I'm working on requires to create several MovieClips at runtime. The amount of MovieClips is dependent on a number given from the database. I've created a movie in CS4, linked it to a class (let's call it ParentComponent) and at runtime I can do something that reads:

       

      var componentCount : int = database.queryComponentCount();


      for (var i : int = 0; i < componentCount; ++i)

      {

        var parent : ParentComponent = new ParentComponent();

        addToStage(parent);

      }

       

      This works all fine, the screen shows the right number of components.

       

      Now I've added a button-component (let's call it TextButton) to the ParentComponent in CS4. This button is a custom MovieClip with its own component parameters and timeline. I've added actions on the button's timeline that are associated with the button's state (eg mouseOver, mouseDown, mouseUp and so on). When I create the ParentComponent as I did previously, the button will show up. However, it has lost all its parameters and actions on its timeline: it has been degraded to a standard MovieClip.

       

      Note that this only happens when I nest the button inside the ParentComponent. If I associate / link the button with a class and create the button in a stand-alone fashion it works fine. Nested inside the ParentComponent it loses all its properties.

       

      It works fine (nested and all) when I actually load the swf via loader, however I'm hestiant to do that for every ParentComponent I need to create. Ideally I would like to be able to keep the creation as it is.

       

      I've tried changing ParentComponent from

       

      public class ParentComponent extends MovieClip

      {

         public var m_textButton : MovieClip;

         ...


         public function ParentComponent()

         {

             super();

         }

      }

       

      to

       

      public class ParentComponent extends MovieClip

      {

          public var m_textButton : TextButton;

         ...


         public function ParentComponent()

         {

             super();

          }

      }

       

      But that results in an Error #1034: Type Coercion failed when the ParentComponent is created.

       

      Any help on how to do this and an explanation on what's going wrong here, why the actionscript actions and parameters are lost would be highly appreciated.

       

      Best

      Mark