Flash Communication

Accessing a Flex movie from Flash (AS3)

One of the problems I have encountered is to access objects in a Flex movie, which is loaded in Flash. Once the movie is loaded accessing it will only lead to the SystemManager of the Flex movie, which does not allow to access any objects. So we need to do an additional manipulation to get to the what is called "mouseCatcher". The mouseCatcher is the 0th child of the SystemManager. The trick to get to there is shown in the script. If you don't see it right, update the Flash player. It also works with Player 10.

The Flash script


import flash.events.Event;
	/*
/ We create a textfield in the Flash movie to follow the events.
*/
var flashText:TextField=new TextField();
flashText.x=100;
flashText.autoSize="left";
flashText.appendText("This is the Flash movie.\n");
addChild(flashText);
	/*
/ We create a Sprite, which we need later.
*/
var mc:Sprite = new Sprite();
addChild (mc);
	/*
/ This is just a regular loading method for the Flex movie.
*/
var url:String = "flexmovie.swf";
var lc:Loader = new Loader ();
lc.contentLoaderInfo.addEventListener(Event.COMPLETE,loadFinished);
function loadFinished (event:Event):void
{
	/*
/ We define the loaded movie as a Sprite. At this point we only get to the Systemsmanager
of the Flex movie. The Flex button is null and not accessible. We now add the Flex movie
to the Sprite. We have to do that or we get errors.
*/
	var loadedName:Sprite = event.currentTarget.content as Sprite;
	flashText.appendText("TARGET1: "+event.currentTarget.content+"\n");
	flashText.appendText("FLEX-BUTTON: "+loadedName.getChildByName("myBut")+"\n");
	mc.addChild (loadedName);
	mc.y=75;
	/*
/ We add an eventlistener, which is triggered as long as the "ADDED" event is fired.
*/
	addEventListener (Event.ADDED, addHandler);
	function addHandler (event:Event):void
	{
		try
		{
	/*
/ When we now ask for the target of the event we get "TARGET: instance3.instance5.mouseCatcher" as a trace. 
We need to use target in order to allow bubbling. We have accessed the mouseCatcher and
are now able to access objects in the Flex movie. 
*/
			flashText.appendText("TARGET: "+event.target+"\n");
			var myBut:Sprite=event.target.getChildByName("myBut") as Sprite;
			var myText:Object=event.target.getChildByName("myText") as Object;
			if(myBut!=null && myText!=null)
			{
	/*
/ To see if we can change objects in the Flex movie we display text in the TextArea and change the button height. 
Then we control the button from the Flash movie and enter new text.
*/
				flashText.appendText ("BUTTON: "+myBut+"\n");
				myText.text="BUTTON: "+myBut+"'s height is now changed.";
				myBut.height=75;
				myBut.addEventListener (MouseEvent.CLICK, clickHandler);
				event.currentTarget.removeEventListener (Event.ADDED, addHandler);
				function clickHandler (event:MouseEvent):void
				{
					myText.text="From Flash to Flex!";
				}
			}
		}
		catch (Error)
		{
			trace (Error);
		}
	}
}
lc.load(new URLRequest(url));
	

The Flex movie script

This is just a simple Flex movie with a button and textfield.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
	<mx:Script>
		<![CDATA[
			
			public function showText():void
			{
				myText.text = "Flex button was accessed!";
			}
		]]>
	</mx:Script>
	<mx:TextArea id="myText" width="200" wordWrap="true"/>
	<mx:Button id="myBut" label="press here" click="showText()"/>
</mx:Application>
	

This works and now allows us to access Flex movies with components, which do not exist in Flash.