12 Replies Latest reply on May 21, 2009 7:36 AM by Seglespaan

    Multiple attributes

    Seglespaan Level 1



      I have an application where the user can add various objects to the stage. Each of these objects will have various attributes such as x and y co-ordinates, size colour etc.


      These can be change by the user or they can select a preset that will automatically update all of the attributes.


      What I'm really trying to work out is the best way to achieve this.


      I could do the following.


      When the user hits preset 1, change object 1 x to 10, object1 y to 50, object1 colour to 123456, etc etc.


      But whats the best way to achieve this rather than hard coding all the presets?


      can I load these from xml?

        • 1. Re: Multiple attributes
          Michael Borbor Level 4

          Yes XML files will be a good way to go in your case.

          • 2. Re: Multiple attributes
            Seglespaan Level 1

            would I then bring this into flex as an array collection?

            • 3. Re: Multiple attributes
              Michael Borbor Level 4

              You could use a loader, and add these properties to a XMLListCollection.

              • 4. Re: Multiple attributes
                Seglespaan Level 1

                ok thanks,


                this might be a bit off thread, but say i have



                var objSend:object = new object();


                objSend.companyX = child.x;

                objSend.compnayY = child.y;


                writeData.send( objSend );



                how do I go about passing a variable name into the objSend, i.e.


                var objSend:object = new object();


                objSend.child.id+"x" = child.x;

                objSend.child.id+"y" = child.y;


                writeData.send( objSend );

                • 5. Re: Multiple attributes
                  Michael Borbor Level 4

                  They way I go with this will be, create bindable variables, so when I change my presets, my vars will change and the layout will reflect that change.

                  • 6. Re: Multiple attributes
                    Seglespaan Level 1

                    Ok thanks,


                    what I'm trying to do with the last piece of code is send all th objects attributes to a php script to be written to a db.


                    so I could write


                    objSend.companyX = child.x;

                    objSend.companyY = child.y;

                    objSend.sloganX = child.slogan.text;


                    etc, etc


                    but as there will be a variable number of objects on the stage (created by user) I don't know which ones will be present at runtime.


                    So by getting all the children of the canvas I then want to pass each of the childrens variables into the objSend


                    so is there any way of coding


                    objSend.(child.id+"X") = child.x;



                    • 7. Re: Multiple attributes
                      Michael Borbor Level 4

                      Yes sending the Canvas children will be my choice too, but probably before I send the data to the backend server I'd only select the relevant object properties, I'd loop through the canvas children using a for each loop, then for instance I'd only choose position, size, padding and so on. To avoid adding overhead to the server and consuming extra bandwith.

                      • 8. Re: Multiple attributes
                        Seglespaan Level 1

                        but whats the syntax fo


                        objSend.(child.id+"X") = child.x;



                        I keep getting an error


                        Cannot assign to a non-reference value.

                        • 9. Re: Multiple attributes
                          Michael Borbor Level 4

                          Which is the purpose of this line of code?


                          objSend.(child.id+"X") = child.x;

                          This definitively will raise an error.

                          • 10. Re: Multiple attributes
                            Seglespaan Level 1

                            Well in my project I have the following code




                            //create object to be sent to php code on server.

                              var objSend:Object = new Object;

                                           //create array of all children on userDesignPanel
                                            var udpChildren = userDesignPanel.getChildren();


                                           //loop through array
                                            for each(var child:* in udpChildren)

                                              objSend.(child.id+"X") = child.x;




                            so say the first child is called userCompany, I want to pass the x an y values of user company.


                            I could write


                            objSend.userCompanyX = child.x;

                            objSend.userCompanyY = child.y;


                            which as far as I know passes the following to the server


                            userCompanyX = 50;

                            userCompanyY = 500;


                            with the userCompanyX being a key to the value 50; and userCompanyY being the key to the value 500


                            So the purpose of the line


                            objSend.(child.id+"X") = child.x;


                            is to dynamically create a key to the value child.x based on the child's ID

                            • 11. Re: Multiple attributes
                              Michael Borbor Level 4

                              Something like this


                              <?xml version="1.0" encoding="utf-8"?>
                              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
                                             import mx.controls.Text;
                                             private var num:Number=1;
                                             private function addCustomChild():void{
                                                  var a:Text=new Text();
                                                  a.name="myText" + num;
                                             private function testCustomChild():void{
                                                  var tmpChildren:Array=myPanel.getChildren()
                                                  for (var i:Number in tmpChildren){
                                   <mx:Panel id="myPanel"  />
                                   <mx:TextArea id="myTextArea" />
                                   <mx:Button label="Add Child" click="addCustomChild()" />
                                   <mx:Button label="Test" click="testCustomChild()" />
                              • 12. Re: Multiple attributes
                                Seglespaan Level 1

                                Finally found what i was looking for,


                                objSend[child.id+"X"] = child.x;



                                when this line of text is contained within the loop its the same as writing



                                objSend.object1X = object1.x;

                                objSend.object2X = object2.x;

                                objSend.object3X = object3.x;



                                Thanks for all you help