Saturday, January 30, 2010

AS3: Using a String to access Object / Class properties and getDefinitionByName

Consider for example you have a wonderful asset library and have a great scheme of describing maps and such in XML. So you get a asset name from XML and now want to add it to stage.

Remember you can access a Object property like so:

import my_assets.graphics;

var grap:graphics = new graphics();

var my_prop:String = "wonderful_img_class"; /* Got from XML :p */

stage.addChild(new graph[my_prop]); /* Now you asset is displayed */

wonderful_img_class in this case could refer to a image you have included using the [Embed(source=)] tag.

I initially spent time trying to use getDefinitionByName and got hit by the "Error #1065: Variable is not defined" before realising I could just do the above.

From my initial googling getDefinitionByName seems pretty unflexible and hence useless.

However if you want to investigate getDefinitionByName this post seems most useful:

http://www.rozengain.com/blog/2009/08/21/getdefinitionbyname-referenceerror-and-the-frame-metadata-tag/

Tuesday, December 22, 2009

AS3: Embedding XML files

If you have a XML file called myfile.xml. Code to include this would be:

[Embed(source="mfile.xml", mimeType="application/octet-stream")]
[Bindable]
private var my_file:Class;

function some_func():void {
var my_xml:XML = XML(new my_file);
}


my_xml now contains valid XML. You can now access stuff it like normal XML variables.

Got this from the comments in this post: http://dispatchevent.org/roger/embed-almost-anything-in-your-swf/

Tuesday, November 10, 2009

AS3: Adding a Sprite as a child to a Flex Container for example a Canvas

From http://www.sebastiaanholtrop.com/archives/3 use:


import mx.core.*;

var my_canvas:Canvas = new Canvas();
var my_uic:UIComponent = new UIComponent();
var my_sprite:Sprite = new Sprite();

my_canvas.addChild(my_uic);
my_uic.addChild(my_sprite);

Sunday, November 8, 2009

Flex 3: Adding you custom component sources in the compiler

Rather then copying custom or external code libraries to your source, it is better to add it using mxmlc compiler option "--source-path=/your/path/here".

Thursday, November 5, 2009

AS3: Enabling scaling in AS3 for your games or movies

By default AS3 set scaling to NO_SCALE. If you want to override this so that your games or movies scale properly use:

stage.scaleMode = StageScaleMode.SHOW_ALL;
once your stage has been initialized.

Tuesday, October 27, 2009

AS3: Opening a window in the users browser

Use navigateToURL if you want to open a window in the users browser. For example:

 navigateToURL(new URLRequest("http://www.lazysquirrelgames.com"),  
"_blank");


Adobe documentation:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLRequest.html