iPhone足シュート

たこの足を撃ち落とすiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface LegsScene : SKScene

@end

@implementation LegsScene

– (void)didMoveToView:(SKView *)view

{

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

    [self createEnemy];

    [self createButton];

}

– (void)createEnemy

{

    SKNode *enemy = [SKNode node];

    enemy.name = @”enemy”;

    enemy.position = CGPointMake(30, 250);

    [self addChild:enemy];

    

    SKAction *moveF = [SKAction moveToX:CGRectGetMaxX(self.frame)-30 duration:3.0];

    SKAction *moveB = [SKAction moveToX:30 duration:3.0];

    [enemy runAction:[SKAction repeatActionForever:[SKAction sequence:@[moveF, moveB]]]];

    

    

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

    headPath.usesEvenOddFillRule = YES;

    [headPath appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(8, 8) radius:4 startAngle:0 endAngle:2.0*M_PI clockwise:YES]];

    [headPath appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 8) radius:4 startAngle:0 endAngle:2.0*M_PI clockwise:YES]];

    SKShapeNode *head = [SKShapeNode node];

    head.path = headPath.CGPath;

    head.fillColor = [SKColor whiteColor];

    head.lineWidth = 0;

    [enemy addChild:head];

    

    UIBezierPath *lPath = [UIBezierPath bezierPath];

    [lPath moveToPoint:CGPointZero];

    [lPath addLineToPoint:CGPointMake(0, 15)];

    [lPath addLineToPoint:CGPointMake(13, 15)];

    [lPath closePath];

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

        SKShapeNode *leg = [SKShapeNode node];

        leg.name = @”leg”;

        leg.position = CGPointMake(i * 1530, –20);

        leg.path = lPath.CGPath;

        leg.fillColor = [SKColor whiteColor];

        [enemy addChild:leg];

    }

}

– (void)createButton

{

    SKSpriteNode *node = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:CGSizeMake(40, 40)];

    node.position = CGPointMake(CGRectGetMidX(self.frame), 20);

    node.name = @”button”;

    [self addChild:node];

    

    SKLabelNode *l = [SKLabelNode node];

    l.name = @”arrow”;

    l.fontSize = 50;

    l.text = @”↑”;

    l.fontColor = [SKColor whiteColor];

    l.position = CGPointMake(CGRectGetMidX(self.frame), 20);

    [self addChild:l];

    

    SKAction *right = [SKAction rotateToAngle:-M_PI_2 duration:1.0];

    SKAction *left = [SKAction rotateToAngle:M_PI_2 duration:1.0];

    [l runAction:[SKAction repeatActionForever:[SKAction sequence:@[right, left]]]];

    

}

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

{

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

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

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

    if ([button containsPoint:p]) {

        SKNode *copyArrow = [arrow copy];

        [self addChild:copyArrow];

        [copyArrow removeAllActions];

        copyArrow.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 40)];

        

        float dx = 5 * cos(copyArrow.zRotation + M_PI_2);

        float dy = 5 * sin(copyArrow.zRotation + M_PI_2);

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

    }

}

– (void)didSimulatePhysics

{

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

    

    [self enumerateChildNodesWithName:@”arrow” usingBlock:^(SKNode *a, BOOL *stopA) {

        [enemy enumerateChildNodesWithName:@”leg” usingBlock:^(SKNode *l, BOOL *stopL) {

            if (!l.physicsBody && CGRectContainsPoint(a.frame, [enemy convertPoint:l.position toNode:self])) {

                *stopA = YES;

                *stopL = YES;

                a.name = @”hit arrow”;

                l.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:l.frame.size];

                l.name = @”fall leg”;

            }

        }];

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end