iPhoneみかん手裏剣

とんでくるミカンに手裏剣を上手く当てて、半分に切るおままごと、的なiPhoneアプリのサンプルコードを描いてみます。


今回使った画像


サンプルを動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

typedef enum int8_t {

    CategoryTypeShuriken = 0x1 << 1,

    CategoryTypeFruit = 0x1 << 2,

    CategoryTypeOther = 0x1 << 3,

} CategoryType;

@interface CookingScene : SKScene <SKPhysicsContactDelegate>

@property BOOL contentCreated;

@end

@implementation CookingScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

        

        self.physicsWorld.contactDelegate = self;

        self.physicsWorld.speed = 0.3;

        

        self.backgroundColor = [SKColor colorWithHue:0.3 saturation:0.6 brightness:0.8 alpha:1.0];

        

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(throwing:)];

        [self.view addGestureRecognizer:pan];

    }

}

– (void)createSceneContents

{

    [self createShuriken];

    [self createOrange];

}

– (void)createShuriken

{

    SKSpriteNode *shuriken = [SKSpriteNode spriteNodeWithImageNamed:@”shuriken_origami”];

    shuriken.name = @”shuriken”;

    shuriken.position = CGPointMake(CGRectGetMidX(self.frame), 80);

    shuriken.zPosition = 1;

    [self addChild:shuriken];

    

    shuriken.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];

    shuriken.physicsBody.categoryBitMask = CategoryTypeShuriken;

    shuriken.physicsBody.collisionBitMask = CategoryTypeOther;

    shuriken.physicsBody.contactTestBitMask = CategoryTypeFruit;

    shuriken.physicsBody.affectedByGravity = NO;

    

    SKAction *rotation = [SKAction rotateByAngle:M_2_PI duration:0.1];

    [shuriken runAction:[SKAction repeatActionForever:rotation]];

}

– (void)createOrange

{

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

    orange.name = @”orange”;

    orange.zPosition = 2;

    orange.position = CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMidY(self.frame));

    [self addChild:orange];

    

    orange.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:orange.size.width * 0.5];

    orange.physicsBody.categoryBitMask = CategoryTypeFruit;

    orange.physicsBody.collisionBitMask = CategoryTypeFruit;

    [orange.physicsBody applyImpulse:CGVectorMake(-40, 80)];

}

– (void)throwing:(UIPanGestureRecognizer*)gr

{

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

    if (gr.state == UIGestureRecognizerStateEnded) {

        CGPoint velocity = [gr velocityInView:self.view];

        shuriken.physicsBody.velocity = CGVectorMake(velocity.x * 0.5, -velocity.y * 0.5);

    }

}

– (void)didSimulatePhysics

{

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

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

    

    if (!CGRectContainsPoint(self.frame, shuriken.position)) {

        [shuriken removeFromParent];

        [self createShuriken];

    }

    

    if (!CGRectContainsPoint(self.frame, orange.position)) {

        [orange removeFromParent];

        [self createOrange];

    }

}

– (void)didEndContact:(SKPhysicsContact *)contact

{

    SKSpriteNode *orange;

    if (contact.bodyA.categoryBitMask == CategoryTypeFruit) {

        orange = (SKSpriteNode*)contact.bodyA.node;

    } else {

        orange = (SKSpriteNode*)contact.bodyB.node;

    }

    

    SKTexture *topTexture = [SKTexture textureWithImageNamed:@”orange_top”];

    orange.texture = topTexture;

    orange.physicsBody.categoryBitMask = CategoryTypeOther;

    

    SKTexture *bottomTexture = [SKTexture textureWithImageNamed:@”orange_bottom”];

    SKSpriteNode *bottom = [orange copy];

    bottom.texture = bottomTexture;

    bottom.name = @”bottom”;

    bottom.zPosition = 0;

    [self addChild:bottom];

    bottom.physicsBody.categoryBitMask = CategoryTypeOther;

    [bottom.physicsBody applyImpulse:CGVectorMake(20, 20)];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

}

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end