检查内置字典 Swift 时游戏冻结
我正在制作一个主要基于 SpriteKit 的简单文字游戏。 有一个由 6 个字母组成的简单数组。 当在我的 Spritekit 游戏中抓取一个字母并将其添加到数组中时,会通过一段简单的代码检查该数组现在是否是合法单词。
let joined = wordArray.joined()
print(joined)
if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: joined) {
print("THATS A WORD")
AudioServicesPlaySystemSound(systemSoundFanfare)
}
这段代码位于我的 func didBegin(_ contact: SKPhysicsContact) 中)
函数
我的问题是,当执行此检查 0.5 秒左右时,整个游戏都会冻结。大概是因为它每次都会迭代整个字典! 我怎样才能解决这个问题,以便在进行此检查时 Spritekit 场景不会被中断?
I am making a simple word game based mostly in SpriteKit.
There is a simple array of 6 letters.
When a letter is grabbed in my Spritekit game and is added to the array a simple bit of code checks to see if the array is now a legitimate word..
let joined = wordArray.joined()
print(joined)
if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: joined) {
print("THATS A WORD")
AudioServicesPlaySystemSound(systemSoundFanfare)
}
this bit of code is in my func didBegin(_ contact: SKPhysicsContact)
function
My problem is the whole game freezes when it does this check for 0.5 seconds or so. Presumably because it is iterating though the entire dictionary each time!
How can I get around this so the Spritekit scene is not interrupted while this check happens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用一组来保存单词。 Swift 集合和
NSSet
有一个contains
函数,您可以在其中提供字符串并返回该字符串是否在集合中。由于集合中的每个元素都是唯一的,因此在集合中查找项目比在数组中查找项目更快。我在文字游戏中使用以下函数来快速检查字符串是否是单词
Use a set to hold the words. Swift sets and
NSSet
have acontains
function where you supply a string and returns whether the string is in the set. Because each element in a set is unique, finding an item in a set is faster than finding an item in an array.I used the following function in a word game to quickly check if a string is a word