iPhoneパーティークラッカー

パーティーでクラッカーをぱ〜っと飛ばそう。という感じのiPhoneアプリのサンプルコードを描いてみます。

今回使っている画像


動かすとこんな感じになります

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface PartyScene : SKScene

@property BOOL contentCreated;

@end

@implementation PartyScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

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

    [self createFlags];

    [self createPartyCracker];

}

– (void)createFlags

{

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

    flags.position = CGPointMake(350, 270);

    [self addChild:flags];

}

– (void)createPartyCracker

{

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

    cracker.name = @”cracker”;

    cracker.position = CGPointMake(70, 70);

    [self addChild:cracker];

}

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

{

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

    

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

    if ([cracker containsPoint:p]) {

        

        SKAction *back = [SKAction moveTo:CGPointMake(cracker.position.x10, cracker.position.y10) duration:0.1];

        SKAction *forward = [SKAction moveTo:cracker.position duration:0.2];

        [cracker runAction:[SKAction sequence:@[back, forward]]];

        

        [self fire];

    }

}

– (void)fire

{

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

    

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

    happy.position = cracker.position;

    [self addChild:happy];

    [happy setScale:0.1];

    

    SKAction *big = [SKAction scaleTo:1.0 duration:0.5];

    SKAction *shoot = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) duration:0.3];

    SKAction *fade = [SKAction fadeAlphaTo:0 duration:3.0];

    [happy runAction:[SKAction group:@[big, shoot, fade]] completion:^{

        [happy removeFromParent];

    }];

    

    

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

        float hue = (arc4random() % 8) * 0.1 + 0.1;

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

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

        paper.name = @”paper”;

        paper.position = CGPointMake(150, 50);

        [self addChild:paper];

        

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

        paper.physicsBody.density = 0.1;

        paper.physicsBody.linearDamping = 10.0;

        

        [paper.physicsBody applyImpulse:CGVectorMake(3, 3)];

        [paper.physicsBody applyAngularImpulse:1];

    }

}

– (void)didSimulatePhysics

{

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

        if (node.position.y < –10) {

            [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 = [[PartyScene alloc] initWithSize:spriteView.bounds.size];

    [spriteView presentScene:scene];

}

@end