niedziela, 10 kwietnia 2011

Register class between swf files

Well I got a problem with registerClassAlias and that was all because I didn't read the manual, the whole thing is working now. Thanks to the awesome project flashlcs - Unlimited 2 way local runtime data messaging for the Flash Platform. that I visited one more time this week. And they are mentioning to read the manual page on their main page.
So from the AS3 manual page correct version of registerClassAlias is:

package {
import flash.display.Sprite;
import flash.net.registerClassAlias;
import flash.utils.ByteArray;

public class RegisterClassAliasExample extends Sprite {
public function RegisterClassAliasExample() {
registerClassAlias("com.example.eg", ExampleClass);
var eg1:ExampleClass = new ExampleClass();
var ba:ByteArray = new ByteArray();
ba.writeObject(eg1);
ba.position = 0;
var eg2:* = ba.readObject();
trace(eg2 is ExampleClass); // true
}
}
}

class ExampleClass {}

But you need to remember when you define the ExampleClass with constructor to not leave the constructor arguments not set otherwise you will get the null pointer exception when reading the object back ex.

// correct
class ExampleClass
{
function ExampleClass(someVar:Object = null, someVar2:String=null)
{
}
}

// incorrect
// will throw Exception when ba.readObject();
class ExampleClass
{
function ExampleClass(someVar, someVar2)
{
}
}

so now I can write and read my objects between my AIR socket server and AS project. I also have some kind of protocol to send large portions of data working.

Also When You are using debugger version of the flashplayer you can force to run GC yourself just by calling
System.gc()
and to invoke it manually Grant Skinner on his blog gave you a tip some time ago. So when I detect that user have no debugger version of flashplayer I simply invoke LocalConnection trick. The example method is:

public function runGC():void
{
if(Capabilities.isDebugger)
{
System.gc();
System.gc();
} else
{
try
{
new LocalConnection().connect("so");
new LocalConnection().connect("so");
} catch(e:Error)
{
log.error(e.getStackTrace());
}
}
}

Well that's it for now. Next I will try to have some fun with the
flash.sampler.Sample