Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 10-28-2009, 04:22 PM   #2 (permalink)
TheRedge
Registered Member
 
Join Date: Oct 2009
Location: Austria
Posts: 20
TheRedge is on a distinguished road
Cool Code samples

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
TheRedge is offline   Reply With Quote
 

» Advertisements
» Online Users: 541
16 members and 525 guests
Batman, Bertrand21, BrianSlick, chiataytuday, daunmi, GHuebner, givensur, GraffitiCircus, HowEver, jasonqg49, k2c, linkmx, mariano_donati, Newbie123, tommiss12
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,318
Threads: 93,988
Posts: 402,406
Top Poster: BrianSlick (7,978)
Welcome to our newest member, Libera
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:41 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.