简体   繁体   English

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

[英]Cannot call value of non function type SKShapeNode

I keep trying to fix this error and trying to make the character contain in the row which will be an int. 我一直在尝试解决此错误,并尝试使字符包含在将为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
}

There are several challenges accessing the characters of the name property of SKNode or an SKNode subclass (such as SKShapeNode ). 访问SKNodeSKNode子类(例如SKShapeNode )的name属性的字符存在一些挑战。

First, since name is a String? 首先,由于nameString? , it needs to be unwrapped. ,需要将其打开。

guard let string = self.name else {
    return
}

Second, you can't access the characters of a String with an Int subscript; 其次,您不能使用Int下标访问String的字符; you'll need to use a String.Index . 您将需要使用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]

Third, you can't directly convert a Character to an Int . 第三,您不能直接将Character转换为Int You'll need to convert the character to a String and then convert the string to an Int . 您需要将字符转换为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
}

At this point, row is the 10th character of name converted to an Int . 此时, row是将name转换为Int的第十个字符。

Alternatively, you can implement the above as an extension 另外,您也可以将上述内容作为扩展

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

and use it with 并与

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM