Flex与Flash的交互_2(深层对象交互示例)

本文介绍了一个Flex应用程序如何与Flash内容进行交互的例子。包括Flex端主程序文件、组件文件及非可视组件的源代码,以及Flash端程序代码。展示了Flex与Flash之间相互调用方法的具体实现。
[b][color=orange]Flex端程序[/color][/b]


1、主程序文件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()"/>

<!--TestComponent标签表示将引入*(同一)目录下的TestComponent.mxml组件-->
<local:TestComponent id="component">



</local:TestComponent>

</mx:Application>



2、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>
<!--下面注释掉的代码就是加载Flash制作的swf的,加载完成后,Flex就可以用swfgame这个对象的content属性表示调用该Flash程序的代码了-->
<!-- <mx:SWFLoader id="swfgame" source="test.swf" x="174" y="75" width="61" creationComplete="loadSwf()"/>-->

<local:VisualView id="view" />

</mx:Canvas>



3、非可视组件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();

}
}
}


[b][color=orange]Flash端程序[/color][/b]

1、帧里(或者文档类)代码如下


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();


//下面这个方法测试调用Flex程序代码的效果,通过下面的四个调用也可以看出Flex代码中各组件的从属关系
function toFlex(eve:TimerEvent){

flexApp.testf(); //flash直接调用flex加载该游戏作用域内方法
flexApp.component.testf(); //flash调用flex子组件方法

flexApp.component.view.testView(); //flash调用flex非可视组件组件中方法


flexApp.component.parentApplication.parentMethod(); //flash调用flex父组件方法

myC.passFlex(flexApp);

}



2、Flash自定义类MyClass类源代码

作用是为表明Flex可以调用Flash中对外公开的类的公开方法(只要是公开的类,就可以一级一级调用下去)


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")

}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值