Kodeco Forums

How To Make a Letter / Word Game with UIKit and Swift: Part 3/3

This third and final part of the series will be the most fun of them all! In this part, you're going to be adding a lot of cool and fun features


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2185-how-to-make-a-letter-word-game-with-uikit-and-swift-part-3-3

Swift 3 (Beta) Adjustments to the code.

This code:

func preloadAudioEffects(effectFileNames:[String]) {
  for effect in AudioEffectFiles {
    let soundPath = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(effect)
    let soundURL = NSURL.fileURLWithPath(soundPath)
 
    var loadError:NSError?
    let player = AVAudioPlayer(contentsOfURL: soundURL, error: &loadError)
    assert(loadError == nil, "Load sound failed")
 
    player.numberOfLoops = 0
    player.prepareToPlay()
 
    audio[effect] = player
  }
}

In Swift 3 Beta becomes:

func preloadAudioEffects(effectFileNames:[String]) {
    for effect in AudioEffectFiles {
        do {
            var soundURL = URL.init(fileURLWithPath: Bundle.main().resourcePath!);
            try soundURL.appendPathComponent(effect)

            let player = try AVAudioPlayer(contentsOf: soundURL)
        
            player.numberOfLoops = 0
            player.prepareToPlay()
            
            audio[effect] = player
        } catch {
            assert(false, "Load sound failed")
        }
    }
}

More Swift 3 Beta changes:

func disableEmitterCell() {
  emitter.setValue(0, forKeyPath: "emitterCells.cell.birthRate")
}

to

func disableEmitterCell() {
    emitter.birthRate = 0;
}

And

//disable the emitter
var delay = Int64(0.1 * Double(NSEC_PER_SEC))
var delayTime = dispatch_time(DISPATCH_TIME_NOW, delay)
dispatch_after(delayTime, dispatch_get_main_queue()) {
  self.disableEmitterCell()
}
 
//remove explosion view
delay = Int64(2 * Double(NSEC_PER_SEC))
delayTime = dispatch_time(DISPATCH_TIME_NOW, delay)
dispatch_after(delayTime, dispatch_get_main_queue()) {
  self.removeFromSuperview()
}

to

    DispatchQueue.main.after(when: .now() + 0.1, execute: {
        self.disableEmitterCell()
    })
    
    DispatchQueue.main.after(when: .now() + 2, execute: {
      self.removeFromSuperview()
    })

@minman - thank you for posting these :slight_smile:

Thank you very much for the solutions!

I re-worked the entire code to Swift 3 up until here and got stuck.

I appreciate your skill and knowledge.

These two lines:

DispatchQueue.main.after(when: .now() + 0.1, execute: {
self.disableEmitterCell()
})

DispatchQueue.main.after(when: .now() + 2, execute: {
  self.removeFromSuperview()
})

Became this:

    // disables the emitter
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
        self.disableEmitterCell()
    })

    
    // removes explosion view
    DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
        self.removeFromSuperview()
    })

I’m a little late to this party but trying to convert the code into SpriteKit and having an issue with matching the tiles and targets. I’ve switched the tiles into SKSpriteNodes with the letters as SKLabelNodes added as children. Using the TileDragDelegateProtocol I’m not detecting any contact likely because I haven’t figured out how to rewrite the extension comparing the nodes. I’ve also tried using SKPhysicsContactDelegate (in duplicate project), I can detect contact but it’s between any tile and any target, again likely issue with comparison of subclass properties. Any recommendation on which path I should pursue?

I’m afraid I don’t really understand the problem you are having.

Have you gone through this tutorial: https://www.raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners?

This will tell you about collision as well as other useful SpriteKit things.

In the contact delegate, you can test the name of the sprite, or the class (if let x = y as? ClassName etc). So you can test to see what objects are contacted.

Thanks! It was the " as? ClassName" that did the trick!!

1 Like

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]