iPhoneぱんくず

パン屑を並べて、パクパク食べてもらうシンプルなiPhoneアプリのサンプルコードを描いてみます。

今回使った画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BreadcrumbScene : SKScene <SKPhysicsContactDelegate>

@property BOOL contentCreated;

@property int counter;

@property SKNode *touched;

@end

@implementation BreadcrumbScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [SKColor lightGrayColor];

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

    self.physicsWorld.contactDelegate = self;

    [self createBread];

    [self createEater];

}

– (void)createBread

{

    SKTexture *bread = [SKTexture textureWithImageNamed:@”bread”];

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

        float x = (i % 3) / 3.0;

        float y = (i / 3) / 3.0;

        CGRect r = CGRectMake(x, y, 1.0/3.0, 1.0/3.0);

        SKTexture *texture = [SKTexture textureWithRect:r inTexture:bread];

        SKSpriteNode *breadcrumb = [SKSpriteNode spriteNodeWithTexture:texture];

        breadcrumb.name = [NSString stringWithFormat:@”bread%d”,i + 1];

        breadcrumb.position = CGPointMake(250 + x * bread.size.width, 40 + y * bread.size.height);

        [self addChild:breadcrumb];

        

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

        breadcrumb.physicsBody.categoryBitMask = 0x1 << 1;

        breadcrumb.physicsBody.collisionBitMask = 0x0;

        

    }

}

– (void)createEater

{

    SKSpriteNode *eater = [SKSpriteNode spriteNodeWithImageNamed:@”eater01″];

    eater.name = @”eater”;

    eater.position = CGPointMake(160, 260);

    [self addChild:eater];

    

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

    eater.physicsBody.categoryBitMask = 0x1 << 2;

    eater.physicsBody.contactTestBitMask = 0x1 << 1;

}

– (void)update:(NSTimeInterval)currentTime

{

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

    if (!self.touched && self.counter > 0) {

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@”name beginswith[c] %@”, @”bread”];

        NSArray *arr = [self.children filteredArrayUsingPredicate:predicate];

        if ([arr count]) {

            SKNode *bread = arr[0];

            float dx = bread.position.x – eater.position.x;

            float dy = bread.position.y – eater.position.y;

            float u = hypotf(dx, dy);

            CGVector v = CGVectorMake(20 * dx / u,  20 * dy / u);

            eater.physicsBody.velocity = v;

        }

    } else {

        eater.physicsBody.velocity = CGVectorMake(0, 0);

    }

}

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

{

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

    CGRect breadRect = CGRectMake(250, 40, 100, 100);

    if (self.counter < 9 && CGRectContainsPoint(breadRect, p)) {

        self.counter ++;

        self.touched = [self childNodeWithName:[NSString stringWithFormat:@”bread%d”, self.counter]];

    }

}

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

{

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

    if (self.touched) self.touched.position = p;

}

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

{

    self.touched = nil;

}

– (void)didBeginContact:(SKPhysicsContact *)contact

{

    SKNode *breadcrumb, *eater;

    if ([contact.bodyA.node.name isEqual:@”eater”]) {

        eater = contact.bodyA.node;

        breadcrumb = contact.bodyB.node;

    } else {

        eater = contact.bodyB.node;

        breadcrumb = contact.bodyA.node;

    }

    

    breadcrumb.physicsBody = nil;

    

    SKTexture *open = [SKTexture textureWithImageNamed:@”eater02″];

    SKTexture *close = [(SKSpriteNode*)eater texture];

    SKAction *eat = [SKAction animateWithTextures:@[open, close] timePerFrame:0.2];

    [eater runAction:[SKAction repeatAction:eat count:3]];

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

    [breadcrumb runAction:fade completion:^{

        [breadcrumb 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 = [[BreadcrumbScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end