iPhone バー

I字型の棒を上から落としてあそぶiPhoneアプリのサンプルコードを描いてみます


動かすとこんな感じです。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BarScene : SKScene

@end

@implementation BarScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [self color:4];

    [self createDot];

    [self createIBar];

    [self createButton];

}

– (void)createDot

{

    int dotPoint[] = {

                        1,0,1,0,1,

                        0,1,0,1,0,

                        0,0,1,1,1

                      };

    

    for (int i=0; i<sizeof(dotPoint)/sizeof(int); i++) {

        if (dotPoint[i] == 1) {

            float x = (i % 5) * 60 + 40;

            float y = 300 – (i / 5) * 100;

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

            SKShapeNode *dot = [SKShapeNode node];

            dot.position = CGPointMake(x, y);

            dot.fillColor = [self color:arc4random()%3];

            dot.strokeColor = dot.fillColor;

            dot.path = path.CGPath;

            [self addChild:dot];

            

            dot.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];

            dot.physicsBody.dynamic = NO;

        }

    }

}

– (void)createIBar

{

    SKNode *iBar = [SKNode node];

    iBar.name = @”iBar”;

    iBar.position = CGPointMake(160, 420);

    

    SKSpriteNode *barA = [SKSpriteNode spriteNodeWithColor:[self color:3] size:CGSizeMake(80, 10)];

    [iBar addChild:barA];

    

    SKSpriteNode *barB = [SKSpriteNode spriteNodeWithColor:[self color:3] size:CGSizeMake(10, 30)];

    barB.position = CGPointMake(-35, 0);

    [iBar addChild:barB];

    

    SKSpriteNode *barC = [SKSpriteNode spriteNodeWithColor:[self color:3] size:CGSizeMake(10, 30)];

    barC.position = CGPointMake(35, 0);

    [iBar addChild:barC];

    

    [self addChild:iBar];

    

    iBar.physicsBody = [SKPhysicsBody bodyWithBodies:@[

        [SKPhysicsBody bodyWithRectangleOfSize:barA.size],

        [SKPhysicsBody bodyWithRectangleOfSize:barB.size center:barB.position],

        [SKPhysicsBody bodyWithRectangleOfSize:barC.size center:barC.position],]];

    iBar.physicsBody.restitution = 0;

    iBar.physicsBody.dynamic = NO;

    

    SKAction *move = [SKAction repeatActionForever:

                      [SKAction sequence:

                       @[[SKAction moveTo:CGPointMake(CGRectGetMaxX(self.frame), iBar.position.y) duration:3.0],[SKAction moveTo:CGPointMake(0, iBar.position.y) duration:3.0]]

                       ]];

    [iBar runAction:move];

}

– (void)createButton

{

    SKLabelNode *btn = [SKLabelNode node];

    btn.name = @”btn”;

    btn.text = @”Go!”;

    btn.fontSize = 30;

    btn.position = CGPointMake(160, 50);

    [self addChild:btn];

}

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

{

    SKNode *node = [self childNodeWithName:@”btn”];

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

    if ([node containsPoint:p]) {

        [self fall];

    }

}

– (void)fall

{

    SKNode *node = [self childNodeWithName:@”iBar”];

    [node removeAllActions];

    node.physicsBody.dynamic = YES;

}

– (void)didSimulatePhysics

{

    SKNode *node = [self childNodeWithName:@”iBar”];

    if (node.position.y < 0) {

        [node removeFromParent];

        [self createIBar];

    }

}

#define ColorHex(rgbValue) [SKColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:0.8]

– (SKColor*)color:(int)i

{

    switch (i) {

        case 0:

            return ColorHex(0x433E54);

        case 1:

            return ColorHex(0x744662);

        case 2:

            return ColorHex(0xBD4D5D);

        case 3:

            return ColorHex(0xCCB058);

        case 4:

            return ColorHex(0x7B90B3);

        default:

            break;

    }

    return nil;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[BarScene alloc] initWithSize:spriteView.bounds.size];

    [spriteView presentScene:scene];

}

@end