Using an external image for a Button icon in Flash with ActionScript 3.0

by Peter deHaan on November 26, 2008

in Button

The following example demonstrates how to load a remote image for a button icon using both a Loader instance and a UILoader component instance.

Full code after the jump.

// ActionScript 3.0
// Import the required component classes.
import fl.controls.Button;
import fl.containers.UILoader;
 
// Create a new Loader instance, and load an external PNG image.
var logo:Loader = new Loader();
logo.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler1);
logo.load(new URLRequest("http://www.helpexamples.com/flash/images/logo.png"));
 
// Create a new UILoader component instance and load an external PNG image.
var logo2:UILoader = new UILoader();
logo2.scaleContent = false;
logo2.addEventListener(Event.COMPLETE, completeHandler2);
logo2.load(new URLRequest("http://www.helpexamples.com/flash/images/logo.png"));
 
// Create a new Button component instance, and add it to the display list.
var myButton:Button = new Button();
myButton.label = "Loader";
myButton.move(10, 10);
myButton.setSize(140, 100);
addChild(myButton);
 
/* Create a new Button component instance, and add it to the display list. */
var secondButton:Button = new Button();
secondButton.label = "UILoader";
secondButton.move(160, 10);
secondButton.setSize(140, 100)
addChild(secondButton);
 
/* Handler function for the Loader instance. This function sets the icon 
   style for the myButton button instance, and updates the button's display
   using the validateNow() method. */
function completeHandler1(evt:Event):void {
    myButton.setStyle("icon", logo);
    myButton.validateNow();
}
 
/* Handler function for the UILoader component instance. This function sets
   the icon style on the secondButton button instance, and updates the UILoader 
   and Button components display using the validateNow() method. */
function completeHandler2(evt:Event):void {
    secondButton.setStyle("icon", logo2);
    logo2.validateNow();
    secondButton.validateNow();
}

For more information on the Flash/ActionScript 3.0 Button component, see the “Using the Button component” Flash Quick Start on Adobe.com.

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: