         physicsWorld.gravity = CGVectorMake(0,-5)
         physicsWorld.contactDelegate = self
         
-        // create some pipes with some duratuon
+        // TODO: create some pipes with some duratuon
+        // TODO: track score of pipes passed
         
-        // contact delegate
+    }
+    
+    // MARK: #===== Frame Update =====#
+    
+    override func update( currentTime: CFTimeInterval ) {
         
-        // game over scene transitions
+        flappyBird.zRotation = self.constrain( -1, maximum: 0.5, attempt: flappyBird.physicsBody!.velocity.dy * ( flappyBird.physicsBody!.velocity.dy < 0 ? 0.005 : 0.002 ) )
         
     }
 
-// TODO: This should be refactored into a method to handle both background types
+
+    // MARK: #===== Sprites =====#
+
    
     func addSkyline () {
+
+        // TODO: there are two bugs, 1) there is a pixel between sprites. 2) there is a jump between the 3rd and 1st sprite
         
-        let skylineTexture = imagesAtlas.textureNamed("Day_Background")
+        let skylineTexture           = imagesAtlas.textureNamed( "Day_Background" )
         skylineTexture.filteringMode = SKTextureFilteringMode.Nearest
         
-        let skylineMove = SKAction.moveByX(-self.frame.width * 2.0, y:0, duration: 15)
-        let skylineReset = SKAction.moveByX(self.frame.width * 2.0, y:0 , duration: 0)
-        let skylineActions = SKAction.repeatActionForever(SKAction.sequence([skylineMove, skylineReset]))
+        let skylineMove    = SKAction.moveByX( -self.frame.width * 2.0, y: 0, duration: 15 )
+        let skylineReset   = SKAction.moveByX( self.frame.width * 2.0, y: 0 , duration: 0 )
+        let skylineActions = SKAction.repeatActionForever( SKAction.sequence( [skylineMove, skylineReset] ) )
         
         for i in 0...3 {
-            let skyline = SKSpriteNode(texture: skylineTexture)
-            skyline.position = CGPointMake(CGFloat(i) * skylineTexture.size().width, self.frame.height * 0.5)
-            skyline.runAction(skylineActions);
-            skyline.zPosition = -99
-            addChild(skyline)
+            let skyline = SKSpriteNode( texture: skylineTexture )
+            skyline.position = CGPointMake( CGFloat(i) * skylineTexture.size().width, self.frame.height * 0.5 )
+            skyline.runAction( skylineActions );
+            forwardMovement.addChild( skyline )
         }
     }
     


class GameScene: SKScene, SKPhysicsContactDelegate {
    
    let imagesAtlas = SKTextureAtlas(named: "FlappyAssets")
    let flappyBird = SKSpriteNode(imageNamed: "Yellow_Bird_Wing_Straight")
   
    override func didMoveToView(view: SKView) {

        addSkyline()
        addForeground()
        addFlappyBird()

            foreground.physicsBody?.contactTestBitMask = flappyContactMasks.Bird
            foreground.physicsBody?.categoryBitMask = flappyContactMasks.Ground

            forwardMovement.addChild(foreground)
        }
    }
    
    func addFlappyBird() {
        
        let birdTexture1 = imagesAtlas.textureNamed( "Yellow_Bird_Wing_Down" )
        let birdTexture2 = imagesAtlas.textureNamed( "Yellow_Bird_Wing_Straight" )
        let birdTexture3 = imagesAtlas.textureNamed( "Yellow_Bird_Wing_Up" )
        
        flappyBird          = SKSpriteNode( texture : birdTexture1 )
        flappyBird.position = CGPointMake( self.frame.width * 0.5, self.frame.height * 0.5 )
        
        flappyBird.physicsBody                     = SKPhysicsBody( rectangleOfSize : flappyBird.size )
        flappyBird.physicsBody?.dynamic            = true
        flappyBird.physicsBody?.allowsRotation     = false
        flappyBird.physicsBody?.contactTestBitMask = flappyContactMasks.Pipe | flappyContactMasks.Ground
        flappyBird.physicsBody?.categoryBitMask    = flappyContactMasks.Bird

        
        let flapAnimation = SKAction.repeatActionForever(
            SKAction.animateWithTextures([birdTexture1, birdTexture2,  birdTexture3], timePerFrame:0.2)
        )

        flappyBird.runAction( flapAnimation )
        
        forwardMovement.addChild(flappyBird)
    }
    
    // MARK: #===== Touch =====#
    
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        
        flappyBird.physicsBody?.velocity = CGVectorMake( 0, 0 )
        
        flappyBird.physicsBody?.applyImpulse( CGVectorMake( 0, 15 ) )
    }
   
    // MARK: #===== Collision =====#
    
    func didBeginContact(contact: SKPhysicsContact) {
    
        var firstObject  = contact.bodyA.contactTestBitMask
        var secondObject = contact.bodyB.contactTestBitMask
        
        if ( firstObject == flappyContactMasks.Bird || secondObject == flappyContactMasks.Bird ) {
            // TODO: do something when a bird collides with the ground or a pipe, gameOver scene and transition?
        }
    }
    
    // MARK: #===== Helpers =====#
    
    func constrain( minimum: CGFloat, maximum: CGFloat, attempt: CGFloat ) -> CGFloat {
        return max( minimum, min( maximum, attempt ) )
    }

}
