Montag, 10. August 2015

Apple Swift: Getting Started #1

Apple released a newer version of its new programming Language Swift. Here are a few starter links and hints, when working with the new version Swift 2:

This link is your headquarter:
https://developer.apple.com/swift/resources/
from there you get the recommendable book "The Swift Programming Language (Swift 2 Prerelease)" and many code examples. The book's writing style is casual and animates to read more.
Unfortunately the code examples are not updated (I hope they will be in future) and this was the main reason to create this blog post, to give absolute beginners help with the start up. In all code samples you can use the converter, which automatically converts the older syntax 1.2 to the newer syntax, but there are still some errors in the samples left.

Adventure: Building a SpriteKit Game Using Swift:
AdventureScene.swift before
// ...

override init(size: CGSize) {
    leafEmitterATemplate = SKEmitterNode(fileNamed: "Leaves_01")
    leafEmitterBTemplate = SKEmitterNode(fileNamed: "Leaves_02")
    projectileSparkEmitterTemplate = SKEmitterNode(fileNamed: "ProjectileSplat")
    spawnEmitterTemplate = SKEmitterNode(fileNamed: "Spawn")
        
    super.init(size: size)

    players[0] = defaultPlayer
}

// ...

func loadWorld() {
    // ...
    let templateWorld = scene.children.first!.copy() as! SKNode
    // ...
}

// ...

func configureConnectedGameControllers() {
    // ...
    assignPresetController(controller, toIndex: playerIndex)
    // ...
}

// ...

func gameControllerDidConnect(notification: NSNotification) {
    // ...
    assignPresetController(controller, toIndex: playerIndex)
    // ...
}

// ...

func assignUnknownController(controller: GCController) {
    // ...
    controller.playerIndex = index
    // ...
}

// ...

func configureController(controller: GCController, forPlayer player: Player) {
    // ...
    loadHUDForPlayer(player, atIndex: player.controller!.playerIndex)
    // ...
}

AdventureScene.swift after
// ...

override init(size: CGSize) {
    leafEmitterATemplate = SKEmitterNode(fileNamed: "Leaves_01")!
    leafEmitterBTemplate = SKEmitterNode(fileNamed: "Leaves_02")!
    projectileSparkEmitterTemplate = SKEmitterNode(fileNamed: "ProjectileSplat")!
    spawnEmitterTemplate = SKEmitterNode(fileNamed: "Spawn")!
        
    super.init(size: size)

    players[0] = defaultPlayer
}

// ...

func loadWorld() {
    // ...
        let templateWorld = scene!.children.first!.copy() as! SKNode
    // ...
}

// ...

func configureConnectedGameControllers() {
    // ...
    assignPresetController(controller, toIndex: playerIndex.rawValue)
    // ...
}

// ...

func gameControllerDidConnect(notification: NSNotification) {
    // ...
    assignPresetController(controller, toIndex: playerIndex.rawValue)
    // ...
}

// ...

func assignUnknownController(controller: GCController) {
    // ...
    controller.playerIndex = GCControllerPlayerIndex(rawValue: index)!
    // ...
}

// ...

func configureController(controller: GCController, forPlayer player: Player) {
    // ...
    loadHUDForPlayer(player, atIndex: player.controller!.playerIndex.rawValue)
    // ...
}

All other errors are of the same kind "value of optional type" and you can simply click the QuickFix.

Keine Kommentare:

Kommentar veröffentlichen