jonathanasdf

Serious Dabbler

Dynamic Constants

leave a comment »

What the heck wordpress…. I went to edit the old post and after I clicked “Publish” the result was a blank post. And there are no revisions so you cannot even revert it. So here I am writing the same post again…

Basically I found a way to make constants in other classes to be dynamically loaded from stuff like flashvars and files. This goes along and proves that constants aren’t inlined into the code in AS3 and that constants aren’t necessarily defined on compile time, two conclusions we already knew about.

Anyways, the code:

package {
	import flash.display.Sprite;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.setTimeout;

	public class Main extends Sprite {
		public static const loader:URLLoader = loadConfig();
		private static function loadConfig() : URLLoader {
			var l:URLLoader = new URLLoader();
			l.load(new URLRequest("Config.txt"));
			return l;
		}

		public static var instance:Main;
		public function Main() {
			instance = this;
			var tf:TextField = new TextField();
			addChild(tf);
			setTimeout(function():void {
				tf.text = Test.test;
			}, 1000);
		}
	}
}

package {
	public class Test {
		public static const test:String = trytest();

		private static function trytest() : String {
			if (Main.instance.loaderInfo.parameters.info) {
				return Main.instance.loaderInfo.parameters.info;
			}
			return Main.loader.data;
		}
	}
}

You can download all of the code and binaries here. Run the .swf to load from Config.txt, and run the .html to load from flashvars.

I don’t think there’s any use for this though.

package {
public class Test {
public static const test:String = trytest();private static function trytest() : String {
if (Main.instance.loaderInfo.parameters.info) {
return Main.instance.loaderInfo.parameters.info;
}
return Main.loader.data;
}
}

Advertisements

Written by jonathanasdf

January 21, 2011 at 10:02 AM

Posted in AS3

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s