Copy link to clipboard
Copied
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.
1 Correct answer
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thank you.
I'll try to add this following the example.
Copy link to clipboard
Copied
you're welcome.

