Loading more than one child image AS3

181 Views Asked by At

I'm trying to load 2 images in to my flash file but getting an error - new to this AS3 malarkey any help would be appreciated!

Getting error: 5 1151: A conflict exists with definition request in namespace internal.

var myImage:String = dynamicContent.Profile[0].propImage.Url;
var myImage2:String = dynamicContent.Profile[0].prop2Image.Url;

var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;

var myImageLoader2:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage2));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;

if(this.currentFrame == 1) {
     addChildAt(myImageLoader, 2);
}

if(this.currentFrame == 2) {
     addChildAt(myImageLoader2, 2);
}
3

There are 3 best solutions below

2
On BEST ANSWER

It's usually a good idea to move duplicate code into its own function instead of doing copy + paste:

function loadImage(file:String, x:Number, y:Number):StudioLoader{
    var myImageLoader:StudioLoader = new StudioLoader();
    var request:URLRequest = new URLRequest(file);
    myImageLoader.load(request);
    myImageLoader.x = x;
    myImageLoader.y = y;
    return myImageLoader;
}
addChild(loadImage(enabler.getUrl(myImage1),17,0));
addChild(loadImage(enabler.getUrl(myImage2),17,0));

That's not just giving you better structured code but also fixes your duplicate variable definition issue because what's defined locally inside a function stays inside the function.

This might provide some insight:

http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/functions.html

0
On

Additionally, I notice in the second block that you declare myImageLoader2 but you then use the original myImageLoader to do the load request. So even if you do declare a new URLRequest, you wont get both images loaded.

Akmozo's solution has this corrected.

0
On

You can't create a new variable with the same name as an existing one, in your case I'm speaking about your URLRequest request, so to avoid this type of error you can do like this :

var myImageLoader2:StudioLoader = new StudioLoader();
// assing a new URLRequest to an existing var
request = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request);
myImageLoader2.x =17;
myImageLoader2.y = 0;

Or :

var myImageLoader2:StudioLoader = new StudioLoader();
// create a new URLRequest var, so the name should be different
var request2:URLRequest = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request2);
myImageLoader2.x =17;
myImageLoader2.y = 0;