iPhoneカラーガン

鉄砲の色を変えて、色々な玉を発射してあそぶiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface ColorGunScene : SKScene <SKPhysicsContactDelegate>

@end

@implementation ColorGunScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor colorWithWhite:0.8 alpha:1.0];

    self.physicsWorld.contactDelegate = self;

    [self createColorBall];

    [self createColorGun];

    [self startTimer];

}

– (void)createColorBall

{

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –20, 40, 40)];

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

        SKShapeNode *ballButton = [SKShapeNode node];

        ballButton.path = path.CGPath;

        ballButton.name = @”ballButton”;

        ballButton.position = CGPointMake(i * 50 + 30, CGRectGetMaxY(self.frame) – 50);

        ballButton.fillColor = [SKColor colorWithHue:0.3 * i + 0.1 saturation:0.6 brightness:0.6 alpha:1.0];

        [self addChild:ballButton];

    }

}

– (void)createColorGun

{

    SKNode *colorgun = [SKNode node];

    colorgun.name = @”colorgun”;

    colorgun.position = CGPointMake(100, 100);

    [self addChild:colorgun];

    

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

    [colorgun addChild:frame];

    

    SKSpriteNode *grip = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(20, 40)];

    grip.position = CGPointMake(-20, –10);

    [colorgun addChild:grip];

    

    SKAction *rotate = [SKAction rotateToAngle:M_PI_4 duration:1.0];

    SKAction *rotateR = [SKAction rotateToAngle:-M_PI_4 duration:1.0];

    [colorgun runAction:[SKAction repeatActionForever:[SKAction sequence:@[rotate, rotateR]]]];

}

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

{

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

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

        if ([node containsPoint:p]) {

            [self colorChange:[(SKShapeNode *)node fillColor]];

            return;

        }

    }];

    

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

    if ([colorgun containsPoint:p]) {

        [self shot];

    }

}

– (void)colorChange:(SKColor *)color

{

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

    [colorgun.children enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        [(SKSpriteNode *)obj setColor:color];

    }];

}

– (void)shot

{

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

    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithColor:[colorgun.children[0] color] size:CGSizeMake(20, 20)];

    ball.name = @”ball”;

    ball.position = colorgun.position;

    [self addChild:ball];

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

    ball.physicsBody.categoryBitMask = 0x1;

    ball.physicsBody.contactTestBitMask = 0x2;

    float r = 20;

    float dx = r * cos(colorgun.zRotation);

    float dy = r * sin(colorgun.zRotation);

    [ball.physicsBody applyImpulse:CGVectorMake(dx, dy)];

}

– (void)startTimer

{

    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(createEnemy) userInfo:nil repeats:YES];

}

– (void)createEnemy

{

    SKSpriteNode *ene = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(30, 30)];

    ene.name = @”enemy”;

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

        SKSpriteNode *eye = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(2, 10)];

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

        [ene addChild:eye];

    }

    ene.position = CGPointMake(CGRectGetMaxX(self.frame) – 10, arc4random() % 100 + 50);

    [self addChild:ene];

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

    ene.physicsBody.categoryBitMask = 0x2;

    ene.physicsBody.affectedByGravity = NO;

    ene.physicsBody.velocity = CGVectorMake(-40, 0);

}

– (void)didBeginContact:(SKPhysicsContact *)contact

{

    SKNode *a = contact.bodyA.node;

    SKNode *b = contact.bodyB.node;

    if ([a.name isEqualToString:@”enemy”] && [b.name isEqualToString:@”ball”])

    {

        [(SKSpriteNode *)a setColor:[(SKSpriteNode *)b color]];

    } else if ([b.name isEqualToString:@”enemy”] && [a.name isEqualToString:@”ball”])

    {

        [(SKSpriteNode *)b setColor:[(SKSpriteNode *)a color]];

    }

}

– (void)didSimulatePhysics

{

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

        if (![self containsPoint:node.position]) {

            [node removeFromParent];

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end