简体   繁体   中英

Create a button with an icon in actionscript

I want to create buttons with icons in Flex dynamically using Actionscript.

I tried this, with no success:

var closeButton = new Button();
closeButton.setStyle("icon", "@Embed(source='images/closeWindowUp.png");

I found an answer that works for me. In my .mxml file, I create Classes for the icons I will use:

// Classes for icons
[Embed(source='images/closeWindowUp.png')]
public static var CloseWindowUp:Class;
[Embed(source='/images/Down_Up.png')]
public static var Down_Up:Class;
[Embed(source='/images/Up_Up.png')]
public static var Up_Up:Class;

In the Actionscript portion of my application, I use these classes when dynamically creating buttons:

var buttonHBox:HBox = new HBox();
var closeButton:Button = new Button();
var upButton:Button = new Button();
var downButton:Button = new Button();

closeButton.setStyle("icon", SimpleWLM.CloseWindowUp);
buttonHBox.addChild(closeButton);

upButton.setStyle("icon", SimpleWLM.Up_Up);
buttonHBox.addChild(upButton);

downButton.setStyle("icon", SimpleWLM.Down_Up);
buttonHBox.addChild(downButton);

You can use this one option of dynamic change of button icon.

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);
    }
}        

错误在引号中, @Embed周围应该没有引号:

closeButton.setStyle("icon", @Embed(source="images/closeWindowUp.png"));

I was able to use an icon in my button with the following code:

<mx:Button id="buttonPlay" label="Play" click="playButtonClicked();" enabled="false" icon="@Embed('./play.png')"/>

the file play.png is in the same folder of the mxml file.

I am using Flash Builder version 4.6.

Edit: the question was about ActionScript and not MXML, but I leave this answer just for reference.

I'm assuming you're adding it to the stage?

Also, I think your Embed is missing a close quote / paren.

closeButton.setStyle("icon", "@Embed(source='images/closeWindowUp.png");

should be:

closeButton.setStyle("icon", "@Embed(source='images/closeWindowUp.png')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM