iPhone鯛お手玉

今日ドコモショップに行ったら、福福鯛というなんか縁起の良さそうなお菓子をもらったので、鯛をお手玉する感じのiPhoneアプリを描いてみます。

使っている画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface OtedamaScene : SKScene

@property BOOL contentCreated;

@end

@implementation OtedamaScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.backgroundColor = [UIColor whiteColor];

        self.contentCreated = YES;

        self.physicsWorld.speed = 0.2;

    }

}

– (void)createSceneContents

{

    SKLabelNode *upArrow = [SKLabelNode node];

    upArrow.name = @”up”;

    upArrow.position = CGPointMake(80, 100);

    upArrow.text = @”↑”;

    upArrow.xScale = 2.0;

    upArrow.fontSize = 100;

    upArrow.fontColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];

    [self addChild:upArrow];

    

    SKLabelNode *leftArrow = [SKLabelNode node];

    leftArrow.name = @”left”;

    leftArrow.position = CGPointMake(250, 80);

    leftArrow.text = @”←”;

    leftArrow.yScale = 2.0;

    leftArrow.fontSize = 100;

    leftArrow.fontColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];

    [self addChild:leftArrow];

    // sima sima

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

        SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[[SKColor redColor] colorWithAlphaComponent:0.3] size:CGSizeMake(50, 600)];

        bar.position = CGPointMake(i * 100, CGRectGetMidY(self.frame));

        [self addChild:bar];

    }

    

    [self createTai];

}

– (void)createTai

{

    SKSpriteNode *tai = [SKSpriteNode spriteNodeWithImageNamed:@”tai02″];

    tai.name = @”tai”;

    tai.position = CGPointMake(250, 430);

    [self addChild:tai];

    

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

    tai.physicsBody.dynamic = NO;

}

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

{

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

    

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

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

    

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

        if (tai.physicsBody.dynamic == NO && [tai containsPoint:p]) {

            SKSpriteNode *t = (SKSpriteNode*)tai;

            t.physicsBody.dynamic = YES;

            SKTexture *textrue = [SKTexture textureWithImageNamed:@”tai01″];

            t.texture = textrue;

            t.size = textrue.size;

            [t.physicsBody applyTorque:2.0];

            [self performSelector:@selector(createTai) withObject:nil afterDelay:1.5];

        }

        

        else if ([left containsPoint:p] && [tai containsPoint:p]) {

            tai.physicsBody.velocity = CGVectorMake(-450, 300);

        }

        else if ([up containsPoint:p] && [tai containsPoint:p]) {

            tai.physicsBody.velocity = CGVectorMake(200, 800);

        }

    }];

}

– (void)didSimulatePhysics

{

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

        if (node.position.y < 0) {

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

    [spriteView presentScene:scene];

}

@end