从SWFLoader修改正在运行的swf中的变量

时间:2011-06-23 17:56:40

标签: flex swfloader

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

我希望flex应用程序加载包含变量得分的swf,我希望能够修改此变量。

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

这是我的flex应用程序:

<?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代码时收到此类错误消息。

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

所以使用以下代码:

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

}

任何想法?

1 个答案:

答案 0 :(得分: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() {

    }
}

}