4 Replies Latest reply on Jul 12, 2011 9:42 PM by William Spence

    Changing Object Properties with State Changes Using Only Actionscript?

    William Spence Level 1

      I need to be able to change the properties of various actionscript objects of an itemRenderer depending on what state the itemRenderer is in.

       

      For instance, I have a textInput control in an itemRenderer that I would like to change the borderColor depending on the State of the itemRenderer.  Usually this is a piece of cake in MXML:

       

      <s:TextInput borderColor.normal = "0xAFAFAF borderColor.hovered="0xFFFFFF" borderColor.selected="0x000000"/>

       

      Unfortunately, due to the high complexity of this particular itemRenderer, it was not possible to create it in MXML so it was necessary to impliment it using Actionscript.  As a result, I do not know how to change the properties of an actionscript object with respect to different states.  I was hoping there was some sort of easy dot syntax that would make it easy like:

       

      myTextInput.normal.setStyle("borderColor", "0xAFAFAF");

      myTextInput.hovered.setStyle("borderColor", "0xFFFFFF");

      myTextInput.selected.setStyle("borderColor", "0x000000");

       

      Unfortunately, I get the feeling it will not be as easy as I am hoping it will be.  My guess is that I will have to add event listeners and handlers to change these properties, but I thought I would ask if there was an easier way before I get started. 

       

      Please let me know if there is a simple way to do this.

        • 1. Re: Changing Object Properties with State Changes Using Only Actionscript?
          CleanCoder Level 2

          This is the basic syntax for setting up the states in Actionscript.

           

          this.states = [
                       new State({
                           name:"normal",
                           overrides:[
                               new SetStyle(myTextInput, "borderColor", 0xAFAFAF);
                           ],
                       }),
                       new State({
                           name:"hovered",
                           basedOn:"normal",
                           overrides:[
                               new SetStyle(myTextInput, "borderColor", 0xFFFFFF);
                           ],
                       })
                   ];

          1 person found this helpful
          • 2. Re: Changing Object Properties with State Changes Using Only Actionscript?
            drkstr_1 Level 4

            You can also listen for a StateChangeEvent.CURRENT_STATE_CHANGE event, then use some control logic to make any updates according to the current state. Using the override system seems akward to me in native AS (as it was intended to be used in declarative MXML), but that is just personal preference.

            1 person found this helpful
            • 3. Re: Changing Object Properties with State Changes Using Only Actionscript?
              CleanCoder Level 2

              Your way probably is cleaner, the overrides dont really do anything that cant be done by setting styles and properties directly.

              • 4. Re: Changing Object Properties with State Changes Using Only Actionscript?
                William Spence Level 1

                Thanks to you both for your very helpful suggestions.  Both would have worked perfectly.  I actually created the states in MXML, but all of the properties of my  objects needed to be changed at runtime using actionscript.  The overrides would have worked, and the StateChangeEvent would have been even easier and cleaner.  In the end, I found a third option that worked best for me.  The states existed in an itemRenderer and I was a little hesitant about how I should add an event listener that didn't keep getting recreated and causing a memory leak with all of the action taking place in the renderer.  So I found that there is an "enterState" method that I ended up using.  It looks like this:

                 

                <s:State name="normal" enterState="enterNormalState(event)"/>

                 

                So in the end, I just used this to create a function for each state where I simply changed the necessary properties that needed changing using actionscript, and I didn't have to worry about the correct way to add the eventlistener.  Thanks so much for taking the time to respond to my question!