无法将变量从flash(父)发送到加载的flex swf(子)?

时间:2012-03-27 10:49:53

标签: flash flex communication

Hai我试图将变量从父(flash)加载到子(flash)。它工作正常,。

父母swf:(闪存)

var parentMessage:String = "Hello";
var swf:MovieClip;
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
l.load(new URLRequest("child.swf"));
function swfLoaded(e:Event):void
{
swf = MovieClip(e.target.content);
swf.passVariable(parentMessage);
}

儿童swf (flex)

public var childMessage:String;
function passVariable(_msg:String):void
{
childMessage = _msg;
trace("message passed from parent to child: " + childMessage);
}

但是在与flash进行通信以使其未加载时。

错误消息

  

ReferenceError:错误#1069:找不到属性passVariable   _child_mx_managers_SystemManager并没有默认值。在Function /()

请帮帮我。

更新

flash中的

parent.swf

 var parentMessage:String = "Hello";
 var swf:MovieClip;
 var l:Loader = new Loader();
 l.load(new URLRequest("asd.swf"));
 swf.addEventListener("applicationComplete", swfLoaded);
 function swfLoaded(e:Event):void
 {
 var app:DisplayObject = swf.getChildAt(1);
 app["passVariable"](parentMessage);
 }

flex中的Child.swf

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"  addedToStage="application1_addedHandler(event)" >
<fx:Script>
    <![CDATA[
        public var childMessage:String;

        protected function application1_addedHandler(event:Event):void
        {

            passVariable(childMessage);
        }
        public function passVariable(_msg:String):void
        {

            childMessage = _msg;
            trace("First message passed from parent to child: " + childMessage);
}

    ]]>
</fx:Script>

的ErrorMessage

   TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_fla::MainTimeline/frame1()
   TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.managers::FocusManager/activate()
at spark.components::Application/initManagers()
at spark.components::Application/initialize()
at asd/initialize()
at mx.managers.systemClasses::ChildManager/childAdded()
at mx.managers.systemClasses::ChildManager/initializeTopLevelWindow()
at mx.managers::SystemManager/initializeTopLevelWindow()
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::kickOff()
at       mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::preloader_completeHandler()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.preloaders::Preloader/timerHandler()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

4 个答案:

答案 0 :(得分:1)

在Flex应用程序中,最顶级的精灵是SystemManager。并且Application对象被添加为SystemManager的子级。所以,你需要得到它。

swf = MovieClip(e.target.content);
swf.addEventListener("applicationComplete", onApplicationReady);

private function onApplicationReady(evt:Event)
{
    var app:DisplayObject = swf.getChildAt(0);
    app["passVariable"](...);
}

请注意,“Application”事件会添加applicationComplete

详细了解loading applications

答案 1 :(得分:0)

您可能正在做以下两件事之一:

  1. 在尝试施放swf之前,你还没有等到swf满载
  2. 您没有将swf的类编译到Flex文件中(但这会给您带来编译错误)
  3. 第二个问题实际上是一个很好的问题 - 你的Flex文件不需要知道它将调用的方法的实现,只是合同< / em>(或接口)这些方法)。

    无论你遇到了什么问题,this example都显示将加载的swf转换为接口并在其上调用方法,这应该会有所帮助。

答案 2 :(得分:0)

这个怎么样

http://www.redcodelabs.com/2009/06/call-function-from-flex-to-flash/

使用SWFLoader实例加载AS3 swf文件。

像这样调用函数:

mySWFLoader.content.functionName();

如何从flex中收听flash事件?

// In your Flex app
/* called when your SWFLoader finishes loading the SWF */
private function onMySWFLoaded( p_event:Event ) :void
{
      mySWFLoader.content.addEventListener( “clicked”, onSWFClick );
}
/* callback for the clicked event */
private function onSWFClick( event:Event ) :void
{
      mx.controls.Alert.show( ‘Click event raised!’ );
}

//在Flash电影中

/* called when the button in your flash movie is clicked */
private function onButtonClick( p_event:Event ) :void
{
      dispatchEvent( new Event( “clicked” );
}

如何在特定帧处从闪光灯调用flex功能?

Embed(source=“../assets/swf/myFlashComponent.swf”, symbol=“Preloader”)]
private var FlashPreloaderSymbol:Class;
private var clip:MovieClip;
clip = new FlashPreloaderSymbol();
addChild(clip);
private function onFlexInitComplete( event:FlexEvent ):void
{
clip.addFrameScript(47, callMyFunction);
}

答案 3 :(得分:0)

我只能看到flex功能不公开。

这可以防止其他类看到它。

将定义更改为

public function passVariable(_msg:String):void

修改 扩展我的评论,

更好的方法是遍历swf的所有子节点,如果存在passVariable函数,则调用它。

function swfLoaded(e:Event):void {
    for each(var app:DisplayObject in swf.getChildren()) {
        if(app.passVariable != null) {
            app["passVariable"](parentMessage);
        }
    }
}