Posts Tagged ‘Flash’

AS3 Boolean Value Quirk

Thursday, August 26th, 2010

When casting string values to primitive objects you are accustomed to this syntax:

var tb:int = int("1234");

This returns an integer type with a value of 1,234. However, in Action Scrip 3 Boolean type casting is a little quirky. Using the same syntax will not deliver the result you are expecting.

var tb:Boolean = Boolean("false"); //** returns a boolean type object
                                   //** with a value of true

This is because the Boolean cast function takes any non-null string as a true value. To “work around” this limitation in the language you can you this simple assignment when casting a string value to a Boolean type object.

var ts:String = "false";
var tb:Boolean = ( ts == 'true') ? true : false; //** Returns a boolean type object
                                                 //** with a value of false.

Access Deeply Nested MovieClips Using A String Value in AS3

Thursday, August 26th, 2010

Something that all AS3 programmers run into in their career is having to take a text value from an external file and use it to reference an object on the Flash stage. This is easy if the object is not deeply nested.

var s:String = "mcMyClip";

MovieClip(this)[s].play();

What happens if the string being passed is a path to a deeply nested object? For example: “mcMyClip.mcClipChild.mcClipGrandchild” Unfortunately the Flash compiler does not parse the string used in direct references (square braces) so using the following code will cause the compiler to try and reference an object instance name of “mcMyClip.mcClipChild.mcClipGrandchild”

var s:String = "mcMyClip.mcClipChild.mcClipGrandchild";

MovieClip(this)[s].play();

To resolve this issue we need to create a simple, reusable, function to parse the path and return an object reference. This can be done as follows:

private function getReference(st:String):*
{
	var t:Array = st.split(".");     //** Create an array for the path
	var tg:* = MovieClip(this);   //** Establish the path start point

	for each( var str:String in t)  //** Loop through the path elements
	{
		tg = tg[str];              //** Assign new object reference to return value
	}
	return tg;                         //** Return new object reference to caller
}

This code works well for referencing any type of object no matter how deeply it is nested.

MouseDown and OnRelease Events in AS2

Wednesday, March 17th, 2010

Have you ever had this happen when you are updating or creating a Flash movie using the old AS2?

You create a movie clip with the following code to trap a mouse click on itself.

onMouseDown = function(){
  trace(this._name);
  var sel = int(substring(this._name,3,1));
  trace(sel);
  if (sel > 0){
   _parent.mcWorld["mcE"+sel].play();
   _parent.mcWorld["mcE"+sel]._visible = true;
  } 
}

You place 9 instances on the stage and run the movie, click on one of the movie clip instances and see this result in the trace window.

mc9
9
mc8
8
mc7
7
mc6
6
mc5
5
mc4
4
mc3
3
mc2
2
mc1
1

Every single instance of the movie clip fired the mouseDown logic but you only clicked on one of them.

Read the rest of this entry »