iPhoneガラポン

福引きのガラガラのiPhoneアプリを描いてみます。SpriteKitをつかってます。玉の出るところに壁を作って一度に全部が出てしまわないようにしています。

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

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface GaraponScene : SKScene

@end

@implementation GaraponScene

– (void)didMoveToView:(SKView *)view

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        [self createSceneContents];

    });

}

– (void)createSceneContents

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(80, 25)];

    [path addArcWithCenter:CGPointMake(0,0) radius:100 startAngle:M_PI * 0.1 endAngle:M_PI * 2.0 clockwise:YES];

    [path addLineToPoint:CGPointMake(60, 0)];

    [path addLineToPoint:CGPointMake(40, 40)];

    [path addLineToPoint:CGPointMake(58, 50)];

    [path addLineToPoint:CGPointMake(63, 43)];

    

    SKShapeNode *garagara = [SKShapeNode node];

    garagara.position = CGPointMake(160, 200);

    garagara.path = path.CGPath;

    [self addChild:garagara];

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

    garagara.name = @”garagara”;

    

    UIBezierPath *ballPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-10, –10, 20, 20)];

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

        SKShapeNode *node = [SKShapeNode node];

        node.path = ballPath.CGPath;

        node.position = CGPointMake(160 + arc4random() % 50, 250);

        node.fillColor = [SKColor colorWithHue:(arc4random() % 10) * 0.1 saturation:1 brightness:1 alpha:1];

        [self addChild:node];

        node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

    }

}

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

{

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

    node.physicsBody.angularVelocity = 2;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

    [self.view addSubview:spriteView];

    

    SKScene *scene = [GaraponScene sceneWithSize:self.view.bounds.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end