iPhoneブロック消し

ブロックをタップするときえます。でも、けしても、けしても、降ってくるので、好きな色が集まるまでいつまででも遊べます。というかんじでiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BoxScene : SKScene

@property BOOL contentCreated;

@end

@implementation BoxScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    [self createWall];

    [self fallBlocks];

}

– (void)createWall

{

    // right & left

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

        SKSpriteNode *wallBlock = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(20, 20)];

        wallBlock.position = CGPointMake(6, (i + 1.45) * 22);

        [self addChild:wallBlock];

        wallBlock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 20)];;

        wallBlock.physicsBody.dynamic = NO;

        

        wallBlock = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(20, 20)];

        wallBlock.position = CGPointMake(CGRectGetMaxX(self.frame) – 6, (i + 1.45) * 22);

        [self addChild:wallBlock];

        wallBlock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 20)];;

        wallBlock.physicsBody.dynamic = NO;

    }

    // bottom

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

        SKSpriteNode *wallBlock = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(20, 20)];

        wallBlock.position = CGPointMake(i * 22 + 6, 10);

        [self addChild:wallBlock];

        wallBlock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 20)];;

        wallBlock.physicsBody.dynamic = NO;

    }

}

– (void)fallBlocks

{

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

        [self performSelector:@selector(createBlock:) withObject:@(i%10) afterDelay:(i/10) * 0.5];

    }

}

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

{

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

    

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@”name beginswith[cd] %@”, @”block”];

    [[self.children filteredArrayUsingPredicate:predicate] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        if([obj containsPoint:p]) {

            [obj removeFromParent];

            NSNumber *index = @([[[obj name] substringFromIndex:@”block”.length] intValue]);

            [self createBlock:index];

        }

    }];

}

– (void)createBlock:(NSNumber*)index

{

    float size = (320.040.0) / 10.0;

    float hue = (arc4random() % 100) * 0.01;

    SKColor *color = [SKColor colorWithHue:hue saturation:0.6 brightness:1.0 alpha:1.0];

    

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

    block.name = [NSString stringWithFormat:@”block%d”, [index intValue]];

    block.position = CGPointMake([index intValue] * size * 1.02 + (size/2.0 + 17), CGRectGetMaxY(self.frame) + size);

    [self addChild:block];

    

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

    block.physicsBody.allowsRotation = NO;

    block.physicsBody.friction = 0;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end