Loading more than one child image AS3

亡梦爱人 提交于 2019-12-11 05:19:37

问题


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

回答1:


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




回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/27580023/loading-more-than-one-child-image-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!