To arouse your curiosity, here are a few examples about how to use Sparrow.
Create and display an image:
Code:
SPTexture *texture = [SPTexture textureWithContentsOfFile:@"jupiter.png"];
SPImage *jupiter = [SPImage imageWithTexture:texture];
jupiter.scaleX = jupiter.scaleY = 1.4f;
jupiter.x = 40;
jupiter.y = 30;
[self addChild:jupiter];
Or load the texture from your texture atlas:
Code:
SPTextureAtlas *atlas = [SPTextureAtlas atlasWithContentsOfFile:@"atlas.xml"];
NSLog(@"found %d textures.", atlas.count);
SPImage *jupiter = [SPImage imageWithTexture:[atlas textureByName:@"jupiter"]];
Now add that image to a Sprite (a DisplayObjectContainer):
Code:
SPSprite *sprite = [[SPSprite alloc] init];
jupiter.rotation = PI / 2.0f;
[sprite addChild:jupiter];
[self addChild:sprite];
[sprite release];
float spriteWidth = sprite.width; // calculates width of all contents
React to touch events:
Code:
// 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 -> move object
SPTouch *touch = [touches objectAtIndex:0];
SPPoint *currentPos = [touch locationInSpace:self.parent];
SPPoint *previousPos = [touch previousLocationInSpace:self.parent];
SPPoint *dist = [currentPos subtractPoint:previousPos];
self.x += dist.x;
self.y += dist.y;
}
else if (touches.count >= 2)
{
// two fingers touching -> rotate object
// ...
}
}
Create a simple textfield using one of the iPhone's standard fonts:
Code:
SPTextField *textField = [SPTextField textFieldWithWidth:300 height:60
text:@"TextFields can have a border and a color."];
textField.fontName = @"Georgia-Bold";
textField.fontSize = 18;
textField.color = 0xaaaaff;
textField.hAlign = SPHAlignCenter;
textField.vAlign = SPVAlignCenter;
[self addChild:textField];
Now use a textfield with a custom bitmap font:
Code:
// 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:@"It is very easy to use them with Bitmap fonts, as well!"];
bmpFontTF.fontName = @"mouse"; // the font name as set in the .fnt-file
// done!
I hope this code looks as simple and logical for you as it does for us =)
Regards,
Daniel