I have an application which pulls in Bitmap resources from a server - currently I use the Loader class to do this, then, once they're loaded, generate a BitmapData based on the loader dimensions and draw the instance of Loader directly to it (the BitmapData is used for Away3D textures as well as Bitmap instances, so I have no need for the Loader once fetched).
This has always worked for me, but recently I started getting 0x0 Loaders, and invalid BitmapData as a result.
I stopped doing this:
this.imageBitmap = new BitmapData(this.imageLoader.width, this.imageLoader.height, true, 0);
..and started doing this:
this.imageBitmap = new BitmapData(event.target.content.width, event.target.content.height, true, 0);
Where event is the Event.COMPLETE event fired by the loader. This fixed the dimension problem, but the BitmapData is just a plain white bitmap (and it's set to transparent by default, so this is being drawn into it). Frustratingly, this doesn't happen every time, if I refresh the application it works as it should around 25% of the time, otherwise it plays up like this.
I've got a tight deadline and I'm really screwing about this, if anyone could help or suggest a better way of doing it you'd really be saving my neck!
Sounds like you need to adjust the image decoding policy for the loader - to ensure it decodes the image before COMPLETE fires - then the width and height etc should be reliable.
To do it, just add a suitable LoaderContext object to the Loader.load method:
var loaderContext:LoaderContext = new LoaderContext();
//set decode policy
loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;
//load image
loader.load(yourUrl, loaderContext);
The default decode policy is ImageDecodingPolicy.ON_DEMAND - which doesnt decode the image until it is actually required.
Lang docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ImageDecodingPolicy.html
Fixed it, stupid oversight and bit of an obscure situation but I'll leave an answer in case anyone runs into something similar.
My loader is contained in an Asset class - when another object requires the internal bitmap, it queries this class - if the bitmap's present, it returns it, if not it loads it with a loader and registers a callback for a COMPLETE event which is fired when the Loader has loaded and transferred its contents to a BitmapData instance.
The stupid mistake I'd made was that, if several objects were querying the same (as-yet-unloaded) asset, it would start reloading the asset each time, creating a new Loader as it did so...so when the first Loader was complete, it would fire an event but no reference to it would exist, not only creating a memory leak but causing the Asset class to extract the BitmapData from the most-recently-created Loader, which was not complete! The asynchronous nature of Loader is the reason it worked sometimes, as on occasion the final Loader would be ready in time for BitmapData extraction.
Simple solution was to create an internal boolean, _isLoading, which is set to true the first time load() is called - any subsequent calls are ignored if it's true, but callbacks still registered, works a treat!
Related
I have "Question.swf", which was created from "Question.fla". Note that Question.fla has no Document Class associated with it. (Note that this is legacy content, and there are over 14,000 variants of "Question.swf"; changing all these is not a viable option.)
Now I have my main Flash application, which loads in Question.swf at runtime. I know that Question.swf has a "Document Class" automatically created which represents the entire "stage" of the SWF (and that it's named "MainTimeline"). I want this application to be able to instantiate multiple instances of that Question.swf Document class... how can I?
I've been working with Flash/AS3 since 2006 (I'm very familiar with loading/using external content, the ApplicationDomain, etc.), but I find that I have no idea how to do this!
Things I've tried which haven't worked include querying the relevant ApplicationDomain with hasDefinition( "Question_fla.MainTimeline" ) - this returns false - as well as running getQualifiedClassName() on my loader.content object - this just returns MovieClip.
I'm not sure how to duplicate the main content of the Loader. However, a reasonable workaround might be to load the SWF bytes once and create multiple Loaders from those bytes:
Load your SWF bytes with a URLLoader:
var urlloader : URLLoader = new URLLoader();
urlloader.load(new URLRequest("your url here"));
Once it is loaded, use the bytes to instantiate new display objects:
var loader : Loader = new Loader();
loader.loadBytes(urlloader.bytes);
Use your loaded loader's loader.content display object on the display list (or the loader itself).
I have an object (MovieClip) on stage at some frame, and at the next frame, even though the object is visually removed, it is still there (it has an internal function that generates something on stage periodically, and the stage objects are being generated). I know I can simply stop the action while leaving the frame, but the object will eat up memory and maybe even CPU time for some background actions (and this is a AIR to iOS project so I care about memory and performance). How can I get rid of the object entirely? I expect everything to be removed when I navigate to another frame if it's a designer-placed object (if the object is not generated by ActionScript) and this is the case, but it only gets visually removed.
Thanks,
Can.
Just null your object reference and it's listeners.
...
myMovieClip.removeListeners();//class function
myMovieClip.parent.removeChild(myMovieClip);
myMovieclip = null;
...
how to get the time which tooks the flash object to load inside the browser, from the creation of the object untill it's ready state ?
If you just want to measure the loading time of the Flash object, you can just use FireBug. But if you want to know when a Flex application is completely ready for usage (i.e. all RSL's loaded; all initial data loaded), I think there are two approaches. One within the app, using a custom preloader to start timing and stop timing when your conditions are met. The other through JavaScript and ExternalInterface. Of which I reckon the latter will give you the most accurate result, because there will probably already be a delay before the preloader is loaded.
JavaScript and ExternalInterface
I have never done this so I'm just going to explain my thoughts. In Javascript you create a 'date' object when the Flash object starts loading. You're probably using SWFObject to inject the Flash object into the page, so you can probably hook up somewhere in there. Then inside your Flex application, when the necessary conditions are met (you define what those are), you use the ExternalInterface.call() to tell JavaScript that the Flex app is ready. Make a new 'date' and subtract the first 'date', and you have your loading time.
More info on using ExternalInterface can be found in the docs: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cb2.html
more detail
Create the JavaScript function that we'll call when the app is ready:
function onFlexAppReady() {
var end = new Date().getTime();
var loadTime = end - start; //in ms.
}
Now in the code that was generated by FlashBuilder, add the start time before the swf is injected:
var start = new Date().getTime();
swfobject.embedSWF(
"MyFlexApp.swf", "flashContent",
"100%", "100%",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
Lastly, in your Flex app, when the right conditions are met, call the JavaScript function:
ExternalInterface.call("onFlexAppReady");
Again, this is all untested code, but it should get you started.
Why this works on flash professional's debugger, but brings null on the compiled SWF?
var firstParameter:SomeObject = new SomeObject();
someLoader = new Loader();
someLoader.contentLoaderInfo.addEventListener(
Event.COMPLETE
, function(evt) {
onLoaded(evt, firstParameter);
}
, false
);
function onLoaded (evt:Event, param:SomeObject):void {
mcOnSceneForTracing.text = param; // this is used for SWF debugging
}
For the record:
To make it work without any issues this can be "solved" by creating a separate scope. However, here I'm wondering why, then, this example even works on the debugger at least.
And, please, if you have a better way other than using two anonymous functions to pass parameters, variables, values, whatever through an Event, do tell! I'm not willing to extend the Event, tho. Too 2005.
mcOnSceneForTracing is what I'm using to "trace" outside the debugger. Suggestions are also accepted here for better (and simpler) ways to do it! I've heard Vizzy is good, but haven't tried it yet.
My guess would be: When loading your resource from the debugger player, the operation finishes instantly, and thus firstParameter is available when your anonymous listener function is called, but when running the swf elsewhere, the load operation takes longer, and then the reference to firstParameter is lost, since it is a local variable.
I created a new class with private vars and public get properties.
When I create a new instance of the class, it loads text files content to the private vars - it probably takes a bit of time to load it.
After the new instance created, I try to get the value of the private var with the get property:
var item1:MyItem = new MyItem("0001");
trace(item1.ItemName);
Well, The output is blank.
The string that ItemName points to is not undefined, it contains data.
So, it's like a timing issue, and ActionScript is probably running the code using background multi-threading, so it's calling the trace command before it finished to run all methods in the MyItem c'tor (methods that load the text file data into the String var that ItemName points to).
Is there any way to force ActionScript avoid using this unwanted "background multi-threading", and run the code normally (by the order of the commands)?
I mean like "Don't run the 2nd command until you finished running the 1st one".
Thanks for any help..
Freddy
It's not multi-threading, loading a text file externally is just a completely asynchronous operation. Within your MyItem class, you need to have an Event.COMPLETE handler for that Loader. From there, there here is what I'd do:
Option 1: In MyItem's COMPLETE handler for the file load, set a flag. The class that uses the getter must check the flag via another getter to see if the data is there, and either use it (if it's there) or set up a listener to wait for it (if it's not). Once the data is loaded, you have immediate synchronous access to it (it's cached).
Option 2: An alternative to keeping the "isLoaded" flag within MyItem would be to have MyItem's Loader COMPLETE handler dispatch it's own TEXT_LOADED event. Then, the instance responsible for creating the MyItem instance would listen for that, and know not to ask for the contents of that text file until it was there.
Either approach will work. It is up to you to figure out which one makes sense. My first option potentially avoids an unnecessary listener if you don't expect to access the loaded data right away (i.e. you're loading the text at startup for use sometime later). The second approach makes sense if you do expect to make use of the text file's data the instant it loads.
Ideally what you'd want to do is implement the Observer Pattern:
Add an event handler to your MyItem class for the Loaded event of whatever component/class you're using to load your XML.
Add an event to your MyItem class that will bubble the Loaded event from Step 1 so that clients of your MyItem class can attach a handler to it. Fire this event inside the Loaded handler you created in Step 1.
Attach an event handler to the event you created in Step 2 and place your trace code in that handler.
Your code is in fact running in a single thread, however the typical data loading classes in Actionscript are asynchronous. It is likely that the data has not loaded before you call trace. To be notified of when the data is loaded you should use an event listener on the loading object.
var loader:URLLoader = new URLLoader();
loader.addEventListener( Event.COMPLETE, onLoadComplete );
loader.load( new URLRequest( 'path_to_data.xml' ) );
function onLoadComplete( event:Event ):void
{
trace( loader.data );
}
The AS3 API docs have a more complete example.