1
votes

Hai am trying to load the variables from parent(flash) to child(flash).Its working fine,.

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

child swf(flex)

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

But while communicating with flash to flex its not loaded.

Error Message

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

Kindly help me .

Update

parent.swf in flash

 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 in flex

<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

4 Answers

1
votes

In Flex applications top most sprite is SystemManager. And Application object is added as a child of SystemManager. So, you'll need to get it.

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

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

Note, that Application is added on "applicationComplete" event.

Read more about loading applications.

0
votes

You're probably doing one of two things:

  1. You're not waiting until the swf is fully loaded before you try to cast it
  2. You didn't compile the swf's Class into the Flex file (but this should give you a compiler error)

The second problem is actually a good problem to have--Your Flex file doesn't need to know about the implementation of the methods it will call, just the contract (or Interface) of those methods).

Regardless of which problem you have, this example, which shows casting a loaded swf to an Interface and calling methods on it, should help.

0
votes

How about this

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

Load the AS3 swf file using an instance of SWFLoader.

Call the function like this:

mySWFLoader.content.functionName();

How to listen for flash events from flex?

// 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!’ );
}

// In Flash Movie

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

How to call a flex function from flash at a specific frame?

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
votes

All I can see is that the flex function is not public.

This prevents other classes from seeing it.

Change the definition to

public function passVariable(_msg:String):void

EDIT Extending my comment,

A better way would be to loop over all children of swf, and if the passVariable function exists in that, call it.

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