iPhoneバランス棒

棒を左右に傾けて、落ちてくるボールを右か左に落として飛ばして、という感じのiPhoneアプリサンプルコードを描いてみます。


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

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BalanceScene : SKScene

@property BOOL contentCreated;

@property NSTimeInterval lastTime;

@end

@implementation BalanceScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [SKColor yellowColor];

    

    SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(300, 20)];

    bar.name = @”bar”;

    bar.position = CGPointMake(CGRectGetMidX(self.frame), 160);

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

    [self addChild:bar];

    

    SKNode *blank = [SKNode node];

    blank.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(5, 5)];

    blank.physicsBody.dynamic = NO;

    [self addChild:blank];

    

    SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:blank.physicsBody bodyB:bar.physicsBody anchor:bar.position];

    pin.lowerAngleLimit = – 0.2;

    pin.upperAngleLimit = 0.2;

    pin.shouldEnableLimits = YES;

    [self.physicsWorld addJoint:pin];

    

    SKLabelNode *uplabel = [SKLabelNode node];

    uplabel.name = @”left”;

    uplabel.text = @”←”;

    uplabel.fontColor = [SKColor blackColor];

    uplabel.position = CGPointMake(bar.position.x50, 50);

    [self addChild:uplabel];

    

    SKLabelNode *downlabel = [SKLabelNode node];

    downlabel.name = @”right”;

    downlabel.text = @”→”;

    downlabel.fontColor = [SKColor blackColor];

    downlabel.position = CGPointMake(bar.position.x + 50, 50);

    [self addChild:downlabel];

}

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

{

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

    

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

    SKNode *upLabel = [self childNodeWithName:@”left”];

    SKNode *downLabel = [self childNodeWithName:@”right”];

    

    if ([upLabel containsPoint:p]) {

        [bar.physicsBody applyAngularImpulse:0.3];

    } else if ([downLabel containsPoint:p]) {

        [bar.physicsBody applyAngularImpulse:-0.3];

    }

}

– (void)createColorBall

{

    float hue = (arc4random() % 10) * 0.1;

    SKColor *color = [SKColor colorWithHue:hue saturation:1 brightness:1 alpha:1];

    SKShapeNode *ball = [SKShapeNode node];

    ball.name = @”ball”;

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

    ball.fillColor = color;

    ball.position = CGPointMake(CGRectGetMidX(self.frame), 350);

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

    [self addChild:ball];

}

– (void)update:(NSTimeInterval)currentTime

{

    if (self.lastTime == 0 || (currentTime – self.lastTime > 1.0)) {

        self.lastTime = currentTime;

        [self createColorBall];

    }

}

– (void)didSimulatePhysics

{

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

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

        [ball removeFromParent];

    }

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    SKView *spriteView = [[SKView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxY(self.view.frame), CGRectGetMaxX(self.view.frame))];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[BalanceScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end