iPhoneたちつてと

達磨落としみたいに、よこからたたいて「たちつてと」を覚えるゲームをiPhoneアプリで描いてみます。


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

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

#import <AVFoundation/AVFoundation.h>

@interface DarumaScene : SKScene

@property BOOL contentCreated;

@property int count;

@property (strong, nonatomic) SKShapeNode *mallet;

@property (strong, nonatomic) AVAudioPlayer *mySound;

@end

@implementation DarumaScene

@synthesize mallet;

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    SKSpriteNode *base = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(320, 10)];

    base.position = CGPointMake(160, 10);

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

    base.physicsBody.dynamic = NO;

    [self addChild:base];

    

    [self createDarumaBody];

    

    // mallet

    mallet = [[SKShapeNode alloc] init];

    mallet.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-60, –15, 120, 30) cornerRadius:5].CGPath;

    mallet.fillColor = [SKColor blackColor];

    mallet.strokeColor = [SKColor whiteColor];

    mallet.position = CGPointMake(250, 40);

    mallet.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(120, 30)];

    [self addChild:mallet];

}

– (void)createDarumaBody

{

    NSArray *words = @[@”,@”,@”,@”,@”];

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

        float y = 60 * i + 50;

        SKShapeNode *node = [[SKShapeNode alloc] init];

        node.name = @”darumaPart”;

        

        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-60, –30, 120, 60) cornerRadius:15];

        node.path = path.CGPath;

        node.fillColor = [SKColor greenColor];

        node.strokeColor = [SKColor brownColor];

        node.position = CGPointMake(100, y + 600);

        node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(120, 60)];

        [self addChild:node];

        

        SKLabelNode *label = [[SKLabelNode alloc] init];

        label.name = @”word”;

        label.text = words[i];

        label.fontSize = 40;

        label.position = CGPointMake(0, –15);

        [node addChild:label];

    }

}

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

{

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

    if ([mallet containsPoint:p]) {

        [mallet.physicsBody applyImpulse:CGVectorMake(-400, 0)];

        SKAction *wait = [SKAction waitForDuration:0.05];

        SKAction *cancelImpulse = [SKAction runBlock:^{

            mallet.physicsBody.velocity = CGVectorMake(0, 0);

        }];

        SKAction *move = [SKAction moveTo:CGPointMake(250, 40) duration:0.2];

        SKAction *sequence = [SKAction sequence:@[wait, cancelImpulse, move]];

        [mallet runAction:sequence];

        

        self.count = self.count + 1;

        if (self.count == 5) {

            [self createDarumaBody];

            self.count = 0;

        }

    }

}

– (void)sound:(NSString*)s

{

    NSArray *kakiku = @[@”, @”, @”, @”, @”];

    int i = [kakiku indexOfObject:s];

    NSArray *names = @[@”ta”,@”ti”,@”tu”,@”te”,@”to”];

    NSString *fileName = [names objectAtIndex:i];

    NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:fileName ofType:@”m4a”]];

    self.mySound = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];

    self.mySound.volume = 0.5;

    [self.mySound setVolume:1.0];

    [self.mySound play];

}

– (void)didSimulatePhysics

{

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

        if (node.position.x < –80) {

            

            SKLabelNode *l = (SKLabelNode*)[node childNodeWithName:@”word”];

            [self sound:l.text];

            [node removeFromParent];

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[DarumaScene alloc] initWithSize:self.view.bounds.size];

    scene.backgroundColor = [UIColor lightGrayColor];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end