Asynchronous loading of Assets not working

I am trying to use the async functions of the Assets class with no luck (loadText/loadBitmapData/etc.) In the project.xml file my asset tag looks like so:

	<assets path="assets" include="*" exclude="*.swf|*.fla" embed="false" type="template" />

and in my code, I have this clause:

			if (Assets.exists(src)) {
				//must be embedded
				var x = Assets.getText (src);
				loadedItems.set("xml",x);
				this.sendNotification(ApplicationFacade.ASSETS_LOADED,"xml");
			
			} else {
				//try to load asynchronously
				trace("attempting xml load:"+src);
				Assets.loadText(src)
					.onComplete(function(theText){
						trace("We got the config xml.");
						loadedItems.set("xml",theText);
						//trace("loaded:"+x);
						this.sendNotification(ApplicationFacade.ASSETS_LOADED,"xml");
					});
					

			}

This compiles just fine (HTML5) and when it executes, it will trace “attempting xml load:” but there is no network activity in the browser, and no error message. Any thoughts?

I believe type “template” is ignored. Could you try and use type “text”, “bytes” or leave type undefined, so it detects automatically?

Thanks! Okay this gets me going somewhere. After removing the type attribute, Lime reported that “the asset exists but only asynchronously”, which means that my first test using “exists” was going to ring true whether I embedded or not. I reduced it to use just the async part and the file loaded. I have the XML file loading okay but I think my onComplete handler for bitmaps isn’t quite right. I’ve simplified it as follows:

Assets.loadBitmapData(id,true)
.onComplete(function(imageData){
	var b=new Bitmap (imageData);
	addChild(b); //supposing this is a Sprite
});

This isn’t working right now. Am I using the imageData variable right? I tried using imageData and imageData.image but my bitmap isn’t displaying.

Side note: there’s mention of “template” asset type here.

This is HTML5, right?

Are there are any console errors, such as cross-domain issues with the image?

Perhaps you should add some trace messages, like:

Assets.loadBitmapData (id).onComplete (function (bitmapData) {
    trace (bitmapData);
    var bitmap = new Bitmap (bitmapData);
    trace (bitmap.width);
    addChild (bitmap);
});

…just to be sure that it looks correct

Thanks again for your reply. I did do some tracing and there appears to be valid bitmapData obtained. I think my problem could be related to adding the asset to an existing Sprite in the display hierarchy, or related to the MVC framework I’m using. Your code does in fact work if I add the asset at the root level. I’ll report back when I learn more. Hopefully it’s not just another dumb mistake I made. :slight_smile:

I have uncovered a bug which explains why my bitmap was not displaying.

EDIT: this was my own bug, so I’m removing this text for the sake of future readers.

Perhaps there is code that handles the sprite.width/sprite.height, but before it has any children?

If you do a blank OpenFL project, do you see a way to reproduce the problem you have? If I took your above sample (in isolation), do you think it would reproduce the issue?

Thanks Joshua I found my offending code. I hope you didn’t waste too much time on me. :slight_smile:

Hey! No problem, glad to help (and to hear it’s working) :success: