FLEX: swfloader component not firing complete event - apache-flex

enter code hereBeen really frustrating just trying to load a swf from a swfloader in Flex and then accessing it's content. I'm sure once I figure out how to do it once then it'll be fine, but I've gone thorugh a bunch of tutorials and somethings not right. Here's my flex code:
and the as code:
public function initHeaderSwf():void
{
trace("INITIATIED");
//trace(headerSwf.content['weekNo'].text = "TWRTWERWEWE");
headerSwf.addEventListener(Event.COMPLETE, onHeaderLoaded);
}
private function onHeaderLoaded(evt:Event):void
{
trace("LOAD YOU JERK! = " + headerSwf.content['weekNo'].text);
}
Here's the problem:
The swf loads, there is no problem there. I see it in my compiled swf. but the complete event doesn't fire. CreationComplete will fire, and then I try, after that, to make a event listener for loading the actual swf, that doesn't fire. I've tried it a variety of different ways with no luck.
All I am trying to do is change a text field in the loaded swf. I imagine this might be a problem with the sequence of things loading or something like that, but not sure.

When are you adding the event listener? Is it possible you are adding the listener after the event has already fired?

Related

How to add Mate Listeners using Action Script

in one of my forms, I have used the following code for adding Mate Listeners
<mate:Listener type="{DBEvent.Update_Result}" receive="{onUpdateResults(event)}"/>
I am displaying this form as a popup. What happening is, for the first time, onUpdateResults method executed only once as expected. Then i close the popup and reopened it. This time onUpdateResults method called twice, then next 4time and so on...
After so much of googling, i found that Mate Listeners are still active, even though we remove/close the popup. I tried weak references, close the popup using PopupManager.RemovePopup and so on. Nothing worked.
Then i thought of registering and unregistering the mate listeners manually using action script. So, i have tried the following code
var _listener:Listener= new Listener();
_listener.addEventListener(DBEvent.Update_Result,onUpdateResults);
to unregister...
_listener.removeEventListener(DBEvent.Update_Result,onUpdateResults);
But this is also not working.
Please somebody help me fix this issue.
Try this.
Create runtime popup windows in main application:
PopUpManager.createPopUp(this, MyPopUp, true);
MyPopUp - mx:TitleWindow from MyPopUp.mxml
Add any listeners in OnInit in MyPopUp.mxml:
_listener.addEventListener(DBEvent.Update_Result, onUpdateResults);
Close popup when you click any button (button's click event or any):
PopUpManager.removePopUp(this);

Unload module event handling in flex

I am developing an AIR app. In the main app I have a module loader 'mainModuleLoader'. I am creating modules as separate mxml files using the <mx:Module> tag. And I am loading such a module in mainModuleLoader dynamically using actionscript. Everything works fine.
For the module which I am creating as mxml files, I would like to know how to add an unload event handler. So that, whenever I call mainModuleLoader.unload(), the unload event hander is triggered from inside the module.mxml. I have tried the following without any success.
<mx:Module creatingComplete="init()" unload="unloadHandler()"/>
<mx:Module creatingComplete="init()" remove="unloadHandler()"/>
The problem with the second statement above is that it triggers the unloadHandler even if some child is removed inside the module. I have also tried to add the following in the action script.
private function init()
{
this.addEventListener(ModuleEvent.UNLOAD, unloadHandler);
}
But, it doesn't trigger on unloading the module. Kindly let me know if I am going wrong somewhere or if there is any other method to attach the event handlers.
Thanks!!
You are part-way there with the remove; just check that event.target is the module before doing your work. It'll ignore the other ones then.
I think you have to handle the unload event of your mainModuleLoader:
<mx:ModuleLoader id="mainModuleLoader" unload="unloadHandler()"/>

Flex SWFLoader Event.COMPLETE not triggering

While trying to load a Bitmap onto a SWFLoader the Event.COMPLETE event is not being triggered
mySWFLoader.source = new Bitmap(Bitmap(someEvent.content).bitmapData);
but if I use a URL as source the complete event is triggered:
mySWFLoader.source = "http://example.com/123.jpg";
Is there some kind of restriction while using Bitmap as source?
I believe if you use data that already exists in memory (which your Bitmap would) then no load operation would happen. It should be usable immediately after construction. I know attaching movies in AS2 worked like that. If it was part of the library you could use it right away and no loading events would happen.
Use
mySWFLoader.addEventListener(Event.ADDED,handleSwfLoadComplete);
It fires when the content is actually created and added to the display list.
I encountered the same issue. The problem is that when you use Bitmap or Class as source, the content is populated only after the Bitmap or class was instantiated.
This happens in swfLoader.loadContent which eventually adds the newly created content to the displayList as the loader's child, and eventually dispatches the ADDED event.

Complete / Progress events aren't firing for Image control with embedded content

I've got the following MXML tag:
<mx:Image id="image" width="100%" height="100%"
source="#Embed('../assets/Test.swf')"
complete="completeHandler(event)"
progress="progressHandler(event)"/>
But for some reason my completeHandler / progressHandler functions aren't being called. The reason I need the complete event is because I want to manipulate the bitmap data once the image has loaded. In creationComplete the bitmap data is still null. Why aren't these events firing?
Edit: The asset is correctly showing in my application - so I know the asset is in the right location (the embed guarantees that at compile time anyway).
Check your asset path. Most probably, the swf is not at the right path or is not getting copied to an assets folder in the debug-build/release-build directory.
So, you just have to add the Event.COMPLETE listener to the loader.contentLoaderInfo directly instead of to the loader. I can't believe this isn't int he docs.
If you're using an embedded asset, the width / height properties are available immediately on the source object:
var mySource:Object = new embeddedClass();
m_myWidth = mySource.width;
m_myHeight = mySource.height;
m_image = new Image();
m_image.source = mySource;
So, you have to create an instance of the source first, then set the source on your image object.
That seems to be the expected behavior here!
From the documentation:
The progress event is not guaranteed to be dispatched. The complete event may be received, without any progress events being dispatched. This can happen when the loaded content is a local file.
So I think this part can explain why no progress events are being caught in your example.
Dispatched when content loading is complete. Unlike the complete event, this event is dispatched for all source types.
Note that for content loaded via Loader, both ready and complete events are dispatched.
For other source types such as embeds, only ready is dispatched.
It clearly says that you should listen for READY events instead of COMPLETE when dealing with embedded sources ;)

Error #2099: The loading object is not sufficiently loaded to provide this information

I have a Flash app made up of AS3 components that I am trying to run in Flex.
In Flash, after the main component is added to the stage, the loader object (loaderInfo.loader) is null which is fine and I handle that.
In Flex, on the applicationComplete event I add the the main component to the stage and the loader object's getter throws an exception - Error #2099: The loading object is not sufficiently loaded to provide this information.
Also, the INIT event, which is dispatched when the properties and methods of a loaded SWF file are accessible, is not firing which is probably part of the problem. But I can't figure out why it is not being dispatched.
Any ideas why the same code has two different results?
Mmm, that seems like a frustrating problem. When you say "main component", I presume you mean the document class in Flash?
I'm not sufficiently knowledgeable about flex to comment on the problem you described, but I can suggest that you try using ADDED_TO_STAGE instead of INIT as your event...
public class MainFlashClass extends Sprite {
public function MainFlashClass() {
addEventListener(Event.ADDED_TO_STAGE, onInit);
}
public function onInit(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onInit);
// do your initialisation code here
}
}
This might work for both scenarios. I've found ADDED_TO_STAGE to be more helpful because it always gets fired, whether the class is already loaded when the swf is executed (like the document class), or if it's being loaded with a Loader.
I'm not sure if this is what's going on with INIT event, but I do know that in flash player 9, which I'm assuming is the version of your SWF? There's a bug with referencing the loader through its own evt target. Basically if you are loading something and you try and access properties of the loader though evt.target.loaderInfo.loader it never can find itself and throws the error you described in your question. I believe it's a known bug for flash player 9 that was fixed with the release of CS4 and flash player 10.
Here's a link to a thread describing some of the problem, hopefully it helps
http://www.actionscript.org/forums/showthread.php3?t=137599
You definitely need to post code so we an see better.
With that said, after addChild are you attempting to grab the loaderInfo for the "main component" or for your mx:Application?
Pseudo
//onApplicationComplete event handler
var myswf:SWFLoader = new SWFLoader();
myswf.load(...);
addChild(nmyswf);
trace(myswf.loaderInfo.loader);
//end onApplicationComplete
Is that what you're doing? If so, you need to add an event listener to your "main component" (assuming an externally loaded swf) to find out when Event.COMPLETE happens.
var myswf:SWFLoader = new SWFLoader();
myswf.addEventListener(Event.COMPLETE, onMySWFComplete);
//..rest of code
Hope that helps. If not, post code.

Resources