Flex与flash交互程序
Flex端程序
源代码TestFlex_Flash.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">
<mx:Script>
<![CDATA[
import flash.profiler.showRedrawRegions;
import mx.controls.Alert;
import flash.utils.Timer;
import flash.events.TimerEvent;
public function testf():void{
Alert.show("已经调用");
trace("ljlo");
var time:Timer=new Timer(1000,1);
time.start();
time.addEventListener(TimerEvent.TIMER_COMPLETE,invokeFlash);
}
public function loadSwf():void{
//传入此flex对象
Object(swfgame.content).setApp(this);
// Object(swfgame.content).toFlex();
}
public function invokeFlash(inovevent:TimerEvent):void{
//调用flash组件实例类对象的方法(myC为在flash帧中定义)
Object(swfgame.content).myC.innerFunction();
}
public function parentMethod():void{
Alert.show("调用父方法......");;
}
]]>
</mx:Script>
<mx:SWFLoader id="swfgame" source="test.swf" x="174" y="75" width="330" height="200" creationComplete="loadSwf()"/>
<local:TestComponent id="component">
</local:TestComponent>
</mx:Application>
源代码TestComponent.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" xmlns:local="*">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function testf():void{
Alert.show("class组件已经调用了....");
trace("ljlo");
}
]]>
</mx:Script>
<!-- <mx:SWFLoader id="swfgame" source="test.swf" x="174" y="75" width="61" creationComplete="loadSwf()"/>-->
<local:VisualView id="view" />
</mx:Canvas>
源代码VisualView.as
package
{
import mx.core.IMXMLObject;
import mx.controls.Alert;
public class VisualView implements IMXMLObject
{
protected var view : Object;
protected var id : String;
public function initialized( document : Object, id : String ) : void
{
this.view = document;
this.id = id;
}
public function VisualView()
{
}
public function testView():void{
Alert.show("调用无视图方法。");
}
Public function invokeFlashMethod():void{
Object(View.parentApplication.swfgame.content).
myC.innerFunction();
}
}
}
Flash端程序
Flash帧里代码如下
import flash.utils.Timer;
import flash.events.TimerEvent;
var flexApp:Object;
function setApp(ap:Object):void {
this.flexApp=ap;
// myC.passFlex(flexApp);
var time:Timer=new Timer(1000,1);
time.start();
time.addEventListener(TimerEvent.TIMER_COMPLETE,toFlex);
}
var myC:MyClass=new MyClass();
function toFlex(eve:TimerEvent){
flexApp.component.testf(); //flash调用flex子组件方法
flexApp.component.view.testView(); //flash调用flex非可视组件组件中方法
flexApp.testf(); //flash直接调用flex加载该游戏作用域内方法
flexApp.component.parentApplication.parentMethod(); //flash调用flex父组件方法
myC.passFlex(flexApp);
}
MyClass类源代码
package {
public class MyClass {
public var myObj:Object=null;
public function MyClass() {
}
public function passFlex(obj:Object) {
//obj.testf();
myObj=obj;
}
public function innerFunction(){
//myObj.component.testf();
trace("i am from the flash inner class")
}
}
}