K-razy A** Line of Code (4K Spoiler)

If it wasn’t for the spirit refreshing 4K game contest, I never would have thought of the need to write a line of code like this:

tempPart.bitmapData = new BitmapData(2, 2, false,((type ==1)?((explodeCtr % 2 ==0) ? aCR[1]: aCR[2]):aCR[3]) );

The good thing is that comments do not count against the final byte code disk space usage or I would NEVER in a MILLION years remember what this line of code does.  There are 2 ? operators in there! I rarely find the need for ONE much less TWO!  I actually would have used 3 or 4 but I ran out of space.

Here are a few more optimizations I have come across:

1. While() loops save me 1 byte over  the for loop equivalent
2. The above ? operator can save space in places where you might have used and if/then/else for selection between two values. Above I have it embedded 2 of them kind of like an if/then/else inside another if/then/else to pick one of two three colors based on a condition. The colors are in an array because it saves 1 byte for each to access them that way rather than write out the 24bit number.  Over use caused me to add bytes though.
3. Vector drawing is more than the sum of it’s parts. moveto and lineto commands compile down to take up much less space than I thought. They can be over used if you get cocky though.
4. All text is EXPENSIVE! I have limited my words to exactly what is needed. I am actually thinking of changing my game’s name (to a shorter version) to get back a few byes to use for simple instruction text.
5. bitmapData.lock() and unlock(). Getting rid of them save me 10 bytes. Enough for the words “Game Over”. (with a Byte to Spare!!! What will I do with those riches?)
6. Any place where I had static Point() or Rect() objects of the same size or location that didn’t need to change, I combined them into one. I have 4 400×400 rectangles that didn’t move, so I made a rect400 variable and used to all over the place rather than 4 separate versions.
7. TextField and text formats are expensive. I created one TextFormat and set the defaultTextFormat atttribute of each TextField to it. This has to be done BEFORE any text is in the field. Once that is done, there is no need to keep resetting the TextFormat of the field every time you want to change text – for example in a Score field.

I’m sure I’ll come up with some more crazy lines of code like above. If I do, I’ll post them here.

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.