I am trying to add gravity and velocity to a player but can't fix this error. I've tried multiple different things but always get the same type of error. Could anyone tell what I am doing wrong and why its not possible to use multiply a CGPoint
and a CGFloat
or a CGFloat
* CGFloat
or a CGPoint
* a CGPoint
?
-
Why a `somePoint * someFloat` make sense? In simply terms of physics, does it seems correct? – Larme Aug 10 '17 at 10:22
-
@Larme its giving me the same error when I do `someFloat * someFloat` also, so what is the actual problem, why is it not possible? – Yes Aug 10 '17 at 10:24
-
@Yes In simple math, you can't multiple apple with oranges. – Desdenova Aug 10 '17 at 10:29
-
You have to do the scalar multiplication as `CGPoint` is a structure. See [this answer](https://stackoverflow.com/a/24985641/3687801) – nayem Aug 10 '17 at 10:29
-
Do not post code as image. – Amin Negm-Awad Oct 17 '17 at 04:15
5 Answers
You can use operator overloading to do this.
func *(lhs: CGPoint, rhs: CGFloat) -> CGPoint {
return CGPoint(x: lhs.x * rhs, y: lhs.y * rhs)
}

- 6,239
- 6
- 24
- 47
It is simply not possible to multiply a CGPoint with a Float. The first is a point and the second is a number. In real life, you can't multiply the coordinate where you're standing with a number. However what you can do, is to multiply each axis of the coordinate for itself and create a new point with the results.
I'm not sure what you're trying to do, but I'd simply change these lines to:
let gravityStep = CGPoint(x: 0, y: gravity * seconds)
let velocityStep = CGPoint(x: velocity.x * seconds, y: velocity.y * seconds)
This should work as expected.

- 177
- 9
-
Thanks thats very helpful, but the problem I get now is that its not possible to do `CGPoint += CGPoint`, thats the error which is appearing on lines 38 and 42 of the image above. Any ideas? – Yes Aug 10 '17 at 10:34
-
@Yes Try operator overloading for your need. See the other answer. You can have your `+` overloading by yourself. – nayem Aug 10 '17 at 10:37
-
@nayem I've tried this for `CGPoint += CGPoint` but I am now getting an error that says `member operator '+=' must have at least one argument of type 'MovementComponent'` Any ideas how to fix this? The code I've written is as follows: `func +=(lhs: CGPoint, rhs: CGPoint) -> CGPoint { return CGPoint(x: lhs.x + rhs.x, y: lhs.y + rhs.y) }` – Yes Aug 10 '17 at 11:13
-
Why everyone is so fast to suggest operator overloading? I would save this for when you actually solve the core problem, and then for extra-credit add syntax-sugar. All you're doing otherwise is adding problems to your problems. – michael salmon Aug 10 '17 at 11:27
-
@Yes that's your decision, but I wouldn't use operator overloading either... To your first comment: that's actually the same thing again, you can't add two coordinates, but what you can do is to add each coordinates x- and y-axis. That means: velocity.x += gravityStep.x and velocity.y += gravityStep.y. Again, I'm not sure what you're trying to do since I've never actually used SpriteKit, but that's the normal way if you want to add two CGPoints. – Matt Harris Aug 10 '17 at 11:31
-
Declare this extension
extension CGPoint {
static func * ( left : CGPoint , right : CGFloat) -> CGPoint {
return CGPoint(x: left.x * right, y: left.y * right)
}
static func * ( left : CGFloat , right : CGPoint) -> CGPoint {
return CGPoint(x: right.x * left, y: right.y * left)
}
}

- 5,070
- 28
- 29
Assuming you're working on iOS, suggest looking at the Core Motion apis like UIGravityBehavior, CMAcceleration, CGVector. Sorry I know this isn't directly answering your question, but hope that helps :)
This article seems to lay it out well: https://www.bignerdranch.com/blog/uidynamics-in-swift/
Generally though if you want to implement gravity directly you should be looking at vector math. And maybe using a helper lib such as https://github.com/nicklockwood/VectorMath (disclaimer: I'm not involved in this library, it's just an example)

- 398
- 4
- 12
I noticed you're going through the "How to make a game like Flappy Bird" tutorial from Ray Wenderlich which includes some helper methods in files called SKUtils. When you add the SKUtils files from Ray Wenderlich, you may have to make sure they're checked in "Target Membership". Shouldn't have to, but I believe it depends on what you've got selected when you drag them into xcode. That is what's causing the error "Binary operator (*) cannot be applied to operands CGPoint and CGFloat" in this case. The other answers here will help you understand the problem, and learn to squash the bug yourself, but I'm assuming you just want the problem fixed, so you can continue the tutorial. Same would apply for many other cases where code and files are borrowed.

- 1
- 3
-
In this question, our friend is going through the "How to make a game like Flappy Bird" tutorial from Ray Wenderlich. I had the exact same problem and therefore was able to provide the solution. – Kevin Kaye Oct 17 '17 at 02:35