iphoneブランコ

ブランコをこいで、ジャンプ。といった感じでiPhoneアプリのサンプルコードを描いてみます。


使った画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BalancoScene : SKScene

@property BOOL contentCreated;

@end

@implementation BalancoScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [self color:3];

    [self createGround];

    [self createBalanco];

    [self createPlayer];

    [self createLifeBar];

}

– (void)createGround

{

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[self color:4] size:CGSizeMake(CGRectGetMaxX(self.frame), 30)];

    ground.position = CGPointMake(CGRectGetMidX(self.frame), 15);

    [self addChild:ground];

    

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

    ground.physicsBody.dynamic = NO;

}

– (void)createBalanco

{

    SKSpriteNode *top = [SKSpriteNode spriteNodeWithColor:[self color:0] size:CGSizeMake(30, 30)];

    top.name = @”top”;

    top.position = CGPointMake(CGRectGetMidX(self.frame) – 50, CGRectGetMaxY(self.frame) – 30);

    [self addChild:top];

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

    top.physicsBody.dynamic = NO;

    

    SKSpriteNode *plate = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(30, 7)];

    plate.name = @”plate”;

    plate.position = CGPointMake(CGRectGetMidX(self.frame) – 50, 50);

    [self addChild:plate];

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

    

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

        CGPoint plateAnchorBolt = CGPointMake(plate.position.x30 + 60*i, plate.position.y);

        SKPhysicsJointLimit *limit = [SKPhysicsJointLimit jointWithBodyA:top.physicsBody bodyB:plate.physicsBody anchorA:top.position anchorB:plateAnchorBolt];

        [self.physicsWorld addJoint:limit];

    }

    

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

}

– (void)createPlayer

{

    SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@”man”];

    player.name = @”player”;

    player.position = CGPointMake(CGRectGetMidX(self.frame) – 50, 75);

    [self addChild:player];

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

    

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

    

    SKPhysicsJointSliding *joint = [SKPhysicsJointSliding jointWithBodyA:plate.physicsBody bodyB:player.physicsBody anchor:plate.position axis:CGVectorMake(1, 0)];

    joint.shouldEnableLimits = YES;

    joint.upperDistanceLimit = 5;

    joint.lowerDistanceLimit = 5;

    [self.physicsWorld addJoint:joint];

}

– (void)createLifeBar

{

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

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

        SKShapeNode *life = [SKShapeNode node];

        life.name = @”life”;

        life.path = path.CGPath;

        life.position = CGPointMake(i * 30 + 20, CGRectGetMaxY(self.frame) – 40);

        life.fillColor = [self color:0];

        [self addChild:life];

    }

}

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

{

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

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

    if ([player containsPoint:p]) {

        

        //check life

        SKShapeNode *life = (SKShapeNode*)[self childNodeWithName:@”life”];

        if (life) {

            life.fillColor = [self color:3];

            life.name = @”used”;

            

            float dx = player.physicsBody.velocity.dx / 20.0;

            float dy = player.physicsBody.velocity.dy / 20.0;

            [player.physicsBody applyImpulse:CGVectorMake(dx, dy)];

        } else {

            // jump

            [player.physicsBody.joints enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                [self.physicsWorld removeJoint:obj];

            }];

            [player.physicsBody applyImpulse:CGVectorMake(10, 30)];

        }

    }

}

-(void)didSimulatePhysics

{

    SKShapeNode *chains = (SKShapeNode*)[self childNodeWithName:@”chains”];

    if (!chains) {

        chains = [SKShapeNode node];

        chains.name = @”chains”;

        chains.strokeColor = [self color:2];

        [self addChild:chains];

    }

    

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

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

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:plate.position];

    [path addLineToPoint:top.position];

    chains.path = path.CGPath;

    

}

#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(0x76BF60);

        case 1:

            return ColorHex(0xB0D14F);

        case 2:

            return ColorHex(0xDBE55E);

        case 3:

            return ColorHex(0xFEF0AC);

        case 4:

            return ColorHex(0xD0AF47);

        default:

            break;

    }

    return nil;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end