Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flex  »  Flex 2 and Flash 9 Together?

Flex 2 and Flash 9 Together?

By Hasan Otuome | Published 11/8/2006 | Flex | Rating:
Hasan Otuome
Hasan Otuome is Chief Architect for Marx Media (http://www.marxmedia.net) where he can usually be found developing Rich Internet Applications for the company's clients. He champions creative uses and combinations of Flash, PHP, AJAX and MySQL, among others, for their benefits in user experience improvement. 

View all articles by Hasan Otuome
The Answer...
A question popped up on flexcoders recently about whether these two could play together and here's my response. Yes, it's possible using SWFLoader. The 9 SWF will be accessible/programmable through the content property of the loader. Here's a very basic example:

Flex
========

import flash.events.Event;



private function initSWF(event:Event):void{
    loadedSWF.content.addEventListener("clicked",clickHandler);
}



public function clickHandler(event:Event):void{
    var button:String = loadedSWF.content["clickedButton"];
   
    //do something with the returned data

}


So, the above code can go in a < mx:Script >< /mx:Script > block or in an external ActionScript file that's the source property of your script block, < mx:Script source="external.as" />. First, we import the Event class so we can listen for and dispatch events. Next comes initSWF(). This method is the event handler for the SWFLoader's complete event.

If that was confusing, it's the function that get's called when the complete event fires. So, what we're doing with this method is adding an event listener to the Flash 9 SWF that we load. And, when the loaded SWF fires the "clicked" event we want to handle that with clickHandler(), which right now is just holding a reference to the name of the button clicked. To get the ball rolling, simply make initSWF() the value of the complete property of your SWFLoader component, < mx:SWFLoader  id="loadedSWF" complete="initSWF(event)" source="pathToSWF" />.

Flash

========

import flash.events.*;



var clickedButton:String = "";

someButton.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:Event):void{
   clickedButton = event.target.name;
   
   dispatchEvent(new Event("clicked"));

}


OK, on the Flash side we do our import first of course. Next, we instantiate our clickedButton variable as an empty string (just to keep from being null/undefined). Now, we add an event listener to the someButton instance on the main timeline. This syntax is a much cleaner way of dealing with events IMO because everybody (you, your team, the compiler) knows exactly what you're trying to do.

Here we want to listen for the CLICK event which is a static member of the MouseEvent Class. When we hear the event clickHandler() once again jumps into action. Inside this click handler we do (2) things. First, we set the value of our clickedButton variable to the instance name of the button that was clicked. Then finally, we want to dispatch our custom "clicked" event which, in turn, kicks things in motion on the Flex side.

Again, this is a simple illustration of some of the things you can do using Flex/Flash together. No LocalConnections just pure AS3 conversations. Enjoy...=)
How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent

Verification:
Enter the security code shown below:
img


Add comment

Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • Comment #1 (Posted by VectorG6 - multimediax at hotmail.com)
    Rating
    I found a good example of swfLoader Flex2 control. http://www.bridel.org/flex/example/swfimport/swfloadasdemo.html
     
  • Comment #2 (Posted by jgu - jug51 at free.fr)
    Rating
    The line
    dispatchEvent(new Event("clicked"));
    must be
    dispatchEvent(new Event("clicked",true));

    Thanks...
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.