Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 27, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/a464fc50c11f9157d998 to your computer and use it in GitHub Desktop.
Save mzsima/a464fc50c11f9157d998 to your computer and use it in GitHub Desktop.
falling down bridge
//
// ViewController.m
// FallingDownBridge
//
// Created by MizushimaYusuke on 8/27/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SpriteKit;
typedef NS_ENUM(NSInteger, MyTapType) {
STOP = 0,
START = 1,
JUMP = 2,
Air = 3
};
@interface ViewController () <SKSceneDelegate, SKPhysicsContactDelegate>
@property (nonatomic, weak) SKScene *scene;
@property (nonatomic, weak) SKShapeNode *player;
@property (nonatomic) NSInteger tapType;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createFloor];
[self createPlayer];
[self createBottom];
}
- (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
s.delegate = self;
s.scene.physicsWorld.contactDelegate = self;
[sv presentScene:s];
s.backgroundColor = [self color:3];
[self.view addSubview:sv];
self.scene = s;
}
- (void)createFloor {
float w = CGRectGetMaxX(self.view.bounds) / 3.0;
float h = 10;
for (int i=0; i<3; i++) {
int color = (i==1) ? 2 : 1;
CGSize size = CGSizeMake((i==1) ? w * 0.8 : w, h);
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[self color:color] size:size];
bar.name = [NSString stringWithFormat:@"bar%d", i];
bar.position = CGPointMake(w * i + w * 0.5, 100);
bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];
bar.physicsBody.categoryBitMask = 1 << 1;
bar.physicsBody.contactTestBitMask = 1 << 2;
bar.physicsBody.dynamic = NO;
[self.scene addChild:bar];
}
}
- (void)createPlayer {
float r = 30;
SKShapeNode *p = [SKShapeNode shapeNodeWithCircleOfRadius:r];
p.fillColor = [self color:0];
p.position = CGPointMake(r, 300);
SKSpriteNode *deco = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(10, 10)];
deco.position = CGPointMake(r * 0.6, 0);
[p addChild:deco];
p.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:r];
p.physicsBody.categoryBitMask = 1 << 2;
p.physicsBody.restitution = 0;
[self.scene addChild:p];
self.player = p;
}
- (void)createBottom {
float h = 50;
float w = CGRectGetMaxX(self.view.bounds);
SKSpriteNode *btm = [SKSpriteNode spriteNodeWithColor:[self color:4] size:CGSizeMake(w, h)];
btm.position = CGPointMake(w * 0.5, h * 0.5);
[self.scene addChild:btm];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.tapType == STOP) {
self.tapType = START;
} else if (self.tapType == START) {
self.tapType = JUMP;
}
}
#define TopSpeed 3
- (void)update:(NSTimeInterval)currentTime forScene:(SKScene *)scene {
if (self.tapType == STOP) return;
if (fabs(self.player.physicsBody.angularVelocity) < TopSpeed) {
[self.player.physicsBody applyTorque:-0.1];
}
if (self.tapType == JUMP) {
self.tapType = Air;
[self.player.physicsBody applyImpulse:CGVectorMake(0, 80)];
}
SKNode *mBar = [self.scene childNodeWithName:@"bar1"];
mBar.physicsBody.velocity = CGVectorMake(0, MAX(mBar.physicsBody.velocity.dy, -1));
if (self.player.position.y < -30) {
[self.player removeFromParent];
[self.scene.children enumerateObjectsUsingBlock:^(SKNode *n, NSUInteger idx, BOOL *stop) {
if ([n.name hasPrefix:@"bar"]) [n removeFromParent];
}];
[self createPlayer];
[self createFloor];
self.tapType = STOP;
}
}
- (void)didBeginContact:(SKPhysicsContact *)contact {
for (SKNode *n in @[contact.bodyA.node, contact.bodyB.node]) {
if ([n.name isEqual:@"bar1"] && !n.physicsBody.dynamic) {
n.physicsBody.dynamic = YES;
n.physicsBody.linearDamping = 1;
n.physicsBody.allowsRotation = NO;
}
}
if (self.tapType == Air) self.tapType = START;
}
#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000)>>16)/255.0 green:((rgb & 0xFF00)>>8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1]
- (UIColor *)color:(int)i {
if (i > 4) return nil;
int code[] = {0x468966, 0xFFF0A5, 0xFFB03B, 0x26100B, 0x8E2800};
return ColorHex(code[i]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment