Search code examples
actionscript-3flashaddchild

Loading more than one child image AS3


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

Solution

  • 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