To arouse your curiosity, here are a few examples showing how to use Sparrow. The following is pure Objective-C and shows how to make use of some Sparrow features. We hope this code looks as simple and logical for you as it does for us! (For a thorough look at the features shown below, have a look at the Introduction to Sparrow!)
Create and display an image
// load image SPImage *image = [SPImage imageWithContentsOfFile:@"image.png"]; // change things like scaling, rotation, etc. image.scaleX = 2.0f; image.rotation = PI / 2.0f; image.x = 40; // display image [self addChild:image];
Load a texture from your texture atlas
SPTextureAtlas *atlas =
[SPTextureAtlas atlasWithContentsOfFile:@"atlas.xml"];
SPImage *image = [SPImage imageWithTexture:
[atlas textureByName:@"atlas_image"]];
Grouping objects together in a sprite (a DisplayObjectContainer)
SPSprite *sprite = [SPSprite sprite]; // create children SPImage *venus = [SPImage imageWithContentsOfFile:@"venus.png"]; SPImage *mars = [SPImage imageWithContentsOfFile:@"mars.png"]; // move children to some relative positions venus.x = 50; mars.x = -20; // add children to the sprite [sprite addChild:venus]; [sprite addChild:mars]; // calculate total width of all children float totalWidth = sprite.width; // rotate the whole group sprite.rotation = PI;
React to touch events
// e.g. in 'init'
[self addEventListener:@selector(onTouchEvent:) atObject:self
forType:SP_EVENT_TYPE_TOUCH];
// the corresponding listener:
- (void)onTouchEvent:(SPTouchEvent*)event
{
NSArray *touches = [[event touchesWithTarget:self
andPhase:SPTouchPhaseMoved] allObjects];
if (touches.count == 1)
{
// one finger touching
SPTouch *touch = [touches objectAtIndex:0];
SPPoint *currentPos = [touch locationInSpace:self.parent];
SPPoint *previousPos =
[touch previousLocationInSpace:self.parent];
// ...
}
else if (touches.count >= 2)
{
// two fingers touching
// ...
}
}
Create a simple textfield using one of the iPhone’s standard fonts
// create the text field SPTextField *textField = [SPTextField textFieldWithText:@"blah!"]; // set things like fontname, size, etc. textField.fontName = @"Georgia-Bold"; textField.fontSize = 18; textField.color = 0xaaaaff; //0xRRGGBB textField.hAlign = SPHAlignCenter; // horizontal alignment textField.vAlign = SPVAlignCenter; // vertical alignment // add it to the display list! [self addChild:textField];
Now use a textfield with a custom bitmap font
// e.g. during startup; file contains coordinates of chars
// and image filename
[SPTextField registerBitmapFontFromFile:@"mouse.fnt"];
// then, anywhere else:
SPTextField *bmpFontTF = [SPTextField textFieldWithWidth:300
height:120 text:@"Text drawn with a bitmap font"];
bmpFontTF.fontName = @"mouse"; // name set in the fnt-file
Animate anything!
// we want to animate the image that was created above
SPTween *tween = [SPTween tweenWithTarget:image time:5.0f
transition:SP_TRANSITION_EASE_IN];
// you can animate any property as long as it's numeric (float,
// double, int). It is animated from it's current value to
// a target value.
[tween animateProperty:@"x" targetValue:310];
[tween animateProperty:@"scaleY" targetValue:0.5];
[tween animateProperty:@"rotation" targetValue:PI_HALF];
// the so-called "juggler" will carry out the tween for you.
[self.stage.juggler addObject:tween];
Can you say that louder, please?!
// the one-liner to play sound [[SPSound soundWithContentsOfFile:@"sound.aif"] play]; // or the advanced way, with more control: SPSound *sound = [SPSound soundWithContentsOfFile:@"sound.caf"]; SPSoundChannel *channel = [sound createChannel]; [channel play];
