iPhoneループコース

四角をジェットコースターのループみたいにクルクルっと飛ばすiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface LoopScene : SKScene

@end

@implementation LoopScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor lightGrayColor];

    self.physicsWorld.gravity = CGVectorMake(0, 0);

    [self createLoop];

}

– (void)createLoop

{

    UIBezierPath *pathA = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:50 startAngle:0 endAngle:M_PI clockwise:NO];

    SKShapeNode *loopA = [SKShapeNode node];

    loopA.path = pathA.CGPath;

    loopA.position = CGPointMake(100, 200);

    [self addChild:loopA];

    loopA.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:pathA.CGPath];

    loopA.physicsBody.dynamic = NO;

    

    UIBezierPath *pathB = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:50 startAngle:0 endAngle:M_PI clockwise:YES];

    SKShapeNode *loopB = [SKShapeNode node];

    loopB.path = pathB.CGPath;

    loopB.position = CGPointMake(180, 300);

    [self addChild:loopB];

    loopB.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:pathB.CGPath];

    loopB.physicsBody.dynamic = NO;

    

    

    UIBezierPath *pathC = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:50 startAngle:0.5*M_PI endAngle:2.0*M_PI clockwise:YES];

    SKShapeNode *loopC = [SKShapeNode node];

    loopC.path = pathC.CGPath;

    loopC.position = CGPointMake(195, 100);

    [self addChild:loopC];

    loopC.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:pathC.CGPath];

    loopC.physicsBody.dynamic = NO;

}

– (SKNode *)createBall

{

    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:0.1 * (arc4random()%10) saturation:0.8 brightness:1.0 alpha:1.0] size:CGSizeMake(20, 20)];

    ball.name = @”ball”;

    ball.position = CGPointMake(60, 400);

    [self addChild:ball];

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

    ball.physicsBody.linearDamping = 0;

    ball.physicsBody.angularDamping = 0;

    ball.physicsBody.friction = 0.1;

    ball.physicsBody.collisionBitMask = 0x2;

    ball.physicsBody.categoryBitMask = 0x1;

    

    return ball;

}

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

{

    SKNode *ball = [self createBall];

    [ball.physicsBody applyImpulse:CGVectorMake(0, –10)];

}

– (void)didSimulatePhysics

{

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

        if(node.position.x > 350) {

            [node removeFromParent];

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end