iPhoneコロコロ丸

四角い石をコロコロまわして丸くしていくiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface ShpereScene : SKScene <SKPhysicsContactDelegate>

@property (nonatomic, weak) UITouch *touch;

@property (nonatomic) BOOL waitFlg;

@end

@implementation ShpereScene

– (void)didMoveToView:(SKView *)view

{

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(-100, –100, 520, 768)];

    self.physicsWorld.contactDelegate = self;

    [self createRim];

    [self createBlock:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];

}

– (void)createRim

{

    SKSpriteNode *rim = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(260, 260)];

    rim.name = @”rim”;

    rim.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    [self addChild:rim];

    

    CGRect r[] = {CGRectMake(-130, –130, 260, 10), CGRectMake(-130, –130, 10, 260), CGRectMake(120, –130, 10, 260), CGRectMake(-130, 120, 260, 10)};

    NSMutableArray *bodies = [NSMutableArray array];

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

        SKPhysicsBody *body = [SKPhysicsBody bodyWithEdgeLoopFromRect:r[i]];

        [bodies addObject:body];

    }

    

    rim.physicsBody = [SKPhysicsBody bodyWithBodies:bodies];

    rim.physicsBody.categoryBitMask = 0x1 << 2;

    rim.physicsBody.collisionBitMask = 0x1 << 2;

    rim.physicsBody.contactTestBitMask = 0x1 << 3;

    SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:rim.physicsBody bodyB:self.physicsBody anchor:rim.position];

    [self.physicsWorld addJoint:pin];

}

– (void)createBlock:(CGPoint)o

{

    SKNode *block = [SKNode node];

    block.position = o;

    [self addChild:block];

    block.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:40];

    block.physicsBody.density = 0.5;

    block.physicsBody.categoryBitMask = 0x1<<1;

    block.physicsBody.collisionBitMask = 0x1<<1;

    

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

        float x = (i % 11) * 5;

        float y = (i / 11) * 5;

        SKSpriteNode *part = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:(arc4random() % 10) * 0.1 saturation:0.8 brightness:0.8 alpha:1] size:CGSizeMake(5, 5)];

        part.name = @”p”;

        part.position = CGPointMake(x + o.x, y + o.y);

        [self addChild:part];

        

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

        part.physicsBody.categoryBitMask = 0x1 << 3;

        SKPhysicsJointFixed *f = [SKPhysicsJointFixed jointWithBodyA:part.physicsBody bodyB:block.physicsBody anchor:part.position];

        [self.physicsWorld addJoint:f];

    }

}

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

{

    self.touch = [touches anyObject];

}

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

{

    self.touch = nil;

}

– (void)update:(NSTimeInterval)currentTime

{

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

    

    if (self.touch) {

        rim.physicsBody.angularVelocity = 3;

    } else {

        rim.physicsBody.angularVelocity = 0;

    }

}

– (void)didBeginContact:(SKPhysicsContact *)contact

{

    if (!self.waitFlg) {

        self.waitFlg = YES;

        [contact.bodyB.node removeFromParent];

        [self performSelector:@selector(setWaitFlg:) withObject:0 afterDelay:0.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 = [[ShpereScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end