When I first started iPhone programming last year, I decided I wanted to stay away from third-party frameworks at first, so I could learn as much of the native environment as possible. My first animation-based project used CALayers, but I later converted it to use OpenGL for better performance.
I am definitely not opposed to using third-party frameworks. When I’m not trying to wring the last bit of performance out of a device, I’d rather deal with higher-level abstractions than directly with OpenGL.
Cocos2D-iPhone is a very popular open source framework for 2D games and graphics applications. It seems very feature-rich, including things like visual effects, particle systems and even integrated physics engines!
But I was immediately drawn to the Sparrow Framework when I first heard about it. It, too, is an open source 2D graphics/game framework for iOS. It has far fewer features than Cocos2D (possibly a boon, depending on your outlook—less code to add to your app) but its main attraction (to me) is that it is modelled after the ActionScript 3 API. For someone like myself who has used Flash for many years, this is a definite plus.
When I was writing the Vampire simulator, I needed to make the vampire sparkle. I figured that this simple animation task would be well suited for my first exploration of the Sparrow framework.
Creating a new Sparrow app is very simple. Just duplicate the “scaffold” folder and rename the Xcode project within. You will have to do a one-time Xcode settings change: adding a SPARROW_SRC folder reference to point to where the Sparrow source files are on your hard drive.
The documentation that is available for Sparrow is minimal but very, very clearly written. Also, the source code is easy to follow. If you have any background with the ActionScript 3 class library, the learning curve is practically zero. I was shocked at how quickly I was making things happen with it.
Here’s a simple example from the vampire app. This snippet places the image “vampire.png” at the centre of the screen:
SPImage *image = [SPImage imageWithContentsOfFile:@"vampire.png"];
image.x = (self.width - image.width) / 2;
image.y = (self.height - image.height) / 2;
[self addChild:image]; |
SPImage *image = [SPImage imageWithContentsOfFile:@"vampire.png"];
image.x = (self.width - image.width) / 2;
image.y = (self.height - image.height) / 2;
[self addChild:image];
Responding to events (touch events, or timing) will be familiar to you if you’ve used ActionScript (or JavaScript, for that matter), using the addEventListener
method:
[self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME]; |
[self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
This will cause the onEnterFrame:
method on self
to be called on every frame of the animation.
Refugees from Flash should note: while Sparrow is modelled after the ActionScript 3 libraries, it is only a small, small subset of it. For example, it does not include any of drawing API (on the other hand, if you want to do any custom drawing, you can subclass SPDisplayObject
and draw with OpenGL directly).
I definitely plan to use Sparrow for whatever my next game project might be. I’ll likely have more to say about it then. I’ll be interested to see how performance holds up if a lot of elements are flying around the screen.