I converted the prototype of a word game from OS X to iOS. The iOS version works well except for one problem. When I launch the game and touch a letter tile for the first time, there's a 3-5 second delay before the touch is registered and the tile I touched gets highlighted. After the first touch, every other touch registers without any delay.
My first thought was that the delay was caused by the word list taking too long to load, keeping the game from responding to player input. But the delay persists even if I wait 20-30 seconds after launching the game to start touching letter tiles.
I profiled the game with the Time Profiler instrument in Instruments, and the CPU percentage shoots up from 40% to 99% on the first touch. Later touches increase the CPU load to 50-60%. Looking at the call tree, all the time is spent in the touchesBegan function. Here's my touchesBegan code:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/ Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
// Determine the tile that was touched.
if let clickedTileNode = nodeAtPoint(location) as? SKSpriteNode {
handleTileClick(clickedTileNode)
}
// Check for button touches.
if let clickedButtonNode = nodeAtPoint(location) as? SKLabelNode {
handleButtonClick(clickedButtonNode)
}
}
}
When I make the first touch, Instruments says 100% of the time is spent in line 12. I changed the if statement in line 12 to an else-if statement, and the initial delay persists.
How do I get the first touch to register without the 3-5 second delay?