飛んでくるちくわとダンベルを上手くキャッチするゲームが昔ファミコンであったな、と思い出したので、ちくわを上手にタップすると得点が増えていくiPhoneゲームのサンプルコードを描いてみました。
今回使った画像
サンプルを動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface ChikuwaScene : SKScene
@property BOOL contentCreated;
@end
@implementation ChikuwaScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.physicsWorld.speed = 0.5;
self.backgroundColor = [SKColor colorWithRed:0 green:0.3 blue:0 alpha:1];
SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(40, 40)];
box.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:box];
SKLabelNode *score = [SKLabelNode node];
score.name = @”score”;
score.text = @”x 00″;
score.position = CGPointMake(80, CGRectGetMaxY(self.frame) – 50);
[self addChild:score];
SKSpriteNode *scoreMark = [SKSpriteNode spriteNodeWithImageNamed:@”chikuwa”];
scoreMark.size = CGSizeMake(15, 30);
scoreMark.position = CGPointMake(30, CGRectGetMaxY(self.frame) – 40);
[self addChild:scoreMark];
}
– (void)didSimulatePhysics
{
int rand = arc4random() % 100;
if (rand < 5) {
if (rand == 0) {
[self createDumbbell];
} else {
[self createChikuwa];
}
}
[self enumerateChildNodesWithName:@”chikuwa” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.y < 0) {
[node removeFromParent];
}
}];
[self enumerateChildNodesWithName:@”dumbbell” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.y < 0) {
[node removeFromParent];
}
}];
}
– (void)createChikuwa
{
SKSpriteNode *chikuwa = [SKSpriteNode spriteNodeWithImageNamed:@”chikuwa”];
chikuwa.name = @”chikuwa”;
chikuwa.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:chikuwa];
chikuwa.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:chikuwa.size];
int dx = (arc4random() % 20);
[chikuwa.physicsBody applyImpulse:CGVectorMake(dx – 10, 60)];
}
– (void)createDumbbell
{
SKSpriteNode *dumbbell = [SKSpriteNode spriteNodeWithImageNamed:@”dumbbell”];
dumbbell.name = @”dumbbell”;
dumbbell.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:dumbbell];
dumbbell.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:dumbbell.size];
int dx = (arc4random() % 20);
[dumbbell.physicsBody applyImpulse:CGVectorMake(dx – 10, 60)];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
[self enumerateChildNodesWithName:@”chikuwa” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
[self catchChikuwa:node];
}
}];
[self enumerateChildNodesWithName:@”dumbbell” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
[self catchDumbbell:node];
}
}];
}
– (void)catchChikuwa:(SKNode*)node
{
node.physicsBody.velocity = CGVectorMake(0, 0);
SKAction *fade = [SKAction fadeAlphaTo:0 duration:0.4];
[node runAction:fade completion:^{
[node removeFromParent];
}];
[self updateScore:1];
}
– (void)catchDumbbell:(SKNode*)node
{
SKLabelNode *xmark = [SKLabelNode node];
xmark.text = @”(x_x)”;
xmark.fontSize =40;
xmark.fontColor = [SKColor redColor];
[node addChild:xmark];
SKAction *fade = [SKAction scaleTo:2.0 duration:1.0];
[xmark runAction:fade completion:^{
[xmark removeFromParent];
}];
[self updateScore:-3];
}
– (void)updateScore:(int)point
{
SKLabelNode *score = (SKLabelNode*)[self childNodeWithName:@”score”];
int p = [[score.text substringFromIndex:1] intValue] + point;
if (p<0) {
p = 0;
}
score.text = [NSString stringWithFormat:@”x %02d”, p];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[ChikuwaScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end