4k Game Contest – and a word about ternary operations in AS3
Edit: Thanks to Kevin Luck for pointing out a flaw with the code in my original post (there was an extra assignment taking place, which caused the byte count to increase). Remove that and ternary will match if/else on a bytes-used basis. I’ll keep this post up here regardless, just ignore everything from this point on 🙂
While working on my 4k Game Competition entry tonight, I found out something that may be useful for others entering:
ternary operations in AS3 will often result in a larger final SWF size.
Here is a set of if/else conditionals taken from the game I’m working on:
if (lx == 0)
this.x = Math.random() * 550;
this.x = lx;
if (ly == 0)
this.y = Math.random() * 400;
this.y = ly;
I replaced the above with this:
x = (lx == 0) ? x = Math.random() * 550 : x = lx;
y = (ly == 0) ? y = Math.random() * 400 : y = ly;
Much more compact, 15 lines less code, does exactly the same thing as the unrolled version above it – yet it uses 20 more bytes in the final SWF. Blindly use ternary operations around your whole 4k game and suddenly you could be wasting a large amount of bytes when you think you’re saving them (because you’re entering less code).
There are a few other little tricks I’ve found while coding this game, and they pretty much all lead back to the same rule: for a smaller SWF size, unroll everything. When 4096 bytes is all you have to play with, this can make all the difference.
Posted on February 18th 2009 at 12:08 am by Rich.
View more posts in ActionScript3. Follow responses via the RSS 2.0 feed.
4 ResponsesLeave a comment
Make yourself heard
All about Photon Storm and our
HTML5 game development services
Filter our Content
- Cool Links
- Flash Game Dev Tips
- Game Development
- Geek Shopping
- In the Media
- Phaser 3