iPhoneボールがこぼれる

コップの中からボールがこぼれ落ちるようなiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface GlassScene : SKScene

@end

@implementation GlassScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [self color:0];

    

    

    SKLabelNode *l = [SKLabelNode node];

    l.text = @”spill”;

    l.fontName = @”chalkduster”;

    l.fontColor = [[SKColor whiteColor] colorWithAlphaComponent:0.7];

    l.position = CGPointMake(160, 40);

    [self addChild:l];

    

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    

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

        SKNode *glass = [self createGlass];

        glass.position = CGPointMake(100 + 50 * i, 300100*i);

        

        SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:glass.physicsBody bodyB:self.physicsBody anchor:CGPointMake(glass.position.x + 5,glass.position.y)];

        pin.shouldEnableLimits = YES;

        pin.upperAngleLimit = 0.4;

        pin.lowerAngleLimit = –M_PI/2.0 + 0.1;

        [self.physicsWorld addJoint:pin];

    }

}

– (SKNode *)createGlass

{

    SKNode *glass = [SKNode node];

    glass.name = @”glass”;

    SKSpriteNode *n1 = [SKSpriteNode spriteNodeWithColor:[self color:4] size:CGSizeMake(50, 5)];

    n1.position = CGPointMake(25, –20);

    [glass addChild:n1];

    

    SKSpriteNode *n2 = [SKSpriteNode spriteNodeWithColor:[self color:4] size:CGSizeMake(50, 5)];

    n2.position = CGPointMake(25, 20);

    [glass addChild:n2];

    

    SKSpriteNode *n3 = [SKSpriteNode spriteNodeWithColor:[self color:4] size:CGSizeMake(5, 40)];

    n3.position = CGPointMake(0, 0);

    [glass addChild:n3];

    

    [self addChild:glass];

    

    glass.position = CGPointMake(160, 280);

    

    glass.physicsBody = [SKPhysicsBody bodyWithBodies:@[[SKPhysicsBody bodyWithRectangleOfSize:n1.size center:n1.position], [SKPhysicsBody bodyWithRectangleOfSize:n2.size center:n2.position],[SKPhysicsBody bodyWithRectangleOfSize:n3.size center:n3.position]]];

    

    return glass;

}

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

{

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

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:NO];

    SKShapeNode *ball = [SKShapeNode node];

    ball.path = path.CGPath;

    ball.position = p;

    ball.fillColor = [self color:arc4random()%3+1];

    [self addChild:ball];

    

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15];

}

– (void)didSimulatePhysics

{

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

        [node.physicsBody applyTorque:0.05];

    }];

}

#define ColorHex(rgbValue) [SKColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

– (SKColor *)color:(NSInteger)i

{

    switch (i) {

        case 0:

            return ColorHex(0x2E5734);

        case 1:

            return ColorHex(0x27804D);

        case 2:

            return ColorHex(0x74B466);

        case 3:

            return ColorHex(0x98C97A);

        case 4:

            return ColorHex(0xA7D9A2);

        default:

            break;

    }

    return nil;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end