繁体   English   中英

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

[英]Cannot call value of non function type SKShapeNode

我一直在尝试解决此错误,并尝试使字符包含在将为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
}

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

首先,由于nameString? ,需要将其打开。

guard let string = self.name else {
    return
}

其次,您不能使用Int下标访问String的字符; 您将需要使用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]

第三,您不能直接将Character转换为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
}

此时, row是将name转换为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
}

暂无
暂无

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

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