Switch On The Code RSS Button - Click to Subscribe
Jan
3

Flex Tutorial - Runtime Button Icons using Flash symbols

So to get the the new year started off I decided to write a quick tutorial about two items that almost any Flex programmer can take advantage of. One is how to change the icon on a button at runtime and the other is using symbols in a flash .swf file for those icons. The major advantage of using the symbols is that you can use a single flash file to hold all the icons you need for your application. And what application wants to have completely static icons on their buttons? Ok, probably most, but we will ignore the nay sayers so that we can have some fun.

Let's take look at a quick example that shows what we will be learning in this tutorial. We have a very simple application below that has a single large button in the middle that changes its icon. The icon is chosen randomly from three that we have, which were all embedded from a single flash .swf file. Well, what are you waiting for? Start clicking. Also feel free to right click and grab the source files.



The first thing we are going to create the flash .swf file. I built my icons in Flash CS3. Once you open up Flash you can draw your first shape. Below is quick screenshot of Flash with a simple shape drawn.

Star Shape in Flash

Once we have the shape we can convert it to a symbol by selecting the entire object and right clicking and going down to "Convert to Symbol", image below. Then a window will pop up, in which we will put the name of the symbol. We also at this point click the check box about half way down to export to actionscript and give the symbol a class name. This will enable us to use the class name in Flex to embed the specific symbol. Images for both steps are below.

Convert Symbol Selection

Symbol Window Popup

Once we have all that done you can build some more symbols, and once we have all our symbols built we now publish the .swf file by going to file->publish. Now that we have our .swf with our symbols in it we can start building the Flex application.

The first thing we do is build the basic application using a panel, a couple text components and a single button. The text components just hold the informational stuff in the application. And I like using a panel for the nice title header. The button is the component we are going to use throughout the tutorial so I gave it an id also. You can see this below.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="absolute" width="300" height="300">

  <mx:Panel x="0" y="0" width="100%" height="100%"
    layout="absolute" title="Changing Button Icons on the Fly">

    <mx:Text x="10" y="10" width="260" height="53"
      fontSize="18" fontFamily="Arial">

      <mx:text>
        Click the button below to change the icon displayed on it
      </mx:text>
    </mx:Text>
    <mx:Button id="but" x="80" y="71" width="120" height="120"/>
    <mx:Text x="5" y="214" fontSize="9" height="36" width="270">
      <mx:text>
        A random icon is chosen each time the button is clicked
      </mx:text>
    </mx:Text>
  </mx:Panel>
</mx:Application>

Next we will add some script to embed the icons we are going to use. We do this by adding a <Script> tag to the top of the application and adding our three private variables for the icons. To embed a .swf symbol we use an Embed tag above our private var which is of Class type. There are two ways we can embed the symbol. The first method is [Embed(source='assets/button_icons.swf', symbol='square')] which will use the .swf file and pull out the specified symbol; the second method is a shorthand which is [Embed(source='assets/button_icons.swf#square')] which uses the # to specify the symbol. In this application we use the shorthand method and the <Script> is below.

<mx:Script>
<![CDATA[
 
  [Embed(source='assets/button_icons.swf#square')]
  private var icon0:Class;
   
  [Embed(source='assets/button_icons.swf#circle')]
  private var icon1:Class;
   
  [Embed(source='assets/button_icons.swf#star')]
  private var icon2:Class;
]]>
</mx:Script>

The next thing we need to do is initialize the button icon, and we use the icon style to do this. To initialize it we add the icon style to the mxml component declaration by setting the icon attribute to {icon1}. We also want to capture the click event for this button, so we set it to a function which called updateIcon. The updated button mxml is below.

<mx:Button id="but" x="80" y="71" width="120" height="120"
  icon="{icon1}" click="{updateIcon()}"/>

Now we just need to write the updateIcon function. This function will update the icon style on the button to switch it to a random icon. We do this by creating a random number between 0 and 2. Once we have the random number we can use the setStyle function on the button to change the icon style. You can see the new function below.

private function updateIcon():void
{
  var iconNum:int = Math.round(Math.random() * 2);
  but.setStyle("icon", this["icon" + iconNum]);
}

And that pretty much takes care of it. Nothing that complex but the concepts can be used all over the place and can be very useful. Here is the full source code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="absolute" width="300" height="300">

  <mx:Script>
    <![CDATA[
     
      [Embed(source='assets/button_icons.swf#square')]
      private var icon0:Class;
       
      [Embed(source='assets/button_icons.swf#circle')]
      private var icon1:Class;
       
      [Embed(source='assets/button_icons.swf#star')]
      private var icon2:Class;
     
      private function updateIcon():void
      {
        var iconNum:int = Math.round(Math.random() * 2);
        but.setStyle("icon", this["icon" + iconNum]);
      }
    ]]>
  </mx:Script>

  <mx:Panel x="0" y="0" width="100%" height="100%"
    layout="absolute" title="Changing Button Icons on the Fly">

    <mx:Text x="10" y="10" width="260" height="53"
      fontSize="18" fontFamily="Arial">

      <mx:text>
        Click the button below to change the icon displayed on it
      </mx:text>
    </mx:Text>
    <mx:Button id="but" x="80" y="71" width="120" height="120"
      icon="{icon1}" click="{updateIcon()}"/>

    <mx:Text x="5" y="214" fontSize="9" height="36" width="270">
      <mx:text>
        A random icon is chosen each time the button is clicked
      </mx:text>
    </mx:Text>
  </mx:Panel>
</mx:Application>

If anyone has any questions or thoughts please just drop a comment. I hope everyone can take away a little bit from this.



Posted in Flex, All Tutorials by The Fattest |

9 Responses

  1. JBaker Says:

    very cool indeed however I get the same thing more often then not. in other words it changes maybe every 6 clicks.

  2. The Fattest Says:

    The code is actually pseudo-random it will round to one about 2/3rds of the time. So yeah you will get the one much more often.

  3. Tommy Says:

    How would you do this for an image?

  4. The Fattest Says:

    And embedded image or an image loaded at runtime?

  5. Tommy Says:

    The images that I have are stored in a folder. Currently I have hardcoded the image to the flex application. I would like to have more images showing, but the size of the page will not allow this. I want to have one image loaded at runtime and when the user clicks on the image he/she will see the other images.
    I have tried the solution above but the size of the image is bigger and I have no control over the width or height. Does this help with the solution?

  6. sridatta Says:

    Very nice tutorial indeed.. I was searching for the same thing exactly and i found here..

    good work …

  7. Keshav Prasad Says:

    Hi,

    I was looking for loading random images in flex. Great tutorial….
    but is there a way where i can load 100 images from an xml file. Will using an xml with 100 images in it, eat away performance.

    Thanks and Regards,
    Keshav.

Tracebacks / Pingbacks


  1. Bookmarks Tagged Static


  2. PHP Coding School » Blog Archive » php tutorial [2008-01-03 19:10:04]


Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash