[英]Unable to send variable from flash(parent) to loaded flex swf(child)?
Hai正在嘗試將變量從parent(flash)加載到child(flash)。它工作正常。
父SWF: (Flash)
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);
}
兒童瑞士法郎 (flex)
public var childMessage:String;
function passVariable(_msg:String):void
{
childMessage = _msg;
trace("message passed from parent to child: " + childMessage);
}
但是在與Flash通信以使其彎曲時未加載。
錯誤信息
ReferenceError:錯誤#1069:在_child_mx_managers_SystemManager上找不到屬性passVariable,並且沒有默認值。 在函數/()處
請幫助我。
更新
閃存中的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>
錯誤信息
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()
在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
”事件上添加的。
閱讀有關加載應用程序的更多信息。
您可能正在做以下兩件事之一:
第二個問題實際上是一個好問題-您的Flex文件不需要知道將要調用的方法的實現 ,只需知道這些方法的協定 (或接口)。
無論您遇到什么問題, 此示例 (顯示將已加載的swf強制轉換為Interface並在其上調用方法)都應該有所幫助。
這個怎么樣
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” );
}
如何從特定幀的Flash調用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);
}
我只能看到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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.