iPhone脱出C

Cの形をした箱の中からボールを脱出させるゲーム、そんな感じでiPhoneアプリのサンプルコードを描いてみます。

今回使った画像


サンプルを動画で見てみる

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface EscapeScene : SKScene

@property BOOL contentCreated;

@end

@implementation EscapeScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        

        self.backgroundColor = [self color:0];

        

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

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

    [self createC];

    [self createBall];

}

– (void)createC

{

    SKNode *c = [SKNode node];

    [self addChild:c];

    

    SKColor *color = [self color:1];

    

    // top

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

    top.position = CGPointMake(160, 360);

    [c addChild:top];

    

    // bottom

    SKSpriteNode *bottom = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(280, 30)];

    bottom.position = CGPointMake(160, 110);

    [c addChild:bottom];

    

    // left

    SKSpriteNode *left = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(30, 280)];

    left.position = CGPointMake(30, 235);

    [c addChild:left];

    

    // right

    SKSpriteNode *rightA = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(30, 100)];

    rightA.position = CGPointMake(285, 160);

    [c addChild:rightA];

    

    SKSpriteNode *rightB = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(30, 100)];

    rightB.position = CGPointMake(285, 310);

    [c addChild:rightB];

    

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

        node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:[(SKSpriteNode*)node size]];

        node.physicsBody.dynamic = NO;

        node.physicsBody.restitution = 1.0;

        node.physicsBody.friction = 0;

    }];

}

– (void)createBall

{

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

    ball.name = @”ball”;

    ball.size = CGSizeMake(30, 30);

    ball.position = CGPointMake(160, 300);

    [self addChild:ball];

    

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15];

    ball.physicsBody.restitution = 1.0;

    ball.physicsBody.friction = 0;

    ball.physicsBody.linearDamping = 0;

    ball.physicsBody.velocity = CGVectorMake(0, 50);

}

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

{

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

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

    

    if (!CGRectContainsPoint(ball.frame, p)) {

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

        wall.zRotation = M_PI / 4.0;

        wall.position = p;

        [self addChild:wall];

        

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

        wall.physicsBody.dynamic = NO;

    }

}

– (void)didSimulatePhysics

{

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

    

    if (ball.position.x > CGRectGetMaxX(self.frame)) {

        SKLabelNode *clear = [SKLabelNode node];

        clear.text = @”clear”;

        clear.fontSize = 50;

        clear.fontColor = [self color:3];

        [self addChild:clear];

        

        [ball removeFromParent];

        

        SKAction *showMessage = [SKAction moveTo:CGPointMake(160, 300) duration:0.3];

        SKAction *wait = [SKAction waitForDuration:1.0];

        [clear runAction:[SKAction sequence:@[showMessage, wait]] completion:^{

            [self removeAllChildren];

            [self createSceneContents];

        }];

        

    }

}

#define ColorHex(rgbValue) [SKColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 blue:((float)(rgbValue & 0x0000FF))/255.0 alpha:1.0]

– (SKColor*)color:(int)i

{

    SKColor *myColor;

    switch (i) {

        case 0: myColor = ColorHex(0x30394F); break;

        case 1: myColor = ColorHex(0xFF434C); break;

        case 2: myColor = ColorHex(0x6ACEEB); break;

        case 3: myColor = ColorHex(0xEDE8DF); break;

        default:

            break;

    }

    return myColor;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

}

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

    SKScene *scene = [EscapeScene sceneWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end