I am working on a vertical-scrolling 2D platform game using GameplayKit, SpriteKit and I am writing it in Swift 2.0.Today I have been working on a tiling-routine which repositions SKNode right after they have left the upper or lower screen-border. I am not spawning new SKNodes because when the game becomes very fast it is more performant to simply reposition and reuse SKNodes than to destroy and create new ones.The whole code is encapsulated into a GKComponent and is being applied to a layer entity. You can assign different Tiles where the tile system just has one single column but multiple rows. Tiles can be of different size and so on.Here's the Code:override func updateWithDeltaTime(seconds: NSTimeInterval) {    super.updateWithDeltaTime(seconds)    for child: SKSpriteNode in self.tileNodes {        let absolutePosition = RPGameScene.sharedGameScene.convertPoint(child.position, fromNode: child.parent!)        /* Camera is moving up */        if cameraComponent.cameraState == RPCameraState.MovingNorth.rawValue {            /* See if cameraState has just switched */            if previousCameraState != cameraComponent.cameraState {                /* In case it has we have to up-invert tileIndex */                /* We are doing this by enumerating over tileNodes                   count +1 (+1 for new placement) and adding one                    count per time */                /* Normally it would be: self.tileNodes.count + 1 but                   since we start by 0 it goes automatically up to                   count + 1 */                for _ in 0...self.tileNodes.count { tileIndex += 1 }                previousCameraState = cameraComponent.cameraState            }            if absolutePosition.y + child.texture!.size().height <= 0 {                /* We have to add old textures size to the                   lowerPositionOffset since we are moving this node to                   the upperPositionOffset */                lowerPositionOffset += child.texture!.size().height                /* We have to work with a reference to new texture                   since even after changing texture the current child                   still has its old size! */                let newTexture = self.tileSet.tiles[tileIndex]                child.runAction(SKAction.setTexture(newTexture, resize: true))                child.position.y = upperPositionOffset                /* Add textures size to the position offset */                upperPositionOffset += newTexture.size().height                tileIndex += 1            }        }        /* Camera is moving down */        else {            if absolutePosition.y >= RPGameSceneSettings.height {                /* See if cameraState has just switched */                if previousCameraState != cameraComponent.cameraState {                    /* In case it has we have to down-invert tileIndex */                    /* We are doing this by enumerating over tileNodes                       count +1 (for new placement) and subtracting                       one count per time */                    /* Normally it would be: self.tileNodes.count + 1                       but since we start by 0 it goes automatically up                       to count + 1 */                    for _ in 0...self.tileNodes.count { tileIndex -= 1 }                    previousCameraState = cameraComponent.cameraState                }                /* We have to substract old texture size from                   upperPositionOffset since we are moving this node to                   the lowerPositionOffset */                upperPositionOffset -= child.texture!.size().height                /* We have to work with a reference to new texture                   since even after changing texture the current child                   still has its old size! */                let newTexture = self.tileSet.tiles[tileIndex]                /* When moving down we have to subtract newTextures                   height before replacing it                    due to Y-AnchorPoint = 0 */                lowerPositionOffset -= newTexture.size().height                child.runAction(SKAction.setTexture(newTexture,                                                     resize: true))                child.position.y = lowerPositionOffset                tileIndex -= 1            }        }    }}There is nothing wrong with the code - It's working like I wanted it to. But there is one thing I would really like to know:Since coming from Objective-C and just making my first steps with Swift I am pretty sure that there are ways to optimize this code - especially when using a modern language like Swift. But the problem is, I am not really that deep into the Swift language features, especially the new ones.So how can I optimize the code above using pure Swift language features?EDIT:There is quite a lot of repetition in that code - I could imagine using a nested function to combine this all together.EDIT:Below you'll find the parts missing in the code sample above for a better understanding:unowned let renderComponent: RPRenderComponentlet tileSet: RPTileSetvar tileNodes: [SKSpriteNode]var cameraComponent: RPCameraComponent!var previousCameraState = RPCameraState.Resting.rawValueprivate var tileIndex_: Int = 0var tileIndex: Int {    get { return tileIndex_ }    set(newTileIndex) {        tileIndex_ = newTileIndex        if tileIndex_ >= self.tileSet.tiles.count {            tileIndex_ = 0        }        else if tileIndex_ < 0 {            tileIndex_ = self.tileSet.tiles.count - 1        }    }}private var upperPositionOffset: CGFloat = 0private var lowerPositionOffset: CGFloat = 0 tileSet is of type RPTileSet which is:struct RPTileSet {    let tileSize: CGSize    let tiles: [SKTexture]    let startTile: SKTexture?}
Vertical-scrolling 2D platform game
swift;sprite kit