-2

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 ...

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 1
    What's your question? Did you use the code code or the other solution? It's all about: What means "pointA+pointB"? And both solution should fix the issue. – Larme Dec 05 '18 at 09:44
  • It isn't clear what you need help with. – Duncan C Dec 05 '18 at 11:40

2 Answers2

4

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)
            }    
        }
    })
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
0

You want to use '+=' operator .

To do this:

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)
        }    
    }
})
Jonathan
  • 330
  • 2
  • 12