A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Loading external jpg in actionscript 3

  1. #1
    Senior Member QBA's Avatar
    Join Date
    Feb 2001
    Location
    Toronto, Canada
    Posts
    312

    Loading external jpg in actionscript 3

    Hi

    How can I load a external jpg into this movie clip that I created via Actionscript 3?

    var LoaderImagesMC:MovieClip = new MovieClip();
    LoaderImagesMC.x = 0;
    LoaderImagesMC.y = 0;
    addChild(LoaderImagesMC);


    Thanks

    Alex

  2. #2
    Member
    Join Date
    Feb 2008
    Location
    Aylesbury
    Posts
    39
    I'd just been working on loading external images with a tutorial from 'actionscript 3.0 beyond the basics' from Lynda.com

    This works for me:


    var LoaderImagesMC:MovieClip = new MovieClip();
    LoaderImagesMC.x = 0;
    LoaderImagesMC.y = 0;
    addChild(LoaderImagesMC);


    var imageLoader:Loader;
    var goat:XML;
    var goatList:XMLList;
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.load(new URLRequest("data/images.xml"));

    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

    function xmlLoaded(event:Event):void
    {
    goat = XML(event.target.data);
    goatList = goat.children();

    for(var i:int = 0; i < goatList.length(); i++)
    {
    imageLoader = new Loader();
    imageLoader.load(new URLRequest(goatList[i].attribute("thumb")));
    imageLoader.x = 25;
    imageLoader.y = i * 150 + 25;
    LoaderImagesMC.addChild(imageLoader);

    }

    }

  3. #3
    Senior Member QBA's Avatar
    Join Date
    Feb 2001
    Location
    Toronto, Canada
    Posts
    312
    Thanks Joe Meering,

    But that looks like a lot of extra code for what I need, plus why the xml?

  4. #4
    Member
    Join Date
    Feb 2008
    Location
    Aylesbury
    Posts
    39
    I'm sure there's a simpler way. This tutorial was for xml in AS3.0.
    However I'm learning that XML is actually a very easy code. Embrace it!

    A quick search and http://manfred.dschini.org/2007/06/1...ternal-images/ seems to show a different way. Just don't forget to alter LoaderImagesMC.addChild(whatever variable - he uses thumbnail); at the end of the code.

    Joe

  5. #5
    Junior Member
    Join Date
    Feb 2008
    Posts
    24
    Hey. Try something like this:

    PHP Code:
    var myImageLoader:Loader = new Loader();
    var 
    imageURLRequest:URLRequest = new URLRequest("image.jpg");
    var 
    myBitmapData:BitmapData;
    var 
    myImage:Bitmap;

    myImageLoader.load(imageURLRequest);
    mycoverloader.contentLoaderInfo.addEventListener(Event.COMPLETEimageLoaded);

    function 
    imageLoaded(e:Event):void {
      
    myBitmapData = new BitmapData(myImageLoader.widthmyImageLoader.height);
      
    myBitmapData.draw(myImageLoader);
      
    myImage.BitmapData myBitmapData;
      
    addChild(myImage);

    Last edited by KevinDoom; 06-13-2008 at 01:29 PM.

  6. #6
    Junior Member
    Join Date
    Feb 2008
    Posts
    24
    sorry, posted twice by accident.

  7. #7
    Senior Member QBA's Avatar
    Join Date
    Feb 2001
    Location
    Toronto, Canada
    Posts
    312
    Thanks KevinDoom,

    For some reason your code is not working for me

  8. #8
    Junior Member
    Join Date
    Feb 2008
    Posts
    24
    Must've been some typos in there. I just tried the following and it worked:

    PHP Code:
    var imageURLRequest:URLRequest = new URLRequest("image.jpg");
    var 
    myImageLoader:Loader = new Loader();
    myImageLoader.load(imageURLRequest);

    myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETEimageLoaded);
    function 
    imageLoaded(e:Event):void {
       var 
    myBitmapData:BitmapData = new BitmapData(myImageLoader.widthmyImageLoader.height);
       
    myBitmapData.draw(myImageLoader);
       var 
    myBitmap:Bitmap = new Bitmap;
       
    myBitmap.bitmapData myBitmapData;
       
    addChild(myBitmap);


  9. #9
    Senior Member QBA's Avatar
    Join Date
    Feb 2001
    Location
    Toronto, Canada
    Posts
    312
    Thanks KevinDoom,

    Your code now works, but what I really want is not just to load a external image, but to load it via a movieclip sitting on a layer, so I can put a mask on the layer above it.

  10. #10
    Junior Member
    Join Date
    Dec 2002
    Location
    Porto, Portugal
    Posts
    8

    Unhappy

    Quote Originally Posted by KevinDoom
    Hey. Try something like this:

    PHP Code:
    var myImageLoader:Loader = new Loader();
    var 
    imageURLRequest:URLRequest = new URLRequest("image.jpg");
    var 
    myBitmapData:BitmapData;
    var 
    myImage:Bitmap;

    myImageLoader.load(imageURLRequest);
    mycoverloader.contentLoaderInfo.addEventListener(Event.COMPLETEimageLoaded);

    function 
    imageLoaded(e:Event):void {
      
    myBitmapData = new BitmapData(myImageLoader.widthmyImageLoader.height);
      
    myBitmapData.draw(myImageLoader);
      
    myImage.BitmapData myBitmapData;
      
    addChild(myImage);


    hello
    i'm using your code to present images (it's just perfect) but i have textfields on the document that stay under the image, making impossible to read . How can i put the image under the others objects of the document? I'm getting the texts and image from a xml file..

    if anyone knows the answer please help.. thank you
    regards

  11. #11
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Just last week I published a wrapper class to automate image loading - you guys might find it easier.

    @akira: change addChild(myImage) to addChildAt(myImage, 0);

  12. #12
    Junior Member
    Join Date
    Dec 2002
    Location
    Porto, Portugal
    Posts
    8
    thanks neznein9 it worked out just fine x)

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