二元运算符“+=”不能应用于两个“CGPoint”操作数
Binary operator ‘+=’ cannot be applied to two ‘CGPoint’ operands
基本语法问题但是,我无法解决
func updateForeground(){
worldNode.enumerateChildNodes(withName: "foreground", using: { node, stop in
if let foreground = node as? SKSpriteNode{
let moveAmount = CGPoint(x: -self.groundSpeed * CGFloat(self.deltaTime), y: 0)
foreground.position += moveAmount
if foreground.position.x < -foreground.size.width{
foreground.position += CGPoint(x: foreground.size.width * CGFloat(self.numberOfForegrounds), y: 0)
}
}
})
}
第 foreground.position += moveAmount
和
行出错
foreground.position += CGPoint(x: foreground.size.width * CGFloat(self.numberOfForegrounds), y: 0)
每个人都说使用这个代码:
public func + (A: CGPoint, B: CGPoint) -> CGPoint {
return CGPoint(x: A.x + B.x, y: A.y + B.y)
}
或;
像这样:
A.position.x += b.position.x
A.position.y += b.position.y
请帮忙...
未为 CGPoint
定义 +
运算符,因此您不能使用它或 +=
。您已经找到了必须定义的正确功能,只需确保您实际使用它即可。您实际上可以更进一步,还可以为 CGPoint
.
定义 +=
运算符
extension CGPoint {
public static func +(lhs:CGPoint,rhs:CGPoint) -> CGPoint {
return CGPoint(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
}
public static func +=(lhs:inout CGPoint, rhs:CGPoint) {
lhs = lhs + rhs
}
}
func updateForeground(){
worldNode.enumerateChildNodes(withName: "foreground", using: { node, stop in
if let foreground = node as? SKSpriteNode {
let moveAmount = CGPoint(x: -self.groundSpeed * CGFloat(self.deltaTime), y: 0)
foreground.position += moveAmount
if foreground.position.x < -foreground.size.width {
foreground.position += CGPoint(x: foreground.size.width * CGFloat(self.numberOfForegrounds), y: 0)
}
}
})
}
您想使用“+=” 运算符。
为此:
func updateForeground() {
worldNode.enumerateChildNodes(withName: "foreground", using: { node, stop in
if let foreground = node as? SKSpriteNode{
let moveAmount = CGPoint(x: -self.groundSpeed * CGFloat(self.deltaTime), y: 0)
foreground.position.x += moveAmount.x
foregorund.postion.y += moveAmount.y
if foreground.position.x < -foreground.size.width {
foreground.position.x += foreground.size.width * CGFloat(self.numberOfForegrounds)
}
}
})
基本语法问题但是,我无法解决
func updateForeground(){
worldNode.enumerateChildNodes(withName: "foreground", using: { node, stop in
if let foreground = node as? SKSpriteNode{
let moveAmount = CGPoint(x: -self.groundSpeed * CGFloat(self.deltaTime), y: 0)
foreground.position += moveAmount
if foreground.position.x < -foreground.size.width{
foreground.position += CGPoint(x: foreground.size.width * CGFloat(self.numberOfForegrounds), y: 0)
}
}
})
}
第 foreground.position += moveAmount
和
foreground.position += CGPoint(x: foreground.size.width * CGFloat(self.numberOfForegrounds), y: 0)
每个人都说使用这个代码:
public func + (A: CGPoint, B: CGPoint) -> CGPoint {
return CGPoint(x: A.x + B.x, y: A.y + B.y)
}
或;
像这样:
A.position.x += b.position.x
A.position.y += b.position.y
请帮忙...
未为 CGPoint
定义 +
运算符,因此您不能使用它或 +=
。您已经找到了必须定义的正确功能,只需确保您实际使用它即可。您实际上可以更进一步,还可以为 CGPoint
.
+=
运算符
extension CGPoint {
public static func +(lhs:CGPoint,rhs:CGPoint) -> CGPoint {
return CGPoint(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
}
public static func +=(lhs:inout CGPoint, rhs:CGPoint) {
lhs = lhs + rhs
}
}
func updateForeground(){
worldNode.enumerateChildNodes(withName: "foreground", using: { node, stop in
if let foreground = node as? SKSpriteNode {
let moveAmount = CGPoint(x: -self.groundSpeed * CGFloat(self.deltaTime), y: 0)
foreground.position += moveAmount
if foreground.position.x < -foreground.size.width {
foreground.position += CGPoint(x: foreground.size.width * CGFloat(self.numberOfForegrounds), y: 0)
}
}
})
}
您想使用“+=” 运算符。
为此:
func updateForeground() {
worldNode.enumerateChildNodes(withName: "foreground", using: { node, stop in
if let foreground = node as? SKSpriteNode{
let moveAmount = CGPoint(x: -self.groundSpeed * CGFloat(self.deltaTime), y: 0)
foreground.position.x += moveAmount.x
foregorund.postion.y += moveAmount.y
if foreground.position.x < -foreground.size.width {
foreground.position.x += foreground.size.width * CGFloat(self.numberOfForegrounds)
}
}
})