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

时间:2015-01-22 15:38:42

标签: actionscript-3 flash flex swfloader

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

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

在这个SWF对象中,用AS3编写,当我点击一个对象时会发生这样的事情:

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

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

public var itemname:String = "test";

我在运行时收到此错误:

  

[Fault] exception,information = ReferenceError:错误#1069:在Main__embed_mxml_Plant_Walking_swf_1661400095上找不到属性项名,并且没有默认值。

为什么呢?谢谢

2 个答案:

答案 0 :(得分:0)

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

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

抱歉,任何拼写错误。

答案 1 :(得分:0)

我终于找到了答案的解决方案。我以这种方式更改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;
        }