iPhone泡あわ

水を一滴、たらしたら下で泡がブクブクブクブク〜〜ってなるイメージのiPhoneアプリを描いてみます。


動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BubbleBox : SKScene

@property BOOL contentCreated;

@end

@implementation BubbleBox

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1];

    [self createBox];

}

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

{

    SKNode *bubble = [self createBubble];

    [self addChild:bubble];

}

– (SKNode*)createBubble

{

    SKShapeNode *bubble = [[SKShapeNode alloc] init];

    bubble.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-10, –10, 20, 20)].CGPath;

    bubble.fillColor = [SKColor whiteColor];

    bubble.strokeColor = [SKColor clearColor];

    

    bubble.name = @”bubble”;

    

    float x = arc4random()% 40 * 2 + 120;

    bubble.position = CGPointMake(x, 600);

    

    bubble.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

    

    return bubble;

}

– (void)createBox

{

    SKSpriteNode *left = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(10, 80)];

    left.position = CGPointMake(100, 10);

    [self addChild:left];

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

    left.physicsBody.dynamic = NO;

    

    SKSpriteNode *right = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(10, 80)];

    right.position = CGPointMake(220, 10);

    [self addChild:right];

    right.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:left.size];

    right.physicsBody.dynamic = NO;

    

    SKSpriteNode *bottom = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(130, 10)];

    bottom.position = CGPointMake(160, 50);

    [self addChild:bottom];

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

    bottom.physicsBody.dynamic = NO;

}

– (void)didSimulatePhysics

{

    if ([self.children count] > 100) {

        if ([self childNodeWithName:@”bubble”]) {

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

                node.name = @”stop”;

            }];

            [self performSelector:@selector(cleanup) withObject:Nil afterDelay:5.0];

        }

    }

    

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

        if (node.position.y < 0) {

            [node removeFromParent];

            [self createBubble];

        }

        

        if (abs(node.physicsBody.velocity.dy) < 0.00001 && node.position.y < 300) {

            SKNode *bubble = [self createBubble];

            float x = 5 + node.position.x;

            float y = 5 + node.position.y;

            bubble.position = CGPointMake(x, y);

            [self addChild:bubble];

        }

    }];

}

– (void)cleanup

{

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

        [node removeFromParent];

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[BubbleBox alloc] initWithSize:self.view.bounds.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end