0
votes

I have a flex application that's compiled with flex 4.1.

I want that flex application to load an swf that contains the variable score and i want to be able to modify this variable.

I wrote two versions of swf, one compiled with as2 code and the other with as3.

this is my flex application:

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

When i execute the flex application on my browser on 127.0.0.1 ( same domain) i get the following error:

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

i get this kind of error message when trying to run as3 or as2 code.

I checked on google and some people wrote that for flash application there is no need to use a system manager and just to cast the SWFLoader.content as an Object and to try and call the functions from there.

so using the following code:

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

i get the following error when running the as3 version:

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

and the following error when running the as3 version:

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

my as2 swf file contains a text element named score_text and the following code in the first and only frame:

var score:Number = 0;

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

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

my as3 flash file contains a main class called Main and the same text element called score_text.

the Main class:

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

}

any ideas?

1
I thought you said the as3 code had a function setScore(). Why are you doing content['score']=500. (In as3 setScore isn't interpreted as a setter. You want to use set score(value:Number):void in the as3 code or ufkLoader.content.setScore(500); in the flex app.Jacob Eggers
oops I modified the question. I want to be able to modify variables not to execute functions. i'll also add my flash sources.ufk
Did you try the source in my answer?Jacob Eggers
yeah i will post a comment on that.ufk

1 Answers

0
votes

I think this does what you want it to:

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() {

    }
}

}