2 Replies Latest reply on Sep 23, 2010 7:18 AM by Scaljeri

    State doesn't change 'source' of mx:Image

    Scaljeri Level 1

      Hi All

       

      I have this image which depending on the state should display a different image. Here is the code

       

      <mx:states>
              <mx:State name="state1">
                  <mx:SetStyle name="source" target="{background}">
                      <mx:value>
                          @Embed(source='/assets/images/img1.png')
                      </mx:value>
                  </mx:SetStyle>
              </mx:State>
              <mx:State name="state2">
                  <mx:SetStyle name="source" target="{background}">
                      <mx:value>
                          @Embed(source='/assets/images/img2.png')
                      </mx:value>
                  </mx:SetStyle>
              </mx:State>
          </mx:states>

       

          <mx:Image id="background" />

       

      I have a creationComplete call to 'init' which sets the state like:

       

      private function init():void

      {

          background.currentState = 'state2' ;

      }

       

      However it doesn't work, no image is shown. Any suggestions what goed wrong ?

       

      Thnx

      Luca

        • 1. Re: State doesn't change 'source' of mx:Image
          BhaskerChari Level 4

          Hi Scaljeri,

           

          Use this.currentState = 'state2' ; but not  background.currentState = 'state2' ;

           

          Also you need to use SetProperty and not SetStyle...

           

          <mx:SetProperty

           

           

          name="source" target="{background}">

           

          <mx:value>

          @Embed(source='img1.jpg')

           

          </mx:value>

           

          </mx:SetProperty>

           

          .......................................................................................... ...............................................................

           

           

          <mx:states>
                  <mx:State name="state1">
                      <mx:SetProperty name="source" target="{background}">
                          <mx:value>
                              @Embed(source='/assets/images/img1.png')
                          </mx:value>
                      </mx:SetProperty>
                  </mx:State>
                  <mx:State name="state2">
                      <mx:SetProperty name="source" target="{background}">
                          <mx:value>
                              @Embed(source='/assets/images/img2.png')
                          </mx:value>
                      </mx:SetProperty>
                  </mx:State>
              </mx:states>

              <mx:Image id="background" />


          private function init():void
             {
                 this.currentState = 'state2' ;
             }

           

          Thanks,

          Bhasker

          • 2. Re: State doesn't change 'source' of mx:Image
            Scaljeri Level 1

            thats it, thanks a lot!!