gpt4 book ai didi

ios - 无法调用非函数类型 SKShapeNode 的值

转载 作者:行者123 更新时间:2023-11-30 12:35:05 25 4
gpt4 key购买 nike

我一直在尝试修复此错误,并尝试使该字符包含在将是 int 的行中。

 func isRightTileAt(location:CGPoint) ->Bool {
//as shape node so we can get fill
var currentRect = self.atPoint(location) as! SKShapeNode
//get the 10th character which will contain the row and make it an int
// let rowOfNode = Int(currentRect.name![10]) //error(tried both of these)
var rowOfNode = Int(currentRect(name[10])) //error
//flip position is used for the row index below the screen to flip it to the top.
var currentRow = self.flipPosition + 1
var currentRowOfClick = self.flipPosition

//we reuse the flip position because it hasn't flipped yet but it normally contains the right row.
//because flip position happens after this check so it won't be sent back around yet
if self.flipPosition == 5 {
currentRowOfClick = 0
}
//if they are at least on the right row
if rowOfNode == currentRowOfClick && currentRect.fillColor.hash == 65536{
return true
}
return false
}

最佳答案

访问name的字符存在一些挑战。 SKNode的属性(property)或SKNode子类(例如 SKShapeNode )。

首先,从name开始是 String? ,需要将其拆开。

guard let string = self.name else {
return
}

其次,你无法访问 String 的字符与 Int下标;您需要使用 String.Index .

// Since Swift is zero based, the 10th element is at index 9; use 10 if you want the 11th character.
let index = string.index(string.startIndex, offsetBy: 9)
// The 10th character of the name
let char = string[index]

第三,你不能直接转换 CharacterInt 。您需要将字符转换为 String然后将字符串转换为 Int .

let rowString = String(char)

// Unwrap since Int(string:String) returns nil if the string is not an integer
guard let row = Int(rowString) else {
return
}

此时,rowname的第10个字符转换为Int .

或者,您可以将上述内容实现为扩展

extension String {
func int(at index:Int) -> Int? {
let index = self.index(self.startIndex, offsetBy: index)
let string = String(self[index])
return Int(string)
}
}

并使用它

guard let name = self.name, let row = name.int(at:9) else {
return
}

关于ios - 无法调用非函数类型 SKShapeNode 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42984693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com