Created
August 7, 2014 16:02
-
-
Save kognate/45a941e251011f038957 to your computer and use it in GitHub Desktop.
bird.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameScene.swift | |
// FlappyBird | |
// | |
// Created by Josh Smith on 8/7/14. | |
// Copyright (c) 2014 Josh Smith. All rights reserved. | |
// | |
import SpriteKit | |
class GameScene: SKScene { | |
override func didMoveToView(view: SKView) { | |
/* Setup your scene here */ | |
let loser = self.childNodeWithName("loser") | |
loser.alpha = 0.0 | |
let pipes = self.childNodeWithName("lowerPipe") | |
pipes.runAction(SKAction.moveTo(CGPointMake(pipes.frame.size.width * -2, pipes.position.y), duration: 5)) | |
let upipes = self.childNodeWithName("upperPipe") | |
upipes.runAction(SKAction.moveTo(CGPointMake(upipes.frame.size.width * -2, upipes.position.y), duration: 5)) | |
} | |
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { | |
/* Called when a touch begins */ | |
let bird = self.childNodeWithName("Bird") | |
bird.position = CGPointMake(bird.position.x, bird.position.y + 50) | |
} | |
override func update(currentTime: CFTimeInterval) { | |
let bird = self.childNodeWithName("Bird") | |
bird.position = CGPointMake(bird.position.x, bird.position.y - 3) | |
if (bird.position.y < 0) { | |
let loser = self.childNodeWithName("loser") | |
let action = SKAction.fadeInWithDuration(3) | |
loser.runAction(action) | |
} | |
let lowerPipe = self.childNodeWithName("lowerPipe") | |
let upperPipe = self.childNodeWithName("upperPipe") | |
if (CGRectIntersectsRect(bird.frame, lowerPipe.frame)) { | |
let loser = self.childNodeWithName("loser") | |
let action = SKAction.fadeInWithDuration(3) | |
loser.runAction(action) | |
} | |
if (CGRectIntersectsRect(bird.frame, upperPipe.frame)) { | |
let loser = self.childNodeWithName("loser") | |
let action = SKAction.fadeInWithDuration(3) | |
loser.runAction(action) | |
} | |
if (lowerPipe.position.x < 0) { | |
let seq = SKAction.sequence([SKAction.moveTo(CGPointMake(1024, lowerPipe.position.y), duration: 0), | |
SKAction.moveTo(CGPointMake(lowerPipe.frame.size.width * -2, lowerPipe.position.y), duration: Double(arc4random_uniform(5)+2))]) | |
lowerPipe.runAction(seq) | |
} | |
if (upperPipe.position.x < 0) { | |
let seq = SKAction.sequence([SKAction.moveTo(CGPointMake(1024, upperPipe.position.y), duration: 0), SKAction.moveTo(CGPointMake(upperPipe.frame.size.width * -2, upperPipe.position.y), duration: Double(arc4random_uniform(5) + 2))]) | |
upperPipe.runAction(seq) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment