基本语法问题,但是我无法解决
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)
}
OR;
类似这样的东西:
A.position.x += b.position.x
A.position.y += b.position.y
请帮助...
答案 0 :(得分:2)
未为+
定义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)
}
}
})
}
答案 1 :(得分: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)
}
}
})