Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 1 of 1

Thread: Loading images from an Array generated from XML

  1. #1

    Loading images from an Array generated from XML

    OK, this is my first day trying to use AS3 and I'm kind of confused on how to load an image onto the screen and then be able to resize it, scale it, etc..

    I'm loading up to 5 images in from an XML file that will be generated on a server. (right now I'm just using a test.xml file).

    Normally I would load the images into dynamically created movie clips (whatever0_mc through whatever4_mc generated by code in AS2). But I can't seem to get anything working.. I've looked on the internet and read through some information but I'm still just REALLY confused... this is what I have so far. To make it easier on me in the future, I've put different code on different layers so I can look at it closer.. I've separated the code into the different layers they're on here:

    Code:
    import flash.events.Event;
    import flash.net.URLLoader; // URL Loader Import for Images
    import flash.net.URLRequest; // URL Requests Import for Images
    import flash.display.Loader; // Loader
    import flash.events.ProgressEvent; // Progress for Loader
    import flash.text.TextField; // Imports for Text Fields
    import flash.display.Sprite; // Imports Sprite stuff for loaded Images
    import flash.display.Bitmap; // Imports stuff to display a bitmap
    Code:
    var imgLoader:Loader = new Loader(); // Initialize Image Loader
    
    // Listeners for the Image Loaders //
    function showPreloader(evt:Event):void {
        trace("-- ** showPreloader ** --");
        addChild(loadProgress_txt);
    }
    
    function showProgress(evt:ProgressEvent):void {
        trace("-- ** showProgress ** --");
        var totalLoaded:Number = evt.bytesTotal;
        var currentLoaded:Number = evt.bytesLoaded;
        var thePercent:Number = (currentLoaded * 100) / totalLoaded;
        
        loadProgress_txt.text = thePercent + "%"; // Show bytes loaded
    }
    
    function showLoadResult(evt:Event):void {
        trace("-- ** showLoadResult ** --");
        
    }
    Code:
    // Setup Variables //
    var imageArray:Array = new Array();
    var galleryTitle:String;
    var numPhotos:Number;
    var gallerySubmitter:String;
    
    // XML Info //
    XML.ignoreComments = true;
    XML.ignoreWhitespace = true;
    var galleryXML:XML;
    var xmlLoader:URLLoader = new URLLoader();
    var request:URLRequest = new URLRequest("test.xml");
    xmlLoader.load(request);
    xmlLoader.addEventListener(Event.COMPLETE, XMLComplete);
    
    function XMLComplete(evt:Event):void {
        trace("-- ** XMLComplete ** --");
        var loader:URLLoader = evt.target as URLLoader;
        
        if (loader != null){
            galleryXML = new XML(loader.data);
            
            galleryTitle = galleryXML.children()[0].attributes()[0];
            numPhotos = galleryXML.children()[0].attributes()[1];
            gallerySubmitter = galleryXML.children()[0].attributes()[2];
            
            trace("galleryTitle: " + galleryTitle);
            trace("numPhotos: " + numPhotos);
            trace("gallerySubmitter: " + gallerySubmitter);
            
            for(var i:Number = 0; i<numPhotos; i++) {
                imageArray.push(galleryXML.children()[0].children()[i].attributes()[1]);
                
            } // for(var i:Number = 0; i<numPhotos; i++)
            
            trace(imageArray.toString());
            trace("loading: images\\gallery\\" + imageArray[0]);
            
            var imgRequest:URLRequest = new URLRequest("images\\gallery\\" + imageArray[0]); // Request the Image into the Loader
            imgLoader.load(imgRequest);
            
            imgLoader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
            imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
            imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult);
            
        } else { // if (loader != null){
            trace("loader is not a URLLoader!");
            
        } // if (loader != null){
            
    } // function onComplete(event:Event):void {
    // End XML Info //
    Can anyone point me in the next direction? thanks...

    I've uploaded the files too but you'll have to change out images and the path to the images if you want to test it.
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012