(AS3 CS4) Convert String to image name
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: (AS3 CS4) Convert String to image name

  1. #1
    Member
    Join Date
    May 2010
    Posts
    33

    (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

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,808
    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

  3. #3
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    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

  4. #4
    Member
    Join Date
    May 2010
    Posts
    33

    resolved

    @arkitx - Thanks! It took me a while to figure out what your code does, but I've got it now. It's brilliant, thanks

  5. #5
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    You are Welcome!

    Making a class to handle this will be a reusable util code.



    arkitx

  6. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    20
    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
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com