New great trick like non swc into AS3 How does it work?


#1

Hello everyone, I know I have readden who want compile into swc. Like @singmajesty said.
But it doesn’t work now.

How do I find if I call from haxe-made SWF like via ApplicationDomain and it can call access function from haxe-made SWF. It is very better than import swc because I have tried yesterday and it doesn’t work because output shows “Your swc is unknown format” or “Your swc doesn’t have libraries”. I have idea - loader and applicationdomain example:

mylib.Example.hx just openfl project from FlashDevelop

package mylib;
/**
 * ...
 * @author Jens Eckervogt
 */
 import openfl.Lib;
 import openfl.display.Sprite;
 import openfl.geom.Point;
 import openfl.text.TextField;
 import openfl.text.TextFormat;
 
class Example extends Sprite
{
	public function new(message:String, pos:Point, size:Point) 
	{
		super();
		var tf:TextField = new TextField();
		tf.width = size.x;
		tf.height = size.y;
		tf.x = pos.x;
		tf.y = pos.y;
		tf.text = message;
		var format:TextFormat = new TextFormat();
		format.size = 20;
		format.color = 0x00000000;
		format.bold = true;
		tf.defaultTextFormat = format;
		Lib.current.addChild(tf);
	}	
}

Build as flash and create AS3 project:

package
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	public class TestEmbedSwfFromHaxe extends Sprite
	{	
		[Embed(source="MyLib.swf", mimeType="application/octet-stream")]
		private var _swf:Class;
		
		public function TestEmbedSwfFromHaxe()
		{
			super();
		
			var _loader:Loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			var _lContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			_lContext.allowCodeImport = true;
			_lContext.allowLoadBytesCodeExecution = true;
			_loader.loadBytes(ByteArray(new _swf()), _lContext);
			addChild(_loader);
		}
		
		protected function onLoadComplete(e:Event):void
		{
			switch(e.type)
			{
				case Event.COMPLETE:
					/**
					 * 	Try call constructure function of ( public function new() { ... } from Example.hx )
					 */
					var example:Class = e.target.applicationDomain.getDefinition("mylib.Example") as Class;
					var appExample:Sprite = new example("swf made from Haxe!", new Point(20, 20), new Point(300, 30));
					addChild(appExample);
					/**
					 * 	Yeah it works and try with unsupported method from Haxe into AS3.
					 */
					var openfl:Class = e.target.applicationDomain.getDefinition("flash.Lib") as Class;
					var appEx1:Sprite = new example("Unsupported openfl.Lib into AS3 Yay!", new Point(20, 60), new Point(500, 30));
					/**
					 * 	openfl = ( openfl.Lib )
					 * 		current = ( Lib.current ) static
					 * 			addChild() = ( Lib.current.AddChild() from MovieClip )
					 */
					openfl.current.addChild(appEx1);
					/**
					 * 	Woah it works fine. I thought we can use more frameworks into AS3 like hack to faked compiler.
					 * 	It is communication of normal as3 and haxe-made SWF.
					 */
					break;
			}
		}
	}
}

Make sure and you copy MyLib.swf into as3 project/src and embed into swf
Yay it works fine. That is great method better than swc. You can add tutorial to your website.
If you know for static and normal method.
Example for static methods:

var myFunc:Class = event.target.applicationdomain.getDefinition(“package.MyClass”) as Class;
// public static function myMethod(x:Int, y:Int):Void { … }
myFunc.myMethod(10, 10) ;

If you want normal methods from embedded haxe-made SWF:

var myFunc:Class = event.target.applicationdomain.getDefinition(“package.MyClass”) as Class;
// public function myMethod(x:Int, y:Int):Void { … }
var myMethod:* = new myFunc();
myMethod.myMethod(10, 10);

I hope you understand my great way with embedded or external haxe-made swfs.

// EDIT Another method via LoaderInfo can work too - yay like realistic variables and functions from haxe-made swfs too

Code: mylib.Example sees here ( I already write code. )

public function TestEmbedWithHaxeAndLoaderInfo()
{
	super();
	var _loader:Loader = new Loader();
	_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
	var _lContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
	_lContext.allowCodeImport = true;
	_lContext.allowLoadBytesCodeExecution = true;
	_loader.loadBytes(ByteArray(new _swf()), _lContext); // Embedded like I write code of ApplicationDomain
}
protected function onLoadComplete(e:Event):void
{
	switch(e.type)
	{
		case Event.COMPLETE:
				var loaderInfo:LoaderInfo = e.target as LoaderInfo;
				addChild(e.target.content);
				var mySWF:Object = loaderInfo.content;				
			break;
	}
}

If you want access functions and variables?
> var mySWF:Object = loaderInfo.content;
> mySwf.myVariableArray = [1, 2, 3, 4]; // public var myVariableArray:Array;
> mySwf.myMethod(); // public function myMethod():void { … }

PS: I tried static variables but it doesn’t work. :confused:

And another example from haxe-made SWF - loader can read it with LoaderInfo and ApplicatioNDomain. Away3D haxe-version looks very better than Away3D AS3 version. Because shadows, reflections, dynamic lights and more made from Away3D haxe.


No problem with:

control from loader YES
rendermode with gpu YES
rendermode with direct NO WORKING
rendermode with auto No tested.
resize function YES
Call and Callback YES
More features YES

Thanks and I hope you have not problem with LoaderInfo and ApplicationDomain.
And enjoy your happy coding!