
カジキマグロをジャンプさせて、風船をはじいて遊ぶiPhoneゲームのサンプルコードを描いてみます。(#import の代わりに @importってのを覚えたので使ってみています。)
今回使った画像

動画でサンプルを確認
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
@import SpriteKit;
@interface SwordfishScene : SKScene
@property BOOL contentCreated;
@end
#define ColorHex(rgbValue) [SKColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@implementation SwordfishScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = ColorHex(0xE2FFFF);
SKSpriteNode *water = [SKSpriteNode spriteNodeWithColor:ColorHex(0x64C3F4) size:CGSizeMake(320, 200)];
water.position = CGPointMake(160, 100);
[self addChild:water];
[self createSwordfish];
[self createBalloon];
}
– (void)createSwordfish
{
SKSpriteNode *swordfish = [SKSpriteNode spriteNodeWithImageNamed:@”swordfish”];
swordfish.name = @”swordfish”;
swordfish.position = CGPointMake(160, 300);
swordfish.zRotation = M_PI / 2.0;
[self addChild:swordfish];
swordfish.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:swordfish.size];
swordfish.physicsBody.allowsRotation = NO;
SKLabelNode *tap = [SKLabelNode labelNodeWithFontNamed:@”chalkduster”];
tap.text = @”tap”;
tap.name = @”tapLabel”;
tap.fontColor = ColorHex(0x2B375D);
tap.position = CGPointMake(-80, –15);
tap.alpha = 0;
[swordfish addChild:tap];
}
– (void)createBalloon
{
UIBezierPath *balloonPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-30, –30, 30, 30)];
SKShapeNode *balloon = [SKShapeNode node];
balloon.path = balloonPath.CGPath;
balloon.name = @”balloon”;
balloon.position = CGPointMake(300, 400);
balloon.fillColor = ColorHex(0xFF5B22);
balloon.strokeColor = balloon.fillColor;
[self addChild:balloon];
balloon.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
balloon.physicsBody.affectedByGravity = NO;
[balloon.physicsBody applyImpulse:CGVectorMake(-10.0, 1.0)];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *swordfish = [self childNodeWithName:@”swordfish”];
if (swordfish.position.y < 200) {
swordfish.physicsBody.linearDamping = 1.0;
float l = 300 – swordfish.position.y;
[swordfish.physicsBody applyForce:CGVectorMake(0, swordfish.physicsBody.mass * 9.8 * l)];
}
SKNode *tapLabel = [self childNodeWithName:@”//tapLabel”];
if (fabs(swordfish.physicsBody.velocity.dy) < 3.0) {
if (![tapLabel actionForKey:@”flush”]) {
SKAction *fadeA = [SKAction fadeAlphaTo:1.0 duration:0.4];
SKAction *fadeB = [SKAction fadeAlphaTo:0.4 duration:0.6];
SKAction *flush = [SKAction repeatActionForever:[SKAction sequence:@[fadeA, [SKAction waitForDuration:1.0],fadeB]]];
[tapLabel runAction:flush withKey:@”flush”];
}
} else {
tapLabel.alpha = 0;
[tapLabel removeAllActions];
}
}
– (void)didSimulatePhysics
{
SKNode *balloon = [self childNodeWithName:@”balloon”];
if (!CGRectContainsPoint(CGRectMake(0, 0, 320, 500), balloon.position)) {
[balloon removeFromParent];
[self createBalloon];
}
SKNode *swordfish = [self childNodeWithName:@”swordfish”];
if (!CGRectContainsPoint(CGRectMake(0, –200, 320, 800), swordfish.position)) {
[swordfish removeFromParent];
[self createSwordfish];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *swordfish = [self childNodeWithName:@”swordfish”];
[swordfish.physicsBody applyImpulse:CGVectorMake(0, 250)];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[SwordfishScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end