-
(AS3 CS4) Convert String to image name
Code:
function f_choosePhoto(photoNum)
{
var photos:Array = ["photo0","photo1","photo2"];
var path:String = (photos[photoNum]);
var bdata = new this[path](560, 380);
var bmp = new Bitmap(bdata);
addChild(bmp);
}
photoNum is returned when a button is clicked. The Array contains the class names of all the photos stored in the library.
What's supposed to happen is that when the button is clicked, the correct picture is shown on the screen. What I get is a runtime error:
TypeError: Error #1007: Instantiation attempted on a non-constructor.
at Tour1_fla::Photo_3/f_choosePhoto()
at Tour1_fla::MainTimeline/f_showPhoto()
at Tour1_fla::MainTimeline/onMouseClick()
I've cobbled the AS together from a couple of seperate tutorials, and if I replace "new this[path](560, 380);" with "new photo1(560, 380);" it works. So I know I'm doing something wrong right there, but I've no idea what.
Can anyone help? Thanks in advance
-
Prid - Outing
I'm no good with AS3, but try changing this line:
Actionscript Code:
var bdata = new this[path](560, 380);
to this:
Actionscript Code:
var bdata = new this[photos[photoNum]](560, 380);
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Actionscript Code:
//This is an Bitmap copy of a MovieClip from Library function f_choosePhoto(photoNum) { var photos:Array = [photo0,photo1,photo2]; var path:MovieClip = new (photos[photoNum]); var b:BitmapData=new BitmapData(path.width,path.height); var bmp = new Bitmap(b); b.draw(path); addChild(bmp); bmp.x=stage.stageWidth/2-bmp.width/2; bmp.y=stage.stageHeight/2-bmp.height/2; }
f_choosePhoto(0);
arkitx
-
@arkitx - Thanks! It took me a while to figure out what your code does, but I've got it now. It's brilliant, thanks
-
You are Welcome!
Making a class to handle this will be a reusable util code.
arkitx
-
Converting images using Code is too complicated for me. I wonder whether there are some professional image converters can help with that? Any suggestion will be appreciated very much. Thanks in advance.
Best regards,
Arron
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|