Recent projects have highlighted some of the most challenging and interesting issues in Flash development I’ve had to solve. For gaming, one key aspect is speed and memory management. Developers who are used to movieclips and tweening, which normally lend themselves so well to animation, soon discover how unwieldy these can become in a realtime game, like Meowcenaries.
In that game, not only are there over a dozen separate animated game objects moving around on screen at any time, but the speed of their movement is important. Add to this the fact that several of the levels are huge, which means lots of information stored at runtime, and the slowdown becomes apparent very quickly.
For Meowcenaries the solutions were varied, but key to maintaining the speed was ‘flattening’ the nested movieclip structure, and using the BitmapData class to copy flat images onto the screen. The advantage is that all the memory that would usually be reserved to store all those movieclips is released.
We also discovered that tweening, especially transformations, such as colour or transparency caused significant slowdown. This was due to the additional maths required for Flash to calculate the overall required colour for any given pixel where semi-tranparent colours are overlayed. We found the best way to keep the cool animations without the memory overhead was to pre-render them or ‘trick’ the eye. For instance, some animations are not tweened, they consist instead of a staggered set of keyframes representing the different states of the tween.
Also, the game was built in AS3 which introduces the issue of memory leaks – a problem that Flash developers, including me, did not really have to worry about in AS2. It soon became apparent that memory leaks could crop up in the unlikeliest places making debugging a serious issue.
After much hair-pulling a solution was found to speed up debugging. This was to use Flash CS4 to create a movie loading environment. Using the System.totalMemory property we could monitor the memory usage at any given time. A button could be pressed to load and unload the flash movie being tested. When the movie was unloaded System.gc() was called, which is only available in CS4. The advantage of this technique is that after a number of times hammering this button, it would become apparent if something was stuck in memory. Testing different modules of code made it much quicker to pinpoint where the problem was.
In summary, game development brings up many new challenges, some which seem to take you deeper and deeper into the rabbit hole. Luckily, the end result is very rewarding – improving gameplay is a very satisfying job.