Critter’s Code

This is a picture of a bridge and a city. I like bridges and cities. They make me smile.

Jan-17-2008

Changing button icons at runtime using Actionscript

I had to do quite a bit of searching around this morning to figure out how to change the icons on my video player at runtime.

phoenix-032   phoenix-031

I thought I could easily modify the [button].icon property, but I could not access that at runtime. You can, however, do the following:

//    embed your icons
[Embed(source='com/images/play.png')]
[Bindable]
public var imagePlay:Class; 

[Embed(source='com/images/pause.png')]
[Bindable]
public var imagePause:Class;

//    using one button to toggle play and pause of video
private function playpause():void
{
    if (seesmicVideo.playing)
    {
        seesmicVideo.pause();
        btn_play.setStyle("icon",imagePlay);
    }
    else
    {
        seesmicVideo.play();
        btn_play.setStyle("icon",imagePause);
    }
}        

Using [button].setStyle() will allow you to change the icon at runtime.

Posted under AIR, ActionScript, Flex, Samples
  1. Marian Said,

    And if I can’t embed the icon ’cause I don’t have it yet?
    setStyle method takes a Class to set the icon, and I have an Image object that I’ve loaded dinamically…

  2. Phil Molaro Said,

    Saves the day. Critter rocks as usual! Thx man!

Add A Comment

« Back to text comment