iPhone矢印ブロック

矢印を落とすと、四角がその方向に動くといったiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface ArrowScene : SKScene

@end

@implementation ArrowScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor whiteColor];

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

    self.physicsBody.dynamic = NO;

    [self createLines];

    [self createArrows];

    [self createPlayer];

}

– (void)createLines

{

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

        [self createLine:CGPointMake(i * 22+ 80, 100)];

    }

}

– (void)createArrows

{

    NSArray *arrows = @[@”↓”, @”↑”, @”←”, @”→”];

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

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

        SKShapeNode *btn = [SKShapeNode node];

        btn.position = CGPointMake(CGRectGetMaxX(self.frame) – 30, i * 50 + 200);

        btn.name = @”button”;

        btn.path = path.CGPath;

        btn.fillColor = [SKColor whiteColor];

        btn.strokeColor = [SKColor orangeColor];

        btn.lineWidth = 5;

        [self addChild:btn];

        

        SKLabelNode *l = [SKLabelNode node];

        l.text = arrows[i];

        l.fontColor = [SKColor orangeColor];

        l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;

        l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

        [btn addChild:l];

    }

}

-(void)createLine:(CGPoint)p

{

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

    node.name = @”line”;

    node.position = p;

    [self addChild:node];

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

    

    SKPhysicsJointSliding *s = [SKPhysicsJointSliding jointWithBodyA:node.physicsBody bodyB:self.physicsBody anchor:node.position axis:CGVectorMake(1, 0)];

    [self.physicsWorld addJoint:s];

}

– (void)createPlayer

{

    

    SKSpriteNode *back = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(250, 250)];

    back.position = CGPointMake(125, CGRectGetMaxY(self.frame) – 155);

    [self addChild:back];

    

    SKSpriteNode *player = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(30, 30)];

    player.name = @”player”;

    player.position = CGPointMake(160,260);

    [self addChild:player];

}

– (void)update:(NSTimeInterval)currentTime

{

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

        node.physicsBody.velocity = CGVectorMake(-20, 0);

        if (node.position.x < 78) {

            [node removeFromParent];

            [self createLine:CGPointMake(CGRectGetMaxX(self.frame)-3, node.position.y)];

        }

    }];

    

    

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

    

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

        if (node.position.y < 30) {

            node.name = @””;

            [player runAction:[SKAction moveByX:0 y:30 duration:0.5]];

            [node runAction:[SKAction fadeOutWithDuration:0.3] completion:^{

                [node removeFromParent];

            }];

        }

    }];

    

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

        if (node.position.y < 30) {

            node.name = @””;

            [player runAction:[SKAction moveByX:30 y:0 duration:0.5]];

            [node runAction:[SKAction fadeOutWithDuration:0.3] completion:^{

                [node removeFromParent];

            }];

        }

    }];

    

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

        if (node.position.y < 30) {

            node.name = @””;

            [player runAction:[SKAction moveByX:0 y:-30 duration:0.5]];

            [node runAction:[SKAction fadeOutWithDuration:0.3] completion:^{

                [node removeFromParent];

            }];

        }

    }];

    

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

        if (node.position.y < 30) {

            node.name = @””;

            [player runAction:[SKAction moveByX:-30 y:0 duration:0.5]];

            [node runAction:[SKAction fadeOutWithDuration:0.3] completion:^{

                [node removeFromParent];

            }];

        }

    }];

    

}

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

{

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

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

        if ([node containsPoint:p]) {

            

            SKNode *copy = [node copy];

            [self addChild:copy];

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

            copy.physicsBody.allowsRotation = NO;

            SKLabelNode *l = copy.children[0];

            l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

            copy.name = l.text;

        }

    }];

    

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end