0

I'm getting two errors. When I use the <= and >=, it gives me the errors:

Binary Operator '>= & <=' cannot be applied to operands of type CGFloat and Int

 override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    var speedOfTouch = 30

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if location.x < CGRectGetMidX(self.frame) {
            square.position.x <= speedOfTouch
            square.physicsBody?.applyImpulse(CGVector(dx: -40, dy: 0))



        }
        else {
            square.position.x >= speedOfTouch
            square.physicsBody?.applyImpulse(CGVector(dx: 40, dy: 0))
        }

    }
}

I need help!

Josh Schlabach
  • 401
  • 5
  • 17

3 Answers3

0

Change one line:

let speedOfTouch = CGFLoat(30)

It didn't work because you can't compare two different types as the compiler states

Kametrixom
  • 14,673
  • 7
  • 45
  • 62
0

The swift complier considers it a programmer's error to try and compare two different types.

Just change your code to:

var speedOfTouch = 30 as CGFloat
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
0

I see at least two problems.

  1. speedOfTouch has type Int because you didn't specify any other type. So in the expression square.position.x <= speedOfTouch, you're trying to compare a CGFloat and an Int.

    Swift doesn't automatically convert between numeric types, and doesn't define a <= operator that takes a CGFloat on the left and an Int on the right, so you cannot compare two values of different types.

    You can fix this by, for example, explicitly declaring speedOfTouch as CGFloat:

    var speedOfTouch: CGFloat = 30
    
  2. The other problem, which will not give you a compiler error, is that you have written square.position.x <= speedOfTouch as a statement. You are comparing two values, but you're not doing anything with the outcome of the comparison.

    In an optimized (release) build, the compiler will probably eliminate the comparison entirely. So why are you doing it?

    You probably wanted to do something with the outcome of the comparison. Maybe, for example, you wanted to do this:

    if ((location.x < CGRectGetMidX(self.frame))
        && (square.position.x <= speedOfTouch)) {
        square.physicsBody?.applyImpulse(CGVector(dx: -40, dy: 0))
    }
    
rob mayoff
  • 375,296
  • 67
  • 796
  • 848