iPhoneトントン相撲

土俵をトントンして力士を戦わせる紙相撲ゲームをiPhoneアプリとして描いてみます。


動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface Sumo : SKScene

@property BOOL contentCreated;

@end

@implementation Sumo

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        self.backgroundColor = [UIColor lightGrayColor];

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    [self createRikishi];

    [self createDohyo];

}

– (void)createRikishi

{

    SKSpriteNode *red = [SKSpriteNode spriteNodeWithImageNamed:@”rikisiRed”];

    red.position = CGPointMake(200, 200);

    [self addChild:red];

    red.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:red.size];

    

    

    SKSpriteNode *green = [SKSpriteNode spriteNodeWithImageNamed:@”rikisiGreen”];

    green.position = CGPointMake(300, 200);

    [self addChild:green];

    green.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:green.size];

    

}

– (void)createDohyo

{

    SKSpriteNode *dohyoTop = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(568, 20)];

    dohyoTop.position = CGPointMake(284, 80);

    [self addChild:dohyoTop];

    dohyoTop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:dohyoTop.size];

    

    

    for (int i=0; i<8; i++) {

        SKSpriteNode *button = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(30, 30)];

        button.position = CGPointMake(i * 70 + 30, 40);

        button.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:button.size];

        [self addChild:button];

        

        button.name = @”button”;

    }

    

    SKSpriteNode *dohyoBase = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(568, 20)];

    dohyoBase.position = CGPointMake(284, 10);

    [self addChild:dohyoBase];

    dohyoBase.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:dohyoBase.size];

    dohyoBase.physicsBody.dynamic = NO;

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInNode:self];

    

    [self enumerateChildNodesWithName:@”button” usingBlock:^(SKNode *node, BOOL *stop) {

        if([node containsPoint:p]) {

            [node.physicsBody applyImpulse:CGVectorMake(0, 200)];

        }

    }];

}

@end

@interface ViewController ()

@property SKView *spriteView;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.spriteView = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 568, 320)];

    [self.view addSubview:self.spriteView];

    

    SKScene *scene = [[Sumo alloc] initWithSize:CGSizeMake(568, 320)];

    [self.spriteView presentScene:scene];

    

    UIButton *start = [UIButton buttonWithType:UIButtonTypeCustom];

    [start setTitle:@”start” forState:UIControlStateNormal];

    start.frame = CGRectMake(500, 30, 60, 30);

    start.backgroundColor = [UIColor blackColor];

    [self.view addSubview:start];

    

    [start addTarget:self action:@selector(restart) forControlEvents:UIControlEventTouchUpInside];

}

– (void)restart

{

    SKScene *newScene = [[Sumo alloc] initWithSize:CGSizeMake(568, 320)];

    [self.spriteView presentScene:newScene transition:[SKTransition flipHorizontalWithDuration:0.5]];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end