在嵌入式 SWF 和 Flex 之间传递数据

Pass data between embedded SWF and Flex

我有一个 Flex 项目,其中嵌入了 SWFLoader 的 SWF 对象:

<mx:SWFLoader id="mySWFLoader" source="@Embed('Plant_Walking.swf')" creationComplete="onComplete()" />

onComplete()函数嵌入在mxml文件中:

<fx:Script>
    <![CDATA[

        public function onComplete() : void {
            mySWFLoader.content.addEventListener( "featureSelected" , handleEvent );
        }

        public function handleEvent( evt:Event ):void
        {

            var selectedInfo : String = mySWFLoader.content['itemname'];

            textinput.text = selectedInfo;
            label.text = selectedInfo;
        }


    ]]>
</fx:Script>

在这个用 AS3 编写的 SWF 对象中,当我单击一个对象时会发生以下情况:

itemname=ev.object.name;
dispatchEvent(new Event("featureSelected", true));

它调用事件 featureSelected 然后它会调用 handleEvent 函数。问题是我没有收到 itemname 参数的值,即使在 SWF 对象中声明了该参数:

public var itemname:String = "test";

我在运行时收到此错误:

[Fault] exception, information=ReferenceError: Error #1069: Property itemname not found on Main__embed_mxml_Plant_Walking_swf_1661400095 and there is no default value.

为什么?谢谢

在您的代码中写下以下内容。它会给出值。

var selectedInfo : String = evt.itemname;

而不是:

var selectedInfo : String = mySWFLoader.content['itemname'];

编辑:

如果上述方法不起作用,请创建自定义事件,例如:

public class FeatureSelectedEvent extends Event
{
    public static var FEATURE_SELECTED:String = "featureSelected";

    private var _itemname:String;
    public function get itemname():String { return _itemname }

    public function FeatureSelectedEvent (type:String, itemname:String, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type,bubbles,cancelable);
        _itemname = itemname;
    }

    override public function clone():Event
    {
        return new FeatureSelectedEvent(type, itemname, bubbles, cancelable);
    }
}

然后像这样发送事件:

dispatchEvent(new FeatureSelectedEvent(FeatureSelectedEvent.FEATURE_SELECTED, ev.object.name));

您得到的参数值如下:

public function handleEvent( evt:Event ):void
 {
    var selectedInfo : String = event.itemname;
 }

抱歉,如有任何拼写错误。

我终于找到了答案。我这样修改 handleEvent 函数的代码:

public function handleEvent( evt:Event ):void
        {   
            var selectedInfo:String = ((mySWFLoader.content as MovieClip).getChildAt(0) as Loader).content["itemname"]
            textinput.text = selectedInfo;
            label.text = selectedInfo;
        }