iPhoneわんちゃんタワー

降ってくるブルドックをカートの上に上手に積み上げていく、そういった落ちゲーみたいな、iPhoneアプリのサンプルコードを描いてみます。

今回使った画像


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

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface TowerScene : SKScene

@property BOOL contentCreated;

@property float lastTime;

@end

@implementation TowerScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.physicsWorld.speed = 0.3;

    self.backgroundColor = [SKColor colorWithRed:0.8 green:0.9 blue:1.0 alpha:1];

    [self createGround];

    [self createCart];

}

– (void)createGround

{

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(500, 20)];

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

    [self addChild:ground];

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

    ground.physicsBody.dynamic = NO;

}

– (void)createCart

{

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

    cart.name = @”cart”;

    cart.position = CGPointMake(CGRectGetMidX(self.frame), 40);

    [self addChild:cart];

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

    cart.physicsBody.friction = 1.0;

    

    float x[] = {CGRectGetMidX(self.frame) – 50, CGRectGetMidX(self.frame) + 40};

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

        SKSpriteNode *tire = [SKSpriteNode spriteNodeWithImageNamed:@”cart_tire”];

        tire.position = CGPointMake(x[i], 20);

        [self addChild:tire];

    

        tire.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:tire.size.width * 0.45];

        SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:cart.physicsBody bodyB:tire.physicsBody anchor:tire.position];

        [self.physicsWorld addJoint:pin];

    }

}

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

{

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

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

    

    if (cart.position.x < p.x20) {

        [cart.physicsBody applyImpulse:CGVectorMake(80, 0)];

    } else if (cart.position.x > p.x + 20){

        [cart.physicsBody applyImpulse:CGVectorMake(-80, 0)];

    }

}

– (void)update:(NSTimeInterval)currentTime

{

    if (self.lastTime == 0) {

        self.lastTime = currentTime;

    }

    

    if (currentTime – self.lastTime > 2.0) {

        [self dropDog];

        self.lastTime = currentTime;

    }

}

– (void)dropDog

{

    NSArray *colors = @[@”bulldog”, @”bluedog”, @”reddog”];

    

    int i = arc4random() % 3;

    SKSpriteNode *dog = [SKSpriteNode spriteNodeWithImageNamed:colors[i]];

    dog.name = @”dog”;

    

    float x = (arc4random() % 20) * 10 + 50;

    dog.position = CGPointMake(x, CGRectGetMaxY(self.frame));

    [self addChild:dog];

    

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

    dog.physicsBody.linearDamping = 1.0;

    dog.physicsBody.friction = 1.0;

    dog.physicsBody.angularDamping = 1.0;

    dog.physicsBody.density = 3.0;

    

}

– (void)didSimulatePhysics

{

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

        if (![self containsPoint:node.position]) {

            [node removeFromParent];

        }

    }];

}

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end