iPhoneくるまかうんと

画面上を走っていくクルマの数をカウントするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface TrafficScene : SKScene

@property int state;

@end

@implementation TrafficScene

– (void)didMoveToView:(SKView *)view

{

    [self createRoad];

}

– (void)createRoad

{

    SKLabelNode *title = [SKLabelNode node];

    title.text = @”Traffic”;

    title.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 50);

    [self addChild:title];

    

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

        SKSpriteNode *road = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:0.25 * i saturation:0.3 brightness:1.0 alpha:1.0] size:CGSizeMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame)/5.0)];

        road.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame)/5.0 * i + 30);

        [self addChild:road];

        

        SKLabelNode *counter = [SKLabelNode node];

        counter.name = [NSString stringWithFormat:@”counter%d”, i];

        counter.text = @”0″;

        counter.position = CGPointMake(CGRectGetMaxX(self.frame) – 80, road.position.y);

        [self addChild:counter];

    }

}

– (SKNode *)createCar

{

    SKNode *car = [SKNode node];

    [self addChild:car];

    

    SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(30, 12)];

    [car addChild:body];

    

    SKSpriteNode *roof = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(15, 10)];

    roof.position = CGPointMake(0, 10);

    [car addChild:roof];

    

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

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

        SKShapeNode *tire = [SKShapeNode node];

        tire.position = CGPointMake(20 * i – 10, –5);

        tire.path = path.CGPath;

        tire.fillColor = [SKColor lightGrayColor];

        tire.strokeColor = [SKColor blackColor];

        tire.lineWidth = 2;

        [car addChild:tire];

    }

    

    return car;

}

– (void)update:(NSTimeInterval)currentTime

{

    if (self.state != 1) {

        return;

    }

    

    int rand = arc4random() % 100;

    if (rand < 4) {

        SKNode *car = [self createCar];

        car.position = CGPointMake(-50, CGRectGetMaxY(self.frame)/5.0 * rand + 30);

        [car runAction:[SKAction moveToX:CGRectGetMaxX(self.frame)+30 duration:arc4random()%2+3] completion:^{

            [car removeFromParent];

            SKLabelNode *counter = (SKLabelNode *)[self childNodeWithName:[NSString stringWithFormat:@”counter%d”, rand]];

            counter.text = [NSString stringWithFormat:@”%d”, [counter.text intValue]+1];

        }];

    }

}

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

{

    self.state = 1;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end