Tic Tac Toe: Graphics, Labels, and Animations

I have a roughed out version of my tic tac toe game running. If my point was to just get something working, I could call it a day at this point. However, the goal I have with this project is to put together a somewhat professional game. This requires a lot of polish and a lot more work.

The focus I have in this blog post is to do the biggest immediate thing I can do to make this look like a real game. This includes real graphics and not just colors that I used as placeholders. I also want to give the user feedback about whose turn it is and what the state of the game is. Additionally I want to add some interest to the game with animations so it feels more responsive than it does now.


This section will focus on incorporating graphics. I got a graphic design degree ten years ago and I haven’t used it much in the last decade. One of my longer term goals is to work more with graphics programs so I can get more comfortable with them. But for this project I am opting to use assets created by other people.

One resource I like a lot is Game Art Guppy. If you have worked through any tutorials from Ray Wenderlich, then the art on Game Art Guppy should look familiar. Ray’s wife Vicki did all of the art assets for the site for a number of years. Game Art Guppy is her market for low cost art assets for developers. Many assets on the site are free and none are over $25.

Since my other major hobby is cooking, I decided to use the pastry icon set from the site for my squares. I know Xs and Os are traditional, but I want this to be whimsical. There is a correlating background that goes with this tile set. These assets were used in an epic Candy Crush tutorial on Ray’s site. I highly recommend it because I learned a lot from working through it.

First I placed the background image behind the board. When I initially added the background it covered the other parts of my board, so I made its z-position -1 so that it would appear behind the board:

let background = SKSpriteNode(imageNamed: "bg_ipad_portrait")
background.zPosition = -1

I chose the donut and one of the sugar cookies for the Xs and Os because they kind of looked a little like Xs and Os. I want to keep the white background but just apply the texture on top of it. I also want to animate the tiles, but that will happen later. Here is the code I used to get this working:

switch currentPlayer {
    case .playerA:
	let playerTile = SKSpriteNode(imageNamed: "pastry_donut_320")
	playerTile.scale(to: node.size)
	playerTile.position = CGPoint(x: node.size.width / 2, 
				      y: node.size.height / 2)
	board[tiles.index(of: node)!] = .playerA
    case .playerB:
	let playerTile = SKSpriteNode(imageNamed: "pastry_starcookie02_320")
	playerTile.scale(to: node.size)
	playerTile.position = CGPoint(x: node.size.width / 2, 
				      y: node.size.height / 2)
	board[tiles.index(of: node)!] = .playerB
    case .notPlaying:
	node.color = UIColor.white
	board[tiles.index(of: node)!] = .notSelected

Each player has a pastry associated with them. If I just change the texture of the containing node to the pastry icon then they will appear to float in space, so I am making the pastry a new sprite and making it a child of the containing node. The pastry icon is way too large to fit inside the containing node, so it needs to be scaled to fit within the tile. Additionally, the anchor point of the tile is in the bottom left, so I am positioning the new sprite to sit in the middle of the sprite node.

Finally, I want to have a real app icon. I always forget to make a generic one for sample projects and tech talks and it constantly bothers me. When I see other people went to the trouble it makes me feel crappy and inadequate, so really wanted to make sure I did that on this project.

I chose the sugar cookie as my main focus on the app icon because I thought it was the more visually interesting sprite in the set. I used Illustrator to place the sprite in the view and exported it as PNG. I then used Photoshop to change the size and apply the correct labels to each size. I believe Illustrator has an option to do all of this in one step, or a least fewer than I did, but I didn’t figure out how to make that work. I have been told Sketch does this as well, but I don’t own it and I’m unfamiliar with it, so I used the thing I am comfortable with.


I want to use a label at the top of the screen to give information to the user. I want to be able to tell the user which player’s turn it is and how the game has ended. Right now I am just printing it out to the console, but that can only be seen by me when I run this in the simulator.

SpriteKit has a specific node for labels. I created one inside the class declaration but outside of any methods:

let label = SKLabelNode(fontNamed: "Noteworthy-Bold")

I created a backing image for the label, which I need to add as a sprite node to the main node hierarchy:

let labelBackground = SKSpriteNode(imageNamed: "headerBacking")
labelBackground.zPosition = 2
labelBackground.size = CGSize(width: boardSize, height: 60.0)
labelBackground.position = CGPoint(x: 0.0, y: (boardSize / 2.0) + 100)
labelBackground.anchorPoint = CGPoint(x: 0.5, y: 0.5)

I need to add the label node as a child of the label background node. I set the anchor point for the label background to the middle of the node so that the text can be placed in the center of the node.

label.text = "Tic Tac Toe"
label.fontColor = SKColor.black
label.fontSize = 40
label.zPosition = 10
label.position = CGPoint(x: 0.0, y: -20.0)

I want raw string values associated with both my player state and my win state so that I can post them in the label for the user. Here are my updates to my state enums:

enum GameState: String {
    case notPlaying
    case playerA = "Player A"
    case playerB = "Player B"

enum GameEndState: String {
    case playerAWin = "Player A Wins!"
    case playerBWin = "Player B Wins!"
    case draw = "Draw!"

I tried attaching an observer to the current user to update the label whenever the current user changed, but that not only resulted in never telling the user that the game was over, it also didn’t prevent the game from continuing. Instead, I explicitly update the label in the line of code after I update the user. I do this in the initializer but I also have to do that on the touch event:

if let isWin = checkForWin(currentPlayer: currentPlayer, board: board) {
    label.text = isWin.rawValue
    isUserInteractionEnabled = false
} else {
    currentPlayer = switchPlayer(currentPlayer: currentPlayer)
    label.text = "\(currentPlayer.rawValue)'s Turn"

Previously I was checking for each specific win condition, but since that is being tracked in the raw value I can just post the end state in the label. I don’t want the player to continue to play if the game is over, so I need to disable user interaction. If the game isn’t over, then the current player switches and the label updates.


Next I want to animate the tiles as they are selected. This works, but it’s a little boring. It’s also a little abrupt to just have an image pop up when you touch a tile. Adding an animation to the action really gives a bit of life to the application.

One thing that confused me greatly about SpriteKit was how animations were done. I knew that animations had to be a huge component of SpriteKit, but there was no SKAnimation class. I thought maybe this descends from UIKit and you would need to use Core Animation to get this to animate. After some digging I found that there is an animation class in SpriteKit, but it’s called SKAction. Why they changed it, I can’t say. People just liked it better that way.

At some point I would like to do a dedicated blog post to SKAction, but for this project I am trying to simply focus on doing what I need to for the effect that I want. I want to create some kind of transition in with the sprites so they don’t just pop out of nowhere on the tiles. I want the sprite to fade in. I also want my sprite to scale up and kind of pop out a little before scaling back to the proper size.

First, I pulled a few properties out of the code blocks that I need to calculate my actions:

let nodeSize = node.size
let largeNodeSize = CGSize(width: nodeSize.width + 30, 
			   height: nodeSize.height + 30)
let spritePosition = CGPoint(x: node.size.width / 2, 
			     y: node.size.height / 2)

Next, I created my action outside of my switch statement. I want to use the same actions for both Player A and Player B, so there isn’t any point in creating those in-line. I wanted to place them outside of the touches method, but there are properties on objects that I need to check inside that method, so the actions are declared there.

SKAction has two ways of grouping actions together: Groups and Sequences. Groups are actions that need to run together at the same time. Sequences are actions that run one after another. I need to utilize both groups and sequences for my sprite animations. The scale up and scale down need to be part of a sequence because one follows the other. That sequence has to be grouped with the fade in so that it sprite both fades in and scales simultaneously.

Here is my action code:

// Actions
let fadeAction = SKAction.fadeIn(withDuration: 0.75)
let scaleUpAction = SKAction.scale(to: largeNodeSize, duration: 0.5)
let scaleDownAction = SKAction.scale(to: nodeSize, duration: 0.25)
let scaleSequence = SKAction.sequence([scaleUpAction, scaleDownAction])
let actionGroup = SKAction.group([fadeAction, scaleSequence])

You may notice that there is no code here to specify what the starting scale is for the sprite. That needs to be set when the sprite is created. My refactored tile sprite code now looks like this:

case .playerA:
    let playerTile = SKSpriteNode(imageNamed: "pastry_donut_320")
    playerTile.position = spritePosition
    board[tiles.index(of: node)!] = .playerA

Music and Sounds

The last bit of polish I want to add to the project is music and sound effects. One interest that I have in game development is doing music and sound effects. Before I went into programming, I went to school for audio engineering and video production. My specific area of interest was sound design. I loved to sit down with an animated scene and remove all the sound and add it back as a project. One of my sound design projects can be seen here.

I need more practice to get back up to speed with sound design, along with graphics and illustration. Those are concerns for another time. But needless to say, I understand the value of music and sounds to making something feel like a finished product. So I wanted to make sure I had nice sounds for the game.

I want background music and I want a nice little “popping” sound effect for when the sprite pops into the tile. I don’t have time right now to make my own sounds or music, so I needed to find decent free sounds and music to include in the project. I don’t assume that if you find something online that it can be used in any way. I make sure that the creator has given permission for the use I have. I am willing to buy music and sound effects, if anyone reading this has any suggestions about good places to find resources.

I found the tile popping sound at Sound Effects Plus. The sounds are free to download. Their licensing and restrictions are available here.

I found my music at Incompetech. I discovered this site through Paul Hudson. He mentions the site, along with a few other good resources, in his SpriteKit book, which I reviewed here.

For the tile popping sound, I want to make sure the pop happens at the right time. I tried simply calling it at the same time the animation was happening, but because the sound was so short, the “pop” was happening while the sprite was still growing. I resolved this by adding it to my scale sequence:

let soundEffect = SKAction.playSoundFileNamed("pop.wav", waitForCompletion: false)
let scaleSequence = SKAction.sequence([scaleUpAction, soundEffect, scaleDownAction])

Finally, I added my background music to the game. Not a lot of people have written much about audio mixing in SpriteKit, so that might be a thing I do in the future. I want to be able to lower the volume of the background music so that it doesn’t drown out the sound effects, but I am not certain how to make that work. I tried a few things but none of them worked, so for now I am simply adding the sound when the application begins:

run(SKAction.playSoundFileNamed("Carpe Diem.mp3", waitForCompletion: false))

I feel comfortable with my control of SKAction for animations, but I am not happy with my control of it for sounds and sound effects. Want to research this further.


You can see the current state of the project here.

In total I spent under ten dollars on assets for this game. The sound effect and background music were free. The tile set was nine bucks. I am honestly kind of shocked at how much better this looks with just a minimal amount of work using assets I found on the internet.

For now this isn’t completely done. I still don’t handle what happens when a game ends in a way that I find satisfactory, but that is a chunk of functionality I intend to tackle in a future blog post. However, this did come along pretty well and I am happy with the results so far.