Tuesday, July 6, 2010

mxmlc: Compiler error - command line: Error: ambiguous argument list

As part of creating a as3 based preloader I needed to use "-frame start some_main_class" option. I hit the below error:

command line: Error: ambiguous argument list; unable to determine where 'frames.frame' parameters end, and default 'file-specs' parameters begin. Use '--' to terminate the parameter list, or perhaps use the '-frames.frame=val[,val]+' syntax instead.

The solution is to use a alternative syntax like the error message points to namely, "-frame=start,some_main_class".

Friday, June 18, 2010

AS3: Textfield alpha

If create a dynamic textfield and use non-embedded fonts, the alpha property will not do anything. The solution is to put the textfield in a container and set the blendMode property of that container to Layer.

Tuesday, March 30, 2010

AS3: Runtime class instantiating

This is very useful if you do custom actions based on XML or any other method. This post explains thing quite nicely:

http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/

Friday, March 5, 2010

AS3: Does my xml doc contain a attribute

This is a simple check which increases the ease of using xml.

if you have a xml file like so:

<root_node some_attribute="">
</root_node>

in AS3 you can decide if a attribute exist by using the lenght() method. So code using the xml would be:

var my_xml:XML = some_function_to_get_xml();

if( my_xml.@some_attribute.length() != 0 ) {
trace("Yup, some_attribute exists");
}

if( my_xml.@some_non_existant_attribute.length() != 0) {
trace("This will not be printed since attribute does not exist");
}

Note it is easy to make a mistake and forget to use length as a method i.e. "length()" and use it as variable i.e. just "length" which will not work.

Wednesday, February 17, 2010

AS3: Preventing a parent from getting a Child's mouse clicks

If you have code that is like this:

var parent_sprite:Sprite = new Sprite();

parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);
addChild(parent_sprite);

function handle_mouse_click(event:MouseEvent):void {
parent_sprite.removeEventListener(MouseEvent.CLICK,
handle_mouse_click);
var child_sprite:Sprite = new Sprite();
draw_button_using_child_sprite(); // Some func to
// draw a button
child_sprite.addEventListener(MouseEvent.CLICK,
handle_button_click);

parent_sprite.addChild(child_sprite);
}

function handle_button_click(event:MouseEvent):void {
do_my_stuff();
parent_sprite.removeChild(child_sprite);
parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);
}

Now one would expect that on clicking on the parent_sprite, the child_sprite would appear. On click on the child_sprite, some stuff would get done and then the child_sprite is removed and the parent_sprite would be the only one on screen.

What I experienced is, that the child_sprite reappeared on the screen. This was because the parent was registering a click as soon as the event for the parent was registered again in handle_button_click() and was doing a addChild(child_sprite). The reason for this is that AS3 has a concept of "bubbling" where a event is passed down to the parent once the child is done with it and it will continue down the chain to the stage.

The solution to this is to use stopPropagation() or stopImmediatePropagation(). stopPropagation() will only prevent parent from receiving the events. While stopImmediatePropagation will prevent everyone listening for the event in the chain from receiving it.

So the code would be:

var parent_sprite:Sprite = new Sprite();
addChild(parent_sprite);
parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);


function handle_mouse_click(event:MouseEvent):void {
parent_sprite.removeEventListener(MouseEvent.CLICK,
handle_mouse_click);

var child_sprite:Sprite = new Sprite();
draw_button_using_child_sprite(); // Some func
// to draw a button

child_sprite.addEventListener(MouseEvent.CLICK,
handle_button_click);


parent_sprite.addChild(child_sprite);
}

function handle_button_click(event:MouseEvent):void {
do_my_stuff();
parent_sprite.removeChild(child_sprite);
parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);

event.stopPropagation(); // Prevent parent from
// receiving event

}

Got the information for this from:
http://www.kirupa.com/forum/showpost.php?p=1948149&postcount=202

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/