Ios SKShapeNode子类框架元素引用的是UIView,而不是SKNode
生成以下异常: 运算符“-”的用法不明确 以下是line和other类的对象类型声明:Ios SKShapeNode子类框架元素引用的是UIView,而不是SKNode,ios,swift,swift2,skshapenode,Ios,Swift,Swift2,Skshapenode,生成以下异常: 运算符“-”的用法不明确 以下是line和other类的对象类型声明: let dx = line.frame.midX - other.frame.midX 我注意到当我跳转到定义时(⌘ + 单击)在Xcode中,frame对象将我带到UIView中的frame属性,而不是SKNode 我的子类划分不正确吗?如何消除运算符异常错误的模糊用法?问题的出现是因为您在创建这些对象的类中既没有导入UIKit也没有导入SpriteKit 例如,如果您有,比如,GameViewContr
let dx = line.frame.midX - other.frame.midX
我注意到当我跳转到定义时(⌘ + 单击)在Xcode中,frame
对象将我带到UIView
中的frame
属性,而不是SKNode
我的子类划分不正确吗?如何消除运算符异常错误的模糊用法?问题的出现是因为您在创建这些对象的类中既没有导入
UIKit
也没有导入SpriteKit
例如,如果您有,比如,GameViewController
类:
import Foundation
import SpriteKit
class Line: SKShapeNode {
var plane:Plane!
init(rect: CGRect, plane: Plane) {
super.init()
self.path = CGPathCreateWithRect(rect, nil)
self.plane = plane
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
请确保导入上述任一框架:
即
other
引用的类是什么?@pricingo other也是一种类型的Line classI无法解释Line
和other
是否都是Line
对象,甚至是Line
和UIView
对象。当编译器无法解析-
运算符两侧的类型时,将抛出不明确的错误消息。确保两者都是CGFloat
。这将是解决这个问题的起点。@Pricingo,是的!这是我的问题。无法解析line.frame
和other.frame
对象中的midX属性。向后操作时,两个帧对象的行为就像它们是UIView
对象一样,这解释了它们为什么没有midX
属性。但是为什么框架对象认为它们是UIView
对象,而不是SKNode
objectsThanks!那是我的问题。实际上,我是在一个实用程序/助手类中进行这些计算的,我忘了导入SpriteKit
class GameViewController : UIViewController
import UIKit
//or
import SpriteKit
class GameViewController : UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let line = Line(rect:....) //specify rect
let other = Line(rect:....) //specify rect
let dx = line.frame.midX - other.frame.midX
}
}