I have created a class called LoadXML that loads an XML file and parses the data returning various arrays consisting of url paths: background urls (called “hintergrund”), image urls (“pfad”) etc. In the main class I am trying to load each of these background urls and insert them each in a seperate cover (for a CoverFlow carousel). The images will then be loaded in the same cover and should appear on top of the background.
I havent been able to insert multiple images in one bitmap. This is what my image loading methods look like:
(I also added my LoadXML class and xml file in the attachments).
public class Main extends Sprite
{
//____________________________________________________________________________Vars : Newly created
public var images : Vector.<DisplayObject>;
private var loaders : Vector.<Loader> = new Vector.<Loader>();
private var loader : URLLoader = new URLLoader();
private var backgr : Bitmap;
private var img : Bitmap;
private var titel : String;
//____________________________________________________________________________Vars : From other classes
public var coverflow : CoverFlow;
private var bg : BG;
private var sb : ScrollPaneExample;
private var sl : ScrollLabels;
private var productSummaryLabel : ProductSummaryLabel;
public var loadXML:LoadXML;
private var xmlData : XML;
private var hintergrund : Array;
private var pfad : Array;
private var maxProducts : int;
private var numOfProducts : int;
private var numOfCategories : int;
//____________________________________________________________________________Constructor
public function Main()
{
trace("Step 0: New LoadXML being loaded in Main class");
loadXML = new LoadXML();
loadXML.addEventListener( Event.COMPLETE, onBackgrLoad);
trace("Test: XML Load Number of Products (Main class): " + numOfProducts);
}
//____________________________________________________________________________Methods
// Loads the backgr for each image.
private function onBackgrLoad(e : Event) : void
{
var backgrLoader : Loader = new Loader();
var xmlData : XML = loadXML.getXMLData();
var hintergrund : Array = loadXML.getBackgr();
var numOfProducts : int = loadXML.getNumOfProducts();
trace("Number of Products (Main class): " + numOfProducts);
trace("Hintergrund paths (Main class): " + hintergrund);
// On completetion of this method or in case of an error do the following:
backgrLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
backgrLoader.addEventListener(IOErrorEvent.IO_ERROR, handleXMLError); // try takin this out: contentLoaderInfo.
trace("Step 5: Backgr images now being loaded.");
// A summary should appear for each object once it is in the center
for (var i : int = 0; i < numOfProducts; i++)
{
var backgrRequest : URLRequest = new URLRequest(hintergrund*);
try
{
backgrLoader.load(backgrRequest);
} catch (error: ArgumentError) {
trace("An Argument Error has occurred.");
} catch (error: SecurityError) {
trace("A Security Error has occurred.");
} catch (error: Error) {
trace("An Error has occurred.");
}
trace("onBackgrLoad Loop: " + i);
// RELEVANT SECTION : NOT WORKING
var myNewBackgr = e.target.loader; // Necessary?
// Adds the backgr to the already exisiting backgrs inside the Bitmap "backgr"
backgr += e.target.content as Bitmap;
// Tried other methods but didnt work:
// Method 1
// var backgrObj : Object = backgrLoader.data as Object; - how do i turn this into a Bitmap later?
// Method 2
// var backgrBitmap : Bitmap = e.target.data as Bitmap;
// Mthod 3
// var backgrBitmap : Object = (e.target as ImageMultiLoader).data
// // determine imagemultiloader!!
}
}
// Load images that will lie above backgr images. When event is completed call onTextLoad().
public function onImageLoad(e : Event) : void
{
var imageLoader : Loader = new Loader();
var xmlData : XML = loadXML.getXMLData();
var pfad : Array = loadXML.getPath();
var numOfProducts : int = loadXML.getNumOfProducts();
trace("Number of Products (Main class): " + numOfProducts);
trace("Pfad paths(Main class): " + pfad);
imageLoader.addEventListener(Event.COMPLETE, onImageLoaded);
imageLoader.addEventListener(IOErrorEvent.IO_ERROR, handleXMLError);
trace("Step 6: Images now being loaded.");
for (var i: int = 0; i < numOfProducts; i++)
{
var imageRequest : URLRequest = new URLRequest(pfad*);
try
{
imageLoader.load(imageRequest);
} catch (error: ArgumentError) {
trace("An Argument Error has occurred.");
} catch (error: SecurityError) {
trace("A Security Error has occurred.");
} catch (error: Error) {
trace("An Error has occurred.");
}
trace("onImageLoad Loop: " + i);
// RELEVANT SECTION : NOT WORKING
var myNewImage = e.target.loader; // necessary?
// Hold the images inside the Bitmap
img += e.target.content as Bitmap;
trace("onImageLoad Loop: " + i);
}
}
public function onImageLoaded(event : Event) : void
{
// var xml : XML = new XML(event.target.data);
backgr.filters = [new DropShadowFilter()];
// Turn xml data into two seperate Bitmap images and put this in a cover
// Insert all the images on top of each other in a single cover
var cover : Sprite = new Sprite();
cover.addChildAt(backgr, 0); // At the back
cover.addChildAt(img, 1); // On top
backgr.x = -backgr.width / 2;
backgr.y = -backgr.height / 2;
cover.graphics.drawRect(backgr.x, backgr.y, backgr.width, backgr.height);
images.push(cover);
// Initiate the rest of the methods
// The nr of images on coverflow should not be more than number allowed on stage.
if(images.length <= maxProducts)
{
initCoverflow();
initProductSummaryLabel();
}
else
{
trace("More images than the maxProducts stated by user. Please increase the maxProduct nr or delete some images.");
}
}