iPhoneかえる流し

上から流れてくるカエルを水鉄砲で移動させるiPhoneアプリのサンプルコードを描いてみます。

今回使った画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface FrogScene : SKScene

@property BOOL contentCreated;

@end

@implementation FrogScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

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

    [self createFrog];

    [self createButton];

}

– (void)createFrog

{

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

    SKNode *target = [SKNode node];

    target.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 100);

    [self addChild:target];

    target.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];

    

    SKSpriteNode *leaf = [SKSpriteNode spriteNodeWithImageNamed:@”leaf”];

    [target addChild:leaf];

    

    SKSpriteNode *frog = [SKSpriteNode spriteNodeWithImageNamed:@”frog”];

    frog.position = CGPointMake(0, 10);

    [target addChild:frog];

}

– (void)createButton

{

    SKSpriteNode *hose = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(20, 50)];

    hose.name = @”hose”;

    hose.position = CGPointMake(CGRectGetMidX(self.frame), 25);

    [self addChild:hose];

}

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

{

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

    

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

    NSArray *points = @[[NSValue valueWithCGPoint:p], [NSValue valueWithCGPoint:hose.position]];

    

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

        [self performSelector:@selector(createBubble:) withObject:points afterDelay:0.1 * i];

    }

}

– (void)createBubble:(NSArray*)points

{

    CGPoint selectPoint = [points[0] CGPointValue];

    CGPoint hosePoint = [points[1] CGPointValue];

    

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-5, –5, 10, 10)];

    SKShapeNode *bubble = [SKShapeNode node];

    bubble.name = @”bubble”;

    bubble.position = hosePoint;

    bubble.path = path.CGPath;

    [self addChild:bubble];

    float dx = selectPoint.x – hosePoint.x;

    float dy = selectPoint.y – hosePoint.y;

    CGVector vector = CGVectorMake(dx / 100.0, dy / 100.0);

    bubble.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];

    [bubble.physicsBody applyImpulse:vector];

}

– (void)didSimulatePhysics

{

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

        if(![self containsPoint:node.position]) {

            [node removeFromParent];

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

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

    [spriteView presentScene:scene];

}

@end