iPhone色石プルプル

つららみたいに天井にくっついた色石をプルプル落とすiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SpriteKit;

@interface DropScene : SKScene

@end

@implementation DropScene

– (void)didMoveToView:(SKView *)view

{

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

    [self createCeiling];

    [self startRandomCreator];

}

– (void)createCeiling

{

    SKSpriteNode *ceiling = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 40)];

    ceiling.name = @”ceiling”;

    ceiling.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 20);

    [self addChild:ceiling];

    

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

    ceiling.physicsBody.dynamic = NO;

}

– (void)startRandomCreator

{

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(createStone) userInfo:nil repeats:YES];

}

– (void)createStone

{

    NSNumber *count = [[self.children filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@”name = ‘stone'”]] valueForKeyPath:@”@count”];

    if (count.intValue > 8) {

        return;

    }

    

    float upper = arc4random() % 30 + 20;

    float lower = arc4random() % 30;

    float x = (arc4random() % 10) * 40 + 20;

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(-upper/2.0, 20)];

    [path addLineToPoint:CGPointMake(upper/2.0, 20)];

    [path addLineToPoint:CGPointMake(lower/2.0, –20)];

    [path addLineToPoint:CGPointMake(-lower/2.0, –20)];

    [path closePath];

    

    SKShapeNode *stone = [SKShapeNode node];

    stone.name = @”stone”;

    stone.path = path.CGPath;

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

    stone.lineWidth = 0;

    stone.position = CGPointMake(x, CGRectGetMaxY(self.frame) – 70);

    [self addChild:stone];

    

    stone.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath];

    stone.physicsBody.dynamic = NO;

    stone.physicsBody.restitution = 1.0;

    

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((arc4random() % 5) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        

        stone.physicsBody.dynamic = YES;

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

        SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:stone.physicsBody bodyB:ceiling.physicsBody anchorA:CGPointMake(stone.position.x, stone.position.y + 20) anchorB:CGPointMake(stone.position.x, ceiling.position.y)];

        [self.physicsWorld addJoint:spring];

        [stone.physicsBody applyImpulse:CGVectorMake(5, 0)];

    });

}

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

{

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

    SKNode *node = [self nodeAtPoint:p];

    if ([node.name isEqual:@”stone”] && node.physicsBody.dynamic == YES) {

        [self.physicsWorld removeJoint:node.physicsBody.joints.lastObject];

        node.name = @””;

    }

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    

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

    [self.view addSubview:spriteVeiw];

    SKScene *scene = [[DropScene alloc] initWithSize:spriteVeiw.frame.size];

    [spriteVeiw presentScene:scene];

}

@end