iPhone車が反転

画面の端にぶつかると、進む向きが反転する車をジャンプさせるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface TurnScene : SKScene

@property (nonatomic) int direction;

@end

@implementation TurnScene

– (void)didMoveToView:(SKView *)view

{

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

    self.backgroundColor = [SKColor lightGrayColor];

    [self createLine];

    [self createCar];

}

– (void)createLine

{

    float pt[] = {70, 50, 250, 100, 100, 180, 240, 250, 80, 320};

    for (int i=0; i<0.5 * sizeof(pt)/sizeof(float); i++) {

        SKSpriteNode *node = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(120, 5)];

        node.position = CGPointMake(pt[2*i], pt[2*i+1]);

        [self addChild:node];

        

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

        node.physicsBody.dynamic = NO;

    }

}

– (void)createCar

{

    SKSpriteNode *car = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 20)];

    car.name = @”car”;

    car.position = CGPointMake(40, 30);

    [self addChild:car];

    

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

    car.physicsBody.allowsRotation = NO;

    

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

    

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

        SKShapeNode *tire = [SKShapeNode node];

        tire.name = @”tire”;

        tire.path = path.CGPath;

        tire.position = CGPointMake(i==0 ? 20 : 60, 20);

        tire.strokeColor = [SKColor blackColor];

        tire.lineWidth = 5;

        tire.fillColor = [SKColor whiteColor];

        [self addChild:tire];

        

        tire.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:8];

        

        SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:tire.physicsBody bodyB:car.physicsBody anchor:tire.position];

        [self.physicsWorld addJoint:pin];

    }

}

– (void)update:(NSTimeInterval)currentTime

{

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

        if (self.direction == 0) {

            node.physicsBody.angularVelocity = –15;

        } else {

            node.physicsBody.angularVelocity = 15;

        }

    }];

    

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

    if (self.direction == 0 && car.position.x > 270) {

        self.direction = 1;

    } else if (self.direction == 1 && car.position.x < 50) {

        self.direction = 0;

    }

}

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

{

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

    [car.physicsBody applyImpulse:CGVectorMake(0, 35)];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end