iPhone魚トントン

ボタンをトントン押して魚を上に進めるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SpriteKit;

@interface FishScene : SKScene

@end

@implementation FishScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:1 alpha:1];

    

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

        SKNode *rope = [self createRope];

        rope.position = CGPointMake(100 + i * 140, CGRectGetMidY(self.frame) + 50);

        

        SKShapeNode *fish = [self createFish:[SKColor colorWithHue:0.2 * i saturation:0.8 brightness:1 alpha:1]];

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

        fish.position = CGPointMake(rope.position.x, 120);

        

        SKSpriteNode *btn = [SKSpriteNode spriteNodeWithColor:fish.fillColor size:CGSizeMake(50, 30)];

        btn.position = CGPointMake(fish.position.x, 15);

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

        [self addChild:btn];

    }

}

– (SKShapeNode *)createFish:(SKColor *)color

{

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:20 startAngle:0.6 *M_PI endAngle:2.3*M_PI clockwise:YES];

    [path addLineToPoint:CGPointZero];

    [path closePath];

    SKShapeNode *fish = [SKShapeNode node];

    fish.path = path.CGPath;

    fish.fillColor = color;

    fish.strokeColor = [SKColor clearColor];

    [self addChild:fish];

    

    SKSpriteNode *eye = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(3, 3)];

    eye.position = CGPointMake(-10, 5);

    [fish addChild:eye];

    

    SKSpriteNode *tail = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(15, 15)];

    tail.zRotation = M_PI/4.0;

    tail.position = CGPointMake(0, –33);

    [fish addChild:tail];

    

    fish.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(30, 5) center:CGPointMake(0, 15)];

    return fish;

}

– (SKNode *)createRope

{

    SKSpriteNode *rope = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(3, 220)];

    [self addChild:rope];

    

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

        SKSpriteNode *knot = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(10, 10)];

        knot.position = CGPointMake(0, i * 30100);

        [rope addChild:knot];

        

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

        knot.physicsBody.dynamic = NO;

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

    }

    

    return rope;

}

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

{

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

    SKNode *n = [self nodeAtPoint:p];

    if ([n.name hasPrefix:@”btn”]) {

        SKNode *fish = [self childNodeWithName:[NSString stringWithFormat:@”fish%@”, [n.name substringFromIndex:3]]];

        [self up:fish];

    }

}

– (void)up:(SKNode *)fish

{

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

    [fish.physicsBody applyImpulse:CGVectorMake(0, 2.5)];

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

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

    });

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    

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

    [self.view addSubview:spriteView];

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

    [spriteView presentScene:scene];

}

@end