PDA

View Full Version : URLRequest contains NULL!



stephsh
October 20th, 2009, 07:01 AM
Hi,
I posted a msg on this before but my code has changed so much in mean time i thought it simpler to create a new thread.

An xml file contains multiple products each with their corresponding background image, foreground image, text and website. The LoadXML class loads this xml file and creates an array of each image, text and website. The Main class then loads each image from the array of background images in the method onBackgrLoad(). This is done through the use of a for loop which loops through the array of backgrounds (called hintergrund[]) and loads each one in turn. On tracing hintergrund[] I sucessfully receive a string of all the different background image paths. However when I examine the backgrRequest and backgrLoader they both contain null. The images therefore dont appear on stage but neither does an error message appear! The same thing happens for onImageLoad (this should load the foreground image) and onTextLoad.

Ive been stuck on this for SOOOOOOO LOOONG, its driving me crazy lol! :hurt:



(...)
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;
private var cover : Sprite;
// private var textContainer : Container_Text = new Container_Text();

//__________________________________________________ __________________________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 reflection : Reflection;

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()
{
initBG();
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 // will this be a prob seein as class is sprite??
{
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(Ev ent.COMPLETE, onImageLoad);
backgrLoader.addEventListener(IOErrorEvent.IO_ERRO R, handleXMLError); // try takin this out: contentLoaderInfo.

trace("Step 5: Backgr images now being loaded.");

/* Create a template bitmap to hold the image info */
var templateBitmap : Bitmap;
var templateBitmapData : BitmapData;

// Loop through the background urls
// 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[i]);
trace("Hintergrund[i]: " + hintergrund[i]);
trace("Trace backgrRequest: " + backgrRequest);
cover = new Sprite();
// Method 1
// titel = String(e.target.data);

// Method 2
// var titelString : String = titel[i]; // has to be a dynamic string so change this and put in container
// trace(titelString);

// Method 3
// var titel : TextField = event.target.String as TextField; // this is text so import as textfield? can i do this like this?
//titel shud be rotated. this can be done using bevel filter bt atm not supporting this (look at reflection probs)
try
{
backgrLoader.load(backgrRequest);
trace("Trace backgrLoader: " + backgrLoader.content);
} 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);

templateBitmap = Bitmap(backgrLoader.content);
//templateBitmap = e.target.content;
//templateBitmapData = templateBitmap.bitmapData;

/* Attach the template BitmapData to each backgr image */
var backgrBitmap : Bitmap = new Bitmap(templateBitmapData);
cover.addChildAt(backgrBitmap, 0);
// coverHolder.addChild(cover);

// Method 1: target data into bitmap
// var backgrBitmap : Bitmap = e.target.data as Bitmap;
// Adds the backgr to the already exisiting backgrs inside the Bitmap "backgr"
// backgr += e.target.content as Bitmap;
//var myNewBackgr = e.target.loader; // Necessary?


// Method 2: create object
//BAD IDEA to create a bitmap obj since this cant be rotated or tweened
// var backgrObj : Object = backgrLoader.data as Object; or //e.target.data

// Method : Imagemultiloader
// var backgrBitmap : Object = (e.target as ImageMultiLoader).data
// // determine imagemultiloader!!


// Method 3: create template bitmap
///* Create a template bitmap to hold the image info */
// var templateBitmap:Bitmap = e.target.content;
// var templateBitmapData:BitmapData = templateBitmap.bitmapData;
// /* Loop through your tiles */
// for (var a:uint = 0; a < tilesWide; a++)
// {
// for (var b:uint = 0; b < tilesHigh; b++)
// {
// var tile:Sprite = new Sprite();
// /* Attach the template BitmapData to each tile */
// var tileBitmap:Bitmap = new Bitmap(templateBitmapData);
// tile.addChild(tileBitmap);
// tile.x = a * tile.width;
// tile.y = b * tile.height;
// tileHolder.addChild(tile);
// }
// }

// Method 4: Load straight into BitmapData
//var img1:BitmapData = BitmapData.loadBitmap("img2")
//var img2:BitmapData = new BitmapData(img1.width, img1.height, false)

// Method 5: Try with image holder
// Try with an image holder.
// backgrMc = imgHolder;

// var backgr : Bitmap = Bitmap(e.target.content);
//
// try {
// cover.removeChildAt(0); // should i take this out to ensure not just last image loads?
// } catch (error: Error) {
// trace("Catch error here.");
// }
// cover.addChildAt(backgr, 0);
}
}

public function onImageLoad(e : Event) : void
{
(...) }

public function onImageLoaded(event : Event) : void
{
// var xmlProductList : XML = xmlData.produkte.produkt;
// var xml : XML = new XML(event.target.data); // will this work now tht there r 3 things loaded?

// To create a shadow only UNDER the backgr image
backgr.filters = [new DropShadowFilter()]; //can this var backgr be used from above? i put them on public




// Turn xml data into three seperate Bitmap images and put this in a cover
// Insert all the images on top of each other in a single cover
// cover.addChildAt(backgr, 0); // At the back
// cover.addChildAt(img, 1); // On top
// cover.addChildAt(titel, 2); // On top // IS THIS POSSIBLE




//To create a shadow around the side of the whole image and the bottom
// cover.filters = [new DropShadowFilter()];

// Ensure cover is 20px smaller height + width

// coverDisplayX = -backgr.width / 2; // backgr is equal to whole image size so not necessary to speicify whole img
// coverDisplayY = -backgr.height / 2;
// coverDistanceX = -50;
// coverDistanceY = -50;

backgr.x = -backgr.width / 2;
backgr.y = -backgr.height / 2; // Height is too long but I managed to decrease pix to 75%
// cover.graphics.lineStyle(0, 0xFFFFFF); // Border around image
cover.graphics.drawRect(backgr.x, backgr.y, backgr.width, backgr.height);
images.push(cover); // images.push(imageLoader.content);


// 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) // Used to be ==
{
initCoverflow();
}
else
{
trace("More images than the maxProducts stated by user. Please increase the maxProduct nr or delete some images.");
}
}


//__________________________________________________ __________________________Initiations

private function initCoverflow() : void
{
if(coverflow)
{
removeChild(coverflow);
coverflow = null;
}
coverflow = new CoverFlow(images);
coverflow.x = stage.stageWidth / 2;
coverflow.y = stage.stageHeight / 2;
addChildAt(coverflow, 1);
}

(...)
//__________________________________________________ __________________________Error catching


private function handleXMLError( event:IOErrorEvent ):void
{
trace("Fehler beim Aufrufen von XML.");
}
}
}



I also attached my XML file (produkte1) and my loadxml class.

stephsh
October 22nd, 2009, 04:04 AM
Even though the URLRequest still gives back null, my images now load. Due to my badly written for statement only the last image loads. How do I change this? And how do I ensure the onImageLoad loads over the already exisiting cover (atm the onImageLoad statements return an error so I commented it out)?

I rewrote my code to make it look like this:



public class Main extends Sprite
{
//__________________________________________________ __________________________Vars : Newly created

public var images : Vector.<DisplayObject> = new Vector.<DisplayObject>();
private var cover : Sprite;
private var backgrLoader: Loader;
private var imageLoader : Loader;
private var textLoader : Loader;
private var backgrBitmap : Bitmap;
private var imageBitmap : Bitmap;
private var dynamicText : TextField;
// private var textContainer : Container_Text = new Container_Text(); // Not using atm

//__________________________________________________ __________________________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 reflection : Reflection;
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()
{
initBG();
trace("Step 0: New LoadXML being loaded in Main class");
loadXML = new LoadXML();
loadXML.addEventListener( Event.COMPLETE, onBackgrLoad);
loadXML.addEventListener( Event.COMPLETE, onImageLoad);
trace("Test: XML Load Number of Products (Main class): " + numOfProducts);
}

//__________________________________________________ __________________________Methods

// Loads the backgr for each image.
private function onBackgrLoad(e : Event) : void // will this be a prob seein as class is sprite??
{
backgrLoader = new Loader(); // Use loader as opposed to URLLoader???
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(Ev ent.COMPLETE, onBackgrLoaded);
backgrLoader.contentLoaderInfo.addEventListener(IO ErrorEvent.IO_ERROR, handleXMLError); // try takin this out: contentLoaderInfo.

trace("Step 5: Backgr images now being loaded.");

// Loop through the background urls
for (var i : int = 0; i < numOfProducts; i++)
{
var backgrRequest : URLRequest = new URLRequest(hintergrund[i]);
backgrRequest.contentType = "image/png";
backgrRequest.method = URLRequestMethod.GET;

trace("Hintergrund[i]: " + hintergrund[i]);
trace("Trace backgrRequest: " + backgrRequest);

// Load the selected backgr image path, making sure to handle possible errors
try
{
backgrLoader.load(backgrRequest);
trace("Trace backgrLoader: " + backgrLoader.content);
} 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);
}
}

// Load images that will lie above backgr images. When event is completed call onTextLoad().
public function onImageLoad(e : Event) : void
{
imageLoader = 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.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, onImageLoaded);
imageLoader.contentLoaderInfo.addEventListener(IOE rrorEvent.IO_ERROR, handleXMLError);

trace("Step 5.1: Images being loaded at same time.");

for (var i: int = 0; i < numOfProducts; i++)
{
var imageRequest : URLRequest = new URLRequest(pfad[i]);
imageRequest.contentType = "image/png";
imageRequest.method = URLRequestMethod.GET;

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);
}
}

public function onBackgrLoaded(event : Event) : void
{
// Create a new sprite in which to add the backgr image
cover = new Sprite();

// Store current background images as Bitmap
backgrBitmap = event.target.content as Bitmap;

// Insert current image in a cover
cover.addChildAt(backgrBitmap, 0);
trace("Step 6.1: Cover created containing backgrBitmap.");

// Then call the method drawCover - note: not possible to do this thru event listener since already loaded!
drawCover();
}

//public function onImageLoaded(event : Event) : void
{
// Create a new sprite in which to add the backgr image
cover = new Sprite();

// Store current foreground images as Bitmap
imageBitmap = event.target.content as Bitmap;

// Add in cover on top of current cover - does this work??
cover.addChildAt(imageBitmap, 1); // This should draw in the current cover...
trace("Step 6.2: Cover created containing imageBitmap.");

// Then call the method drawCover
drawCover();
}


// Draws the cover fully and initiates the coverflow - why doesnt this appear????
public function drawCover() : void
{
maxProducts = 4; //loadXML.getMaxProducts();
trace("Max Products: " + maxProducts);

// To create a shadow only UNDER the backgr image
backgrBitmap.filters = [new DropShadowFilter()]; //can this var backgr be used from above? i put them on public

backgrBitmap.x = -backgrBitmap.width / 2;
backgrBitmap.y = -backgrBitmap.height / 2; // Height is too long but I managed to decrease pix to 75%
cover.graphics.drawRect(backgrBitmap.x, backgrBitmap.y, backgrBitmap.width, backgrBitmap.height);
images.push(cover);

// If the correct amount of products, initiate the coverflow
if(maxProducts > images.length || maxProducts == images.length)
{
initCoverflow();
trace("Step 7: Init CoverFlow");
}
else
{
initCoverflow();
trace("More images than the maxProducts stated by user. Please increase the maxProduct nr or delete some images.");
}
}

//__________________________________________________ __________________________Initiations

private function initCoverflow() : void
{
if(coverflow)
{
removeChild(coverflow);
coverflow = null;
}
coverflow = new CoverFlow(images);
coverflow.x = stage.stageWidth / 2;
coverflow.y = stage.stageHeight / 2;
addChildAt(coverflow, 1);
}

//__________________________________________________ __________________________Error catching


private function handleXMLError( event:IOErrorEvent ):void
{
trace("Fehler beim Aufrufen von XML.");
}
}
(...)
}