This engine was my senior thesis in school, and a hobby before that. It started out with me wondering if I could reimplement RPG Maker XP in Java, using open-source libraries. Turns out I can, and I did enough of that to satisfy me. Java was too slow (or, more appropriately, I didn’t know enough to write it efficiently in Java), so I ported it to C++. The C++ port done, and a variety of scripting languages embedded and removed, I got bored and starting reimplementing it in a variety of languages as a learning tool. I did Python, Ruby, and later C#/XNA.
When I got my first Flash job offer, I decided I needed a portfolio piece, and this engine seemed a good enough choice.
I see a lot of constructs in code that are just wordier than they need to be. All of these examples are in ActionScript 3, though most of them apply in other languages as well.
Many of these are also marginally faster than their wordier counterpart.
Testing booleans against true or false
var b:Boolean = isSomethingTrue();
if (b == true)
doSomething();
else if (b == false)
doSomethingElse();Booleans are, by definition, either true or false, so you don’t need to check if they equal true or false in a conditional.
if (isSomethingTrue())
doSomething();
else
doSomethingElse();Assigning boolean to literal based on conditional
var b:Boolean;
if (isSomethingTrue() && isSomethingElseTrue())
b = true;
else
b = false;Same as above – the result of the expression is itself either true or false, so just use that result.
var b:Boolean = isSomethingTrue() && isSomethingElseTrue();Explicit null testing
var o:Object = findObjectOrReturnNull();
if (o != null)
useObject(o);null implicitly casts to false when necessary, so you don’t need to check for null-ness – just toss the object itself in the conditional.
var o:Object = findObjectOrReturnNull();
if (o)
useObject(o);Using else if when only two states exist
var x:int = getRandomInt();
var y:int = getRandomInt();
if (x >= y)
trace("X is bigger");
else if (y > x)
trace ("Y is bigger");If x is not greater than or equal to y, there’s only one option – y is greater than x. You don’t need to test for this in an else-if – just use else.
var x:int = getRandomInt();
var y:int = getRandomInt();
if (x >= y)
trace("X is bigger");
else
trace("Y is bigger");Personally, I’d probably go one step further and use the ternary conditional operator in this case:
trace((x >= y ? "X" : "Y") + " is bigger");I did a stupid thing. That said, I was using an idiot-proof web interface and it let me do it, so I figured it was cool.
I wanted to password protect subdirectories within my Subversion repository. Accepting from the beginning that this isn’t possible in the literal sense, I went for the next best thing – making repositories mapped to what appear to be subdirectories of the base repository. I could then set these ‘subrepositories” to private, and give the illusion of a seamless repository with private subdirectories.
This worked for a while, and then suddenly (a month later when I checked it again) all repositories in the system were totally wiped.
This is a Dreamhost Happy Subversion Robot issue, not an issue with SVN in general. Tech support says “you shouldn’t have done that.” I’ve lost a year’s worth of accumulated Flash code.
Don’t do what I did.
I still think Dreamhost is awesome, despite this loss.
I recently signed a purchase agreement on a new townhome – I should close on October 7th and move in as quickly as possible afterward. I snapped some pictures of the model home, which is exactly the same floorplan and basically the same options as my in-progress place, and I also took some pictures of my actual unfinished joint for good measure. Have a look, if you’re curious.
This post is inspired by Nick’s aversion to bitwise operators all over the place. Despite this glaring personality flaw, he’s still worth hiring.
Many developers, and unfortunately almost every Flash developer, are far too comfortable with their abstract data types like double and int that they forget that, under the hood, everything’s stored as binary numbers. Having just a little knowledge about how things work under the hood makes you write better, faster, cleaner code.
This is about 8 hours of work, porting Jamis Buck’s dungeon generator from C to AS3.
There’s no goal, as this is only an experiment. Apologies to Gauntlet, from which I ripped some quick graphics. The minimap doesn’t show your position, but rest assured it is accurate, and after running around for a few seconds you should be able to extrapolate where you are.
This is a game I wrote in about 24 hours total. Mitch from Puny Entertainment did the artwork – those guys are awesome and Mitch doubly so!
Get points by making rows or columns of 3 or more clones! The clones are defective and, when matched with their unstable brethren, obliterate eachother and disappear. Get more points by matching bigger groups – a group of 10 with one swap gives ridiculous points, especially on higher levels.
This post is in response to Nate, who asked me this question a few days ago.
If you load a file using AS3, there are a couple of potential outcomes –
- The file exists, you have permissions to access it, and it completes successfully! Hurray! The
URLLoaderdispatches anEvent.COMPLETE. - The file exists, but you don’t have permissions – the
URLLoaderdispatches aSecurityErrorEvent.SECURITY_ERROR. - The file doesn’t exist, or you otherwise can’t access it – the
URLLoaderdispatches anIOErrorEvent.IO_ERROR.
Flash is an incredibly event driven framework. Everything seems to dispatch an event or five. Most of these events you simply ignore, because nobody really needs this level of information. This leads to a tricky situation, however, when the default event behavior (just ignore it) doesn’t work.
Specifically, ErrorEvents and their subclasses, if not listened for by something, will pop up an error dialog to the user. So, in the case when you’re loading a file, but you don’t really care if it exists or not, you actually have to listen for IO and security error events, and explicitly ignore them, for the usually-default event behavior to work.
More generally, there are two sets of event behaviors an AS3 developer needs to know:
ErrorEvents (and subclasses) must be listened for if they’re to be ignored, otherwise the user gets a error box.Events (and non-ErrorEventsubclasses) can be ignored without repercussion.
This is a website about game programming in general. As I currently work in ActionScript 3 professionally, many of my examples will be in Flash. I will occasionally post things of interest that I come across or invent.