6 Replies Latest reply on Jun 18, 2009 12:58 AM by christoferek

    Creating properties from as3

    christoferek Level 1

      Hi All,

       

      I have

       

      <mx:Canvas x="0" y="0" width="100%" height="100%" id="prepBlobs"/>

       

      so there are no properties in this component.

       

      I want to add a new component now:

       

           var itemPicX:Bitmap = new Bitmap();

           var wrapperX:UIComponent = new UIComponent();
           wrapperX.addChild(itemPicX);
           wrapperX.name = "first picture";
           wrapperX.id = "itemPicX";
                 
           Application.application.prepBlobs.addChild(wrapperX);
           Application.application.prepBlobs.itemPicX.visible = true;

       

      This fails with the error: Error #1069: Property itemPicX not found on mx.containers.Canvas and there is no default value.

       

      What am I doing wrong?

       

      I did all of this because I want to loop all the elements within the canvas. I want to create them at the beginning of the loop and remove all of them using prepBlobs.removeAllChildren at and the end of the loop.

       

      Christopherek

        • 1. Re: Creating properties from as3
          leybniz Level 4

          Application.application.prepBlobs.itemPicX.visible = true

           

          itemPicX - is not a property of Canvas.

           

          If you want to iterate through Canvas childs, use this one loop:

          for each (var d:DisplayObject in prepBlobs.getChildren())

           

          If you feel this message answers your question or helps, please mark it respectively

          • 2. Re: Creating properties from as3
            christoferek Level 1

            Hi Alex,

             

            Thank you for your answer.

             

            To be fully happy I want to know how I make itemPicX to be the property of the canvas and use it like this Application.application.xxx.yyy

            That makes the most difficult problem to me.

             

            Chris

            • 3. Re: Creating properties from as3
              leybniz Level 4

              Why do you need it to be a property? Actually the use of Application.application is discouraged, so it would be better to go with something else.

              for example you could come up with your own modelHolderClass, which will contains all the references you need elsewere in the code.

               

               

               

               

              public

               

               

              class ModelLocator extends EventDispatcher {

               

               

              private

               

              static var instance:ModelLocator = getInstance();

               

              public var itemPicX:DisplayObject;

               

               

               

               

               

               

               

               

               

               

              function ModelLocator() {

               

               

                   if (instance)

                        throw new Error('Error instance already exists', getQualifiedClassName(this));

               

              }

               

               

               

               

              public static function getInstance():ModelLocator {

               

               

                   if (!instance)

                        instance = new ModelLocator();

               

               

                   return instance;

              }

               

              }

               

              And you are using it like this:

              Inside your Application tag

               

              ModelLocator.getInstance().itemPicX = new Image(); and so on..

               

              anywhere in the code access this property using the same line of code:

              ModelLocator.getInstance().itemPicX

              • 4. Re: Creating properties from as3
                christoferek Level 1

                Hi Alex,

                 

                Thank you for your kind answer.

                 

                I want a simple thing to do:

                 

                Create a canvas, add some children like html component, add pictures to it as children, take a snapshot of the result and then remove all the children from the canvas. And repeat all this again a few times.

                 

                Using Application.application.prepBlobs.children makes it easy to use Application.application.prepBlobs.removeAllChildren for clearing the stage.

                 

                prepBlobs is a canvas created in MXML and all other children are created dynamicaly by as3. (or I overlooked something and my thinking goes in wrong direction).

                 

                I noticed that having images created by MXML is easier to add as a child to the canvas but after deleting there is no way to recreate them (images) as MXML. That's the point and my thinking went into using as3  to create all the needed children.

                 

                Is my idea correct?

                 

                Chris

                • 5. Re: Creating properties from as3
                  leybniz Level 4

                  I have an better Idea for you,

                  why don't you create your very own Canvas Descendant class in pure AS3 and put all logic inside of it?

                  something like this:

                   

                  public class YourCanvas extends Canvas {

                   

                   

                       public function addStuffToCanvas(stuff:*):void {

                       }

                   

                       public function cleanCanvas():void {

                            this.removeAllChildren();

                       }

                   

                       public function takeASnapshot():BitmapData {

                           return snapshot data;

                       }

                  }

                   

                  If you feel this message answers your question or helps, please mark it respectively

                  • 6. Re: Creating properties from as3
                    christoferek Level 1

                    Hi Alex,

                     

                         You are great.

                     

                         I have just implemented your idea.

                     

                         Your soulution simplifies a few problems and works like a charm.

                     

                         Thank you.

                     

                         Chris