iPhoneひらがな坂道

坂道を使ってボールをとばして遊んで「はひふへほ」を覚えるiPhoneアプリを描いてみます。(SpriteKitを使いました。)

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

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

#import <AVFoundation/AVFoundation.h>

@interface SlideScene : SKScene <SKPhysicsContactDelegate>

@property BOOL contentCreated;

@property (assign, nonatomic) int count;

@property NSMutableArray *mySounds;

@end

@implementation SlideScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        [self prepareToSounds];

        self.contentCreated = YES;

        self.physicsWorld.contactDelegate = self;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [UIColor whiteColor];

    

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(200, 10)];

    ground.position = CGPointMake(100, 100);

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

    ground.physicsBody.dynamic = NO;

    ground.name = @”slope”;

    [self addChild:ground];

    

    SKSpriteNode *stopper = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(10, 50)];

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

    stopper.physicsBody.dynamic = NO;

    stopper.position = CGPointMake(-95, 20);

    [ground addChild:stopper];

    [self createBall];

    

    [self createHole];

    

    UIButton *play = [[UIButton alloc] initWithFrame:CGRectMake(10, 260, 50, 50)];

    play.backgroundColor = [UIColor greenColor];

    [play setTitle:@”→” forState:UIControlStateNormal];

    play.titleLabel.font = [UIFont boldSystemFontOfSize:50];

    [self.view addSubview:play];

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

    

    UIButton *up = [[UIButton alloc] initWithFrame:CGRectMake(120, 260, 50, 50)];

    up.backgroundColor = [UIColor greenColor];

    [up setTitle:@”↑” forState:UIControlStateNormal];

    up.titleLabel.font = [UIFont boldSystemFontOfSize:50];

    [self.view addSubview:up];

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

    

}

– (void)createBall

{

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

    ball.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –20, 40, 40)].CGPath;

    ball.strokeColor = [SKColor greenColor];

    ball.position = CGPointMake(35, 130);

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];

    ball.name = @”ball”;

    ball.physicsBody.contactTestBitMask = 0x1 << 1;

    [self addChild:ball];

}

– (void)createHole

{

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

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

        float x = i * 70 + 250;

        float y = 40;

        

        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(-30, –40, 10, 100)];

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

        h.fillColor = [UIColor greenColor];

        h.position = CGPointMake(x, y);

        h.path = path.CGPath;

        h.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path.CGPath];

        h.physicsBody.dynamic = NO;

        [self addChild:h];

        

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

        l.text = words[i];

        l.fontSize = 40;

        l.fontColor = [UIColor greenColor];

        l.position = CGPointMake(x, y);

        l.name = @”hiragana”;

        l.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];

        l.physicsBody.categoryBitMask = 0;

        l.physicsBody.contactTestBitMask = 0x1;

        l.physicsBody.dynamic = NO;

        [self addChild:l];

    }

}

– (void)play

{

    SKNode *node = [self childNodeWithName:@”ball”];

    int vx = arc4random() % 10 + 45;

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

}

– (void)up

{

    self.count = (self.count + 1) % 5;

    SKNode *node = [self childNodeWithName:@”slope”];

    node.zRotation = (M_PI/20.0)* self.count;

}

– (void)didBeginContact:(SKPhysicsContact *)contact

{

    NSArray *nodes = @[contact.bodyA.node, contact.bodyB.node];

    for (SKNode *node in nodes) {

        if ([node.name isEqual:@”hiragana”]) {

            [self sound:[(SKLabelNode*)node text]];

        }

    }

}

– (void)prepareToSounds

{

    NSArray *names = @[@”ha”,@”hi”,@”fu”,@”he”,@”ho”];

    self.mySounds = [[NSMutableArray alloc] init];

    for (NSString *s in names) {

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

        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];

        [player prepareToPlay];

        [self.mySounds addObject:player];

    }

}

– (void)sound:(NSString*)s

{

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

    NSUInteger i = [kakiku indexOfObject:s];

    [(AVAudioPlayer*)self.mySounds[i] play];

}

– (void)didSimulatePhysics

{

    SKNode *ball = [self childNodeWithName:@”ball”];

    if (ball.position.y < –20) {

        [ball removeFromParent];

        [self createBall];

    }

}

@end

@interface ViewController ()

@property (strong, nonatomic) 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 = [[SlideScene alloc] initWithSize:CGSizeMake(568, 320)];

    [self.spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end