0

我有一个用 flex 4.1 编译的 flex 应用程序。

我希望那个 flex 应用程序加载一个包含变量 score 的 swf,并且我希望能够修改这个变量。

我写了两个版本的 swf,一个用 as2 代码编译,另一个用 as3 编译。

这是我的弹性应用程序:

<?xml version="1.0" encoding="utf-8"?>
<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">

<fx:Script>
<![CDATA[

        import mx.events.FlexEvent;
        import mx.managers.SystemManager;

private var loadedSM:SystemManager;

private function initNestedAppProps():void
{
loadedSM = SystemManager(ufkLoader.content);
loadedSM.addEventListener(FlexEvent.APPLICATION_COMPLETE,updateNestedLabels);
}

private function updateNestedLabels(e:Event):void {
loadedSM.application["score"]=500;
}

]]>
</fx:Script>
<mx:SWFLoader  loadForCompatibility="true" width="500" height="500" creationComplete="initNestedAppProps()" id="ufkLoader" source="http://127.0.0.1/path/to/swf.swf" />
</s:Application>

当我在 127.0.0.1(同域)上的浏览器上执行 flex 应用程序时,出现以下错误:

An ActionScript error has occurred:
TypeError: Error #1034: Type Coercion failed: cannot convert Main__Preloader__@f36e6191 to mx.managers.SystemManager.
at Main/initNestedAppProps()
at Main/__ufkLoader_creationComplete()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.core::UIComponent/set initialized()
at mx.managers::LayoutManager/doPhasedInstantiation()
at mx.managers::LayoutManager/doPhasedInstantiationCallback()

我在尝试运行 as3 或 as2 代码时收到这种错误消息。

我在谷歌上查了一下,有些人写道,对于 Flash 应用程序,不需要使用系统管理器,只需将 SWFLoader.content 转换为对象并尝试从那里调用函数。

所以使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<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">

<fx:Script>
<![CDATA[
private function updateNestedLabels(e:Event):void {
trace("HERE");
ufkLoader.content['score']=500;
}
]]>
</fx:Script>
<mx:SWFLoader  loadForCompatibility="true" width="500" height="500" complete="updateNestedLabels(event)" id="ufkLoader" source="http://127.0.0.1/~ufk/work-projects/xpofb/flash/toolkit-test/scores/as3/score.swf" />
  </s:Application>

运行 as3 版本时出现以下错误:

An ActionScript error has occurred:
ReferenceError: Error #1056: Cannot create property score on Main__Preloader__.
at Main/updateNestedLabels()
at Main/__ufkLoader_complete()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.controls::SWFLoader/http://www.adobe.com/2006/flex  /mx/internal::contentLoaderInfo_completeEventHandler()

以及运行 as3 版本时出现以下错误:

An ActionScript error has occurred:
ReferenceError: Error #1056: Cannot create property score on flash.display.AVM1Movie.
at Main/updateNestedLabels()
at Main/__ufkLoader_complete()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.controls::SWFLoader/http://www.adobe.com/2006/flex/mx/internal::contentLoaderInfo_completeEventHandler()

我的 as2 swf 文件包含一个名为 score_text 的文本元素以及第一个也是唯一一个帧中的以下代码:

var score:Number = 0;

function setScore() {
  score++;
  score_text.text=score.toString();
}

function addToScore(num:Number) { 
  score+=num;
  setScore();
}
setInterval(setScore,1000);

我的 as3 flash 文件包含一个名为 Main 的主类和名为 score_text 的相同文本元素。

主类:

package  {

import flash.display.MovieClip;
import flash.utils.setInterval;

public class Main extends MovieClip {
public var score:Number = 0;

public function setScore() {
score++;
score_text.text=score.toString();
}

public function addToScore(num:Number) {  
score+=num; 
setScore();
}

    public function Main() {
    setInterval(setScore,1000);     
    }
}

}

有任何想法吗?

4

1 回答 1

0

我认为这可以满足您的要求:

package  {

import flash.display.MovieClip;
import flash.utils.setInterval;

public class Main extends MovieClip {
    private var _score:Number = 0;

    public function set score(value):void {
       _score = value;
       score_text.text=score.toString();
    }

    public function addToScore(num:Number):void {  
        score+=num; 
    }


    public function Main() {

    }
}

} 
于 2011-06-23T18:19:50.027 回答