Tuesday, July 7, 2009

Object stuff

Just adding some of my daily epiphanies.

The ability of As3 to just assign description strings "I call them", to objects is as handy as a four armed butler. If you need some kind of switch in your program you don't necessarily need to declare a public variable to use as a switch. It is very simple to just add a string to an object. My object is Player. just add dot syntax and:

player.state = "off";

For the newbies you can add anything you want to your object, real time, like this:

player.happynessiocity = "groovy";

Makes for an easy conditional to check a condition, like this:

if(player.state == "off")
{
}


This is a bit of a hack and it throws a swf error but runs. Maybe will work in a pinch but here is a clever solution for a switch in a case where event flow won't allow you to access a listener somewhere. Just change the name of the button.

player.name = "off";

You are effectively negating listeners of parent display object containers elsewhere until you return the name to normal:

player.name = "playbutton";

Concerning functions, it is very handy to be able to send a function an object. You can then mass produce object modifications, like alpha, scale, with dot syntax. Just pass the function the object and then change it:
changeTheSize(myObject);

function changeTheSize(obj:myObject):void
{
obj.scaleX = 1.5;
obj.scaleY = 1.5;
}


You can create factories this way and just send in anything you want to make bigger, or whatever.

No comments:

Post a Comment