Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adding Image to Simple Button Dynamically Using AS3

Contributor ,
Aug 25, 2013 Aug 25, 2013

Hi,

I need some help trying to figure out how to dynamically add an image (PNG or JPEG) that I can place in the library to a simple button also dynamically created in AS3.

Is there a way to (1) add the image instead of using a text label and have it centered in the button?

Here's the AS3 for the simple button without the image (currently uses text label but would prefer if possible to substitute an image for the text:

       

        var mc2:MovieClip = new MovieClip();

        mc2.addChild( bgRed2 );

        mc2.addChild( txt2 );//currently uses text label; would prefer to use an image istead of text

        var mc2a:MovieClip = new MovieClip();

        mc2a.addChild( bgRed2a );

        mc2a.addChild( txt2a );currently uses text label; would prefer to use an image istead of text

        var clearBtn:SimpleButton = new SimpleButton();

        clearBtn.upState = mc2;

        clearBtn.overState = mc2a;

        clearBtn.downState = clearBtn.upState;

        clearBtn.hitTestState = clearBtn.upState;

        clearBtn.x = 0;

        clearBtn.y = 0;

        addChild( clearBtn );

        clearBtn.x = 55;

        clearBtn.y = stage.stageHeight-clearBtn.height;

Any help appreciated.

TOPICS
ActionScript
4.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 25, 2013 Aug 25, 2013

assign your image a class name (eg, Img1). you can then use:

var img1D:BitmapData=new BitmapData();

var img1:Bitmap=new Bitmap(img1D);

cleanBtn.upState=img1; // for example, button's upstate is the image.

// if you wanted some background and the image centered on the background, create your background (sprite or movieclip), use addChild to add img1 to your background and center it.  then assign your button's upState etc to be your background

Translate
Community Expert ,
Aug 25, 2013 Aug 25, 2013

assign your image a class name (eg, Img1). you can then use:

var img1D:BitmapData=new BitmapData();

var img1:Bitmap=new Bitmap(img1D);

cleanBtn.upState=img1; // for example, button's upstate is the image.

// if you wanted some background and the image centered on the background, create your background (sprite or movieclip), use addChild to add img1 to your background and center it.  then assign your button's upState etc to be your background

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 25, 2013 Aug 25, 2013

Thank you for this suggestion.

I'm not sure, using a pure AS3 editor, how to assign a class to the image?

Is there a way to use pure AS3 to assign a class to the image?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 25, 2013 Aug 25, 2013
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 25, 2013 Aug 25, 2013

Thank you.

I'll try to add this following the example.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 25, 2013 Aug 25, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines