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 🙂
Disinformation follows:
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:
[code]
if (lx == 0)
{
this.x = Math.random() * 550;
}
else
{
this.x = lx;
}
if (ly == 0)
{
this.y = Math.random() * 400;
}
else
{
this.y = ly;
}
[/code]
I replaced the above with this:
[code]
x = (lx == 0) ? x = Math.random() * 550 : x = lx;
y = (ly == 0) ? y = Math.random() * 400 : y = ly;
[/code]
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.
Make yourself heard
Hire Us
All about Photon Storm and our
HTML5 game development services
Recent Posts
OurGames
Filter our Content
- ActionScript3
- Art
- Cool Links
- Demoscene
- Flash Game Dev Tips
- Game Development
- Gaming
- Geek Shopping
- HTML5
- In the Media
- Phaser
- Phaser 3
- Projects
Brain Food