Basic Syntax Problem but, I can't solve it
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)
}
}
})
}
Error on line foreground.position += moveAmount
and
foreground.position += CGPoint(x: foreground.size.width * CGFloat(self.numberOfForegrounds), y: 0)
Everybody say use this code :
public func + (A: CGPoint, B: CGPoint) -> CGPoint {
return CGPoint(x: A.x + B.x, y: A.y + B.y)
}
OR;
something like this :
A.position.x += b.position.x
A.position.y += b.position.y
Please help ...
The +
operator is not defined for CGPoint
, so you cannot use that or +=
. You already found the right function you have to define, just make sure you actually use it. You can actually go one step further and also define the +=
operator for 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)
}
}
})
}