芋掘りゲームで「らりるれろ」を勉強するiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ImoHoriScene : SKScene
@property BOOL contentCreated;
@property (strong, nonatomic) NSMutableArray *mySounds;
@end
const int kDefaultNumberOfWalkFrames = 4;
const float showCharacterFramesOverOneSecond = 1.0f/(float) kDefaultNumberOfWalkFrames;
@implementation ImoHoriScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
self.backgroundColor = [SKColor brownColor];
[self createSceneContents];
self.contentCreated = YES;
[self createImo];
NSTimer *timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(createImo) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[self prepareToSounds];
}
}
– (void)createSceneContents
{
SKSpriteNode *man = [SKSpriteNode spriteNodeWithImageNamed:@”man01″];
man.name = @”man”;
man.position = CGPointMake(120, 70);
[self addChild:man];
}
– (void)createImo
{
SKSpriteNode *imo = [SKSpriteNode spriteNodeWithImageNamed:@”imo”];
imo.position = CGPointMake(568, –20);
imo.name = @”imo”;
[self addChild:imo];
NSArray *words = @[@”ら“,@”り“,@”る“,@”れ“,@”ろ“];
SKLabelNode *l = [[SKLabelNode alloc] init];
l.name = @”hiragana”;
l.text = [words objectAtIndex:arc4random()%5];
l.fontColor = [SKColor yellowColor];
l.fontSize = 40;
l.position = CGPointMake(0, –18);
[imo addChild:l];
SKAction *move = [SKAction moveToX:-100 duration:5.0];
[imo runAction:move completion:^{
[imo removeFromParent];
}];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKTexture *f1 = [SKTexture textureWithImageNamed:@”man01.png”];
SKTexture *f2 = [SKTexture textureWithImageNamed:@”man02.png”];
SKTexture *f3 = [SKTexture textureWithImageNamed:@”man03.png”];
NSArray *digTextures = @[f1,f2,f3,f1];
SKAction *animationFramesAction = [SKAction animateWithTextures:digTextures timePerFrame:showCharacterFramesOverOneSecond resize:YES restore:NO];
SKNode *man = [self childNodeWithName:@”man”];
[man runAction:animationFramesAction];
[self enumerateChildNodesWithName:@”imo” usingBlock:^(SKNode *node, BOOL *stop) {
if ([man intersectsNode:node]) {
SKLabelNode *l = (SKLabelNode*)[node childNodeWithName:@”hiragana”];
[self play:l.text];
[node removeAllActions];
SKAction *waite = [SKAction waitForDuration:0.3];
[node runAction:waite completion:^{
node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
[node.physicsBody applyImpulse:CGVectorMake(0, 100)];
}];
}
}];
}
– (void)didSimulatePhysics
{
[self enumerateChildNodesWithName:@”imo” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.y < –100) {
[node removeFromParent];
}
}];
}
– (void)prepareToSounds
{
NSArray *names = @[@”ra”,@”ri”,@”ru”,@”re”,@”ro”];
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)play:(NSString*)s
{
NSArray *hiragana = @[@”ら“, @”り“, @”る“, @”れ“, @”ろ“];
NSUInteger i = [hiragana indexOfObject:s];
[(AVAudioPlayer*)self.mySounds[i] play];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 568, 320)];
[self.view addSubview:spriteView];
SKScene *scene = [[ImoHoriScene alloc] initWithSize:CGSizeMake(568, 320)];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end