10ポケット

ポケットに書いてある数字になるように四角をうごかしてあそぶiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SpriteKit;

@interface ViewController ()

@property (nonatomic, weak) SKScene *scene;

@property (nonatomic, weak) SKNode *coin;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createPockets];

    [self createCoins];

}

– (void)setupScene {

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

    [self.view addSubview:spriteView];

    SKScene *scene = [SKScene sceneWithSize:spriteView.frame.size];

    scene.backgroundColor = [UIColor colorWithHue:0.2 saturation:0.7 brightness:1 alpha:1];

    [spriteView presentScene:scene];

    self.scene = scene;

    

    self.scene.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.view.bounds];

}

– (void)createPockets {

    float w = CGRectGetMaxX(self.view.bounds);

    float h = CGRectGetMaxY(self.view.bounds);

    float r = w / 8.0;

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path addArcWithCenter:CGPointZero radius:r startAngle:0 endAngle:1.0*M_PI clockwise:NO];

    [path appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(-r, 0, r * 2.0, r)]];

    

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

        float x = (w / 6.0) * (2 * i +1);

        float y = h * 0.4;

        SKShapeNode *pocket = [SKShapeNode node];

        pocket.name = @”pocket”;

        pocket.path = path.CGPath;

        pocket.fillColor = [UIColor colorWithHue:0.4 saturation:0.8 brightness:1 alpha:1];

        pocket.position = CGPointMake(x, y);

        [self.scene addChild:pocket];

        

        SKLabelNode *ten = [SKLabelNode node];

        ten.text = @”10″;

        ten.fontSize = 60;

        [pocket addChild:ten];

    }

}

– (void)createCoins {

    float w = CGRectGetMaxX(self.view.bounds) / 7;

    NSArray *nums = @[@”4″, @”8″, @”6″, @”5″, @”7″];

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

        SKSpriteNode *coin = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:i * 0.15 saturation:0.9 brightness:0.7 alpha:1] size:CGSizeMake(40, 40)];

        coin.position = CGPointMake((i+1) * w, 140);

        coin.name = @”coin”;

        

        SKLabelNode *n = [SKLabelNode node];

        n.text = nums[i];

        n.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

        [coin addChild:n];

        

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

        

        [self.scene addChild:coin];

    }

}

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

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

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

        if ([node containsPoint:p]) {

            self.coin = node;

            node.xScale = 1.4;

            node.yScale = 1.4;

            node.physicsBody = nil;

        }

    }];

}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

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

    if (self.coin) {

        self.coin.position = p;

    }

}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

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

    

    if (self.coin) {

        

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

            if ([node containsPoint:p]) {

                int pocketNumbe = [[(SKLabelNode *)node.children[0] text] intValue];

                int coinNumber = [[(SKLabelNode *)self.coin.children[0] text] intValue];

                if (pocketNumbe – coinNumber <= 0) {

                    // remove pocket

                    [node runAction:[SKAction fadeOutWithDuration:0.5]];

                    node.name = @””;

                } else {

                    [(SKLabelNode *)node.children[0] setText:[NSString stringWithFormat:@”%d”, pocketNumbe – coinNumber]];

                }

                

                if (pocketNumbe – coinNumber >= 0) {

                    [self.coin runAction:[SKAction fadeOutWithDuration:0.5] completion:^{

                        [self.coin removeFromParent];

                    }];

                } else {

                    [(SKLabelNode*)self.coin.children[0] setText:[NSString stringWithFormat:@”%d”, coinNumber – pocketNumbe]];

                    self.coin.xScale = 1.0;

                    self.coin.yScale = 1.0;

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

                }

                

                self.coin = nil;

                return;

            }

        }];

        

        self.coin.xScale = 1.0;

        self.coin.yScale = 1.0;

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

        self.coin = nil;

    }

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end