ゲームセンターによくあるメダルを落とすゲームっぽいiPhoneアプリのサンプルコードを描いてみます。
今回使った画像
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface PushScene : SKScene
@property int direction;
@end
@implementation PushScene
– (void)didMoveToView:(SKView *)view
{
[self createSceneContents];
}
– (void)createSceneContents
{
self.backgroundColor = [SKColor yellowColor];
[self createGround];
[self createMedalAndPrize];
[self createPushBar];
}
– (void)createGround
{
SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 20)];
ground.position = CGPointMake(CGRectGetMidX(self.frame) – 80, 100);
[self addChild:ground];
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
}
– (void)createMedalAndPrize
{
for (int i=0; i<20; i++) {
float x = (arc4random() % 150) + 200;
float y = 200;
double delayInSeconds = 0.1 * i;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self createMedalAt:CGPointMake(x, y)];
});
}
SKSpriteNode *prize = [SKSpriteNode spriteNodeWithImageNamed:@”prize”];
prize.position = CGPointMake(280, 260);
[self addChild:prize];
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
prize.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:prize.size];
prize.physicsBody.friction = 1.0;
});
}
– (void)createMedalAt:(CGPoint)p
{
float hue = (arc4random() % 10) * 0.1;
SKColor *color = [SKColor colorWithHue:hue saturation:0.8 brightness:0.8 alpha:1.0];
SKSpriteNode *medal = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(40, 10)];
medal.position = p;
[self addChild:medal];
medal.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:medal.size];
medal.physicsBody.friction = 1.0;
}
– (void)createPushBar
{
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(150, 40)];
bar.name = @”bar”;
bar.position = CGPointMake(0, 130);
[self addChild:bar];
bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
[self createMedalAt:p];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *bar = [self childNodeWithName:@”bar”];
if (self.direction == 0) {
if (bar.position.x < 150) {
bar.physicsBody.velocity = CGVectorMake(30, 0);
} else {
self.direction = 1;
}
} else {
if (bar.position.x > 0) {
bar.physicsBody.velocity = CGVectorMake(-30, 0);
} else {
self.direction = 0;
}
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[PushScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end