Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

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

In this second part of the tutorial series, you'll aim for developing a fully playable version of the game. When you're finished, the user will be able to drag the tiles and drop them on the correct targets, where they will "stick" to the spot.


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

Just thought I’d throw a solution to an Issue I had. The lastest version of using Xcode 7.3 attempted to suggest that value++ will be removed and switched the syntax to self.value =+ 1.

so the following code was corrected to this:

   if (endValue < value) {
            value -= 1
        } else {
            value += 1
        }

However a bit of investigation it seems that writing the code this way doesn’t trigger the “didSet” observer on the value property. I fixed it by writing out the statement in full.

 if (endValue < value) {
            value = value - 1
        } else {
            value = value + 1
        }

Hope this helps anyone who finds that when changing the Points in an animated way suddenly stops the whole point system from working.

1 Like

That’s interesting - thanks for sharing :slight_smile:

For future people, as this is one of the conversions to Swift 3 that took me a while to figure out:

    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector:"tick:", userInfo: nil, repeats: true)

converts to Swift 3 Beta Code:

    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(GameController.tick(timer:)), userInfo: nil, repeats: true)
1 Like

Your guide has been really helping me learn how to program in Swift, and I am very appreciative. I am trying to take the next step and to be able to identify when the cursor is in one of these images, versus clicking and dragging.

i.e. I want to identify which tile the user has dragged over, change the tile image, and record the interaction (for multiple tiles during the same interaction).

Do you have any advice on this?

@apocal - depending on what you are trying to do, I think there are a couple of methods to explore.

A. Intersection with other views’ bounds.

Something like this in TileView.swift's touchesMoved() - you might be able to make it more elegant.

       if let superview = superview {
        for view in superview.subviews {
          if view.isKindOfClass(TileView) && view != self {
            if CGRectIntersectsRect(self.frame, view.frame) {
              view.hidden = true
            }
          }
        }
      }

That depends on how accurate you want it though. The views are slightly rotated, so the frame intersection might be slightly off.

B. UIKit Dynamics.

Which I still don’t know much about. Someone asked a question about repelling other tiles so I came up with this answer on the old board:

http://archive.raywenderlich.com/forums/viewtopic.php?f=20&t=19455&start=20#p92573

Thank you. I really appreciate the feedback and the guide in general. It’s enabled me to get back into programming after ~ 10 years of being out of it. I am actually really enjoying the swift language.

Thought I’d share my progress and solution on this (in case others are also trying to solve something similar to this). What I found is that I have to manage the interaction points through the GameView because the TileView is only specific to the image areas that were created (neat!). The below is for touchesBegan, and I do the same for touchesMoved:

Game View:

`    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        if let touch = touches.first as UITouch?
        {
            let point = touch.preciseLocationInView(GameView)
            if (touch.view == GameView)
            {
                if let getLetters: String = controller.selectTiles(point)
                {
                    LettersLabel.text = getLetters
                }
            }
        }
    }`

In the method selectTiles, I then check the value of the touch point versus the bounds of the tile:

Game Controller:

`func selectTiles(touchPoint: CGPoint) -> String?
{
        for tile in tiles
        {
            // If the Tile is Touched
            if tile.frame.contains(touchPoint)
        {
            print(“do stuff”)
        }
    }
}

This has worked extremely well. Thank you again!

`

1 Like

I’m glad you found a solution. Thank you for putting it here. I’m sure it will help in the future :smile:.

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! :]