Today’s guest tutorial is the second from Omid Ahourai, an indie game developer who goes by the alias “ArdentKid.” Over the past two years, Omid has risen to become an experienced Corona developer, and is working to release his first highly-anticipated Corona game, “Balloon Bazooka.” He also offers Corona tutoring for programmers of all levels. Check out his work and blog at www.ardentkid.com.
In an effort to boost the performance of my upcoming game, I decided that I needed a more efficient way to use transitions with my game sprites. I have found a couple of libraries like the one by TimeSpaceMagic which also incorporates pause and time-warping. However, all of them calculate the transition properties at Runtime, which can become pretty taxing on the processor. This is unnecessary if the transition will always be the same.
That’s why I have created “AK-Tween”, a library for predetermining transitions and manually applying them at Runtime. I’ve benchmarked it with results up to 20% faster than other Runtime transition approaches, the most efficient method I had found previously. The alpha version of the library is built with easing equations based on GTween for AS3, so it will yield a nearly identical result compared to Corona’s transition easing. There’s a link available at the end of this tutorial to download AK-Tween, along with a benchmarking tool (see iPhone4 test results below).
The Core Idea
“AK-Tween” calculates all of the transitions at load time (not during Runtime) by storing the values in tables/arrays. We then iterate through them using custom Runtime functions when they’re needed. Here’s a glance at a piece of the tween calculation code which returns the array of values:
And here’s the code to actually set up a tween (we’ll use a bouncing ball as an example):
This allows our ball to bounce from y=0 to y=-120 and fall back down, with a natural-feeling quadratic ease. It will take 400ms each way (24 frames at 60 fps). We could just call the :apply() function in the last line and use ball:playTween(“bounce”) to have our tween be completely handled by AKtween. For the sake of this tutorial, however, we’re going to do the same thing manually and only use the y-value array created from the library.
So, with the predetermined y-values stored in ball.yBounceArr, we can set up our Runtime ball:bounce() script:
We call ball:bounce() whenever we want to start bouncing the ball. It sets up a local Runtime script called frameCount, which will set a new y-position for our ball on every frame of the game. We also saved a reference to this function in ball.bounceFC, which means we can pause, resume, or stop the bounce like this:
The “manual way” is harder to implement than a regular transition, but it allows us to customize precisely what we do with our animation properties. It’s extremely efficient from a performance standpoint and it’s also pause-enabled. In combination with my dynamic sprites implementation, the game should run very smoothly.
Here are the benchmark results using this method. Both manual and AKtween play functions yield nearly identical results.
In summary, we’ve defined an underlying alternate method for transitioning our objects. These benchmarks prove that the method is very efficient! We simply read values from an array and apply them to the correct object property. We can apply play functionality by passing objects to the AKtween juggler, or we can achieve total control over animations by adding and removing local runtime scripts. Either way, you may want to check out AKtween and see if its methods can benefit your app.
Posted by Brent Sorrentino. Thanks for reading...