0
votes

I have 2 projects created with Flash Builder 4.6:

  1. an ActionScript project
  2. a Mobile Flex project

both of these project load the same external SWF file (created with flash professional CS6) that contains movie clip assets (using the same shared code to load external swf files)

both project extend the movie clips in the swf asset file. for example, in the swf file I have a movie clip called "Test" that contains a single text field. the linkage class is "MyTest" and the text field has an instance name of "txt1".

I have a class called "MyTest" in both project which contains a public "txt1" property and a private "var counter:int", and once the swf is loaded I use the following code to create an instance of the class:

var x:MyTest = new MyTest();

in the actionscript project it works great and x is created correctly, the textfield txt1 is set correctly.

in the flex project however, the txt1 property is null, so the instance is not an instance of the "Test" movie clip from the external swf.

what am I doing wrong? why is it working in the actionscript project and not in the mobile flex project? this is a shared piece of code doing all this work, and I'd hate to rewrite a new one just for the flex project...

thanks.


to make things clearer

this piece of code:

var cls:Class = loader.contentLoaderInfo.applicationDomain.getDefinition("MyTest") as Class;
var x:Object = new cls();
trace(getQualifiedClassName(x));
var y:MyTest = x as MyTest;

will set x to be of type MyTest as defined in the swf (does not have a "counter" property", and y will be null (safe case did not succeed).

1
Is the ActionScript project a mobile project? Are you testing on devices or in emulators? I believe iOS will not load SWFs at runtime because doing so violates portions of the iOS Developer agreement regarding processing code at runtime; but that behavior will only exists on a device.JeffryHouser
the ActionScript project is not a mobile project, it's used to run our application on the web (it's a facebook game which we now want to port to mobile). I'm using emulators, not testing on a device yet.Ami
it's important to note that the mobile project loads the swf and I can create an instance of the class "MyTest". the problem is that the instance I create from the swf is not the same as one defined in my flex project. i.e., var x:MovieClip = loader.contentLoaderInfo.applicationDomain.getDefinition("MyTest") as Class; works fine, and then var x2:MyTest = x as MyTest sets x1 to null.Ami

1 Answers

0
votes

When you say

var y:MyTest = x as MyTest

Then obviously something is wrong here. Your MyTest that is defined in the swc via linkage id has "stuff" (such as the physical graphics) that the MyTest that's compiled into your Flex project (via the import statement that allows the variable y to be declared as type MyTest) does not know about. So you have two colliding Class definitions, especially if they both have the same namespace.

I would not expect that y would be able to hold an instance of the MyTest created from the library linkage and I would expect that attempting to cast from one to the other would yeild null--exactly as you are reporting.

You would be better off creating an ITest Interface that declares a getter/setter pair for txt1 (which by the way I hope you're not using as an actual production variable name). The cast should then work just fine.