2012-03-27 58 views
1

海正试图从父(闪存)加载变量到子(闪存)。它工作正常。无法从闪存(父级)发送变量到加载的Flex SWF(子)?

父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(柔性)

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

不过,虽然有闪光灯沟通,展示其未加载。

错误消息

ReferenceError: Error #1069: Property passVariable not found on _child_mx_managers_SystemManager and there is no default value. at Function/()

请帮助我。

更新

闪光灯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); 
} 

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

回答

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” 事件添加。

查看更多about loading applications

+0

谢谢我得到这样的错误,1061:调用一个可能未定义的方法passVariable通过引用与静态类型flash.display:DisplayObject。 – Ela 2012-03-27 12:32:17

+0

@ user1268367我已经更新了答案。您可以使用动态属性。 – 2012-03-27 13:18:06

+0

再次出现错误,ReferenceError:错误#1069:在_ViewPPT_mx_managers_SystemManager上找不到属性passVariable,并且没有默认值。 \t在功能/ () – Ela 2012-03-27 13:29:57

0

你可能会做两件事情之一:

  1. 你不是等到SWF满载尝试投它
  2. 你没有编译SWF的Class进入前Flex的文件(但是这应该给你一个编译器错误)

第二个问题实际上是有一个很好的问题 - 你的Flex文件并不需要了解实施的,它会调用方法 ,只是合同(或接口)的那些方法)。

无论您遇到哪种问题,this example(显示将加载的swf转换为接口并调用接口上的方法)都会有所帮助。

+0

Kindy源代码 – Ela 2012-03-27 12:57:11

+0

在蓝色的文本解释(“本示例”)是所谓的“超链接, “你可能没有意识到的是实际上可点击的文本,将带你到另一个地方。在这种情况下,它会将您带到博客文章,您可以在其中查看启用了View Source的示例。 – 2012-03-27 18:25:19

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”); 
} 

如何在特定的框架中调用闪存柔性功能?

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); 
} 
+0

我需要通过变量从闪存(父)flex(孩子)(即我加载闪存swf) – Ela 2012-03-27 13:24:39

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); 
     } 
    } 
} 
+0

也尝试公共职能,TypeError:错误#1009:无法访问空对象引用的属性或方法。 \t at Untitled_fla :: MainTimeline/frame1() TypeError:错误#1009:无法访问空对象引用的属性或方法。 \t在mx.managers ::的FocusManager /激活() mx.managers ::的SystemManager/HTTP://www.adobe.com/2006/flex/mx/internal :: preloader_completeHandler() \t在flash.events :: EventDispatcher/dispatchEventFunction() \t \t at mx。预加载器:: Preloader/timerHandler() 从父项传递给子项的消息:null – Ela 2012-03-27 13:52:43

+0

为什么不调试代码?是'swf.getChildAt(1);'返回预期的Flex对象?更好的方法是(看我编辑的答案) – 2012-03-28 07:06:20

相关问题