2012-03-27 16 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

相關問題