七輪の上で、おせんべいを焼くゲームをiPhoneアプリサンプルとして描いてみます。焼く時間がちょうどいいとおいしそうなお煎餅に。長いと、焦げ焦げになるようにしましょう。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface Osenbei : SKScene
@property BOOL contentCreated;
@property NSTimeInterval startTime;
@property NSUInteger state;
@end
typedef enum {
GameStateReady = 1,
GameStateStart = 2,
GameStateWaiting = 3,
GameStateStop = 4,
GameStateFinish = 5
} GameState;
@implementation Osenbei
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.backgroundColor =[SKColor greenColor];
self.state = GameStateReady;
SKSpriteNode *back = [SKSpriteNode spriteNodeWithImageNamed:@”senbei_sumibi”];
[self addChild:back];
back.position = CGPointMake(160, 200);
SKLabelNode *button = [SKLabelNode node];
button.name = @”button”;
button.text = @”すたーと“;
button.fontColor = [SKColor brownColor];
button.position = CGPointMake(160, 80);
[self addChild:button];
}
– (void)update:(NSTimeInterval)currentTime
{
if (self.state == GameStateStart) {
self.state = GameStateWaiting;
self.startTime = currentTime;
SKSpriteNode *senbei = [SKSpriteNode spriteNodeWithImageNamed:@”senbei_tane”];
senbei.name = @”senbei”;
[self addChild:senbei];
senbei.position = CGPointMake(160, 250);
NSString *path = [[NSBundle mainBundle] pathForResource:@”hokuhoku” ofType:@”sks”];
SKEmitterNode *yugeEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
yugeEmitter.name = @”hokuhoku”;
yugeEmitter.particlePosition = CGPointMake(160, 280);
[self addChild:yugeEmitter];
}
float duration = currentTime – self.startTime;
if (self.state == GameStateStop) {
self.state = GameStateFinish;
SKSpriteNode *senbei = (SKSpriteNode*)[self childNodeWithName:@”senbei”];
if (duration < 2.0) {
// NONE
} else if (duration > 3.0) {
// Bad
senbei.texture = [SKTexture textureWithImageNamed:@”senbei_bad”];
SKEmitterNode *yugeEmitter = (SKEmitterNode*)[self childNodeWithName:@”hokuhoku”];
[yugeEmitter removeFromParent];
NSString *path = [[NSBundle mainBundle] pathForResource:@”smoke” ofType:@”sks”];
SKEmitterNode *smoke = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
smoke.name = @”smoke”;
smoke.position = CGPointMake(160, 280);
[self addChild:smoke];
} else {
senbei.texture = [SKTexture textureWithImageNamed:@”senbei_good”];
}
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *node = [self childNodeWithName:@”button”];
if ([node containsPoint:p] && self.state == GameStateReady) {
SKLabelNode *label = (SKLabelNode*)[self childNodeWithName:@”button”];
SKAction *rotate = [SKAction rotateByAngle:M_PI * 2.0 duration:0.5];
SKAction *change = [SKAction runBlock:^{ label.text = @”ストップ“; }];
[label runAction:[SKAction sequence:@[rotate, change]]];
self.state = GameStateStart;
}
if ([node containsPoint:p] && self.state == GameStateWaiting) {
SKLabelNode *label = (SKLabelNode*)[self childNodeWithName:@”button”];
SKAction *rotate = [SKAction rotateByAngle:M_PI * 2.0 duration:0.5];
SKAction *change = [SKAction runBlock:^{ label.text = @”もう一枚“; }];
[label runAction:[SKAction sequence:@[rotate, change]]];
self.state = GameStateStop;
}
if ([node containsPoint:p] && self.state == GameStateFinish) {
SKNode *senbei = [self childNodeWithName:@”senbei”];
[senbei removeFromParent];
SKNode *yuge = [self childNodeWithName:@”hokuhoku”];
[yuge removeFromParent];
SKNode *smoke = [self childNodeWithName:@”smoke”];
[smoke removeFromParent];
SKLabelNode *label = (SKLabelNode*)[self childNodeWithName:@”button”];
SKAction *rotate = [SKAction rotateByAngle:M_PI * 2.0 duration:0.5];
SKAction *change = [SKAction runBlock:^{ label.text = @”スタート“; }];
[label runAction:[SKAction sequence:@[rotate, change]]];
self.state = GameStateReady;
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[Osenbei alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end