Chapter 2 defining boundaries (pg71)

Hey people,

When i define boundaries to bounce my zombie off i get an error stating I ‘cannot assign value of type ‘CGfloat’ to type ‘CGPoint’’ .

as far as i can tell the zombie.position value is CGFloat and we defined topRight and bottomLeft as CGPoints.

What am i missing here ?

thanks. My code is below


func boundsCheckZombie() {
    let bottomLeft = CGPoint.zero
    let topRight = CGPoint (x: size.width, y:size.height)
    
    if zombie.position.x <= bottomLeft.x {
        zombie.position = bottomLeft.x
        velocity.x = -velocity.x
    }
    if zombie.position.x >= topRight.x {
        zombie.position = topRight.x
        velocity.x = -velocity.x
    }
    if zombie.position.y <= bottomLeft.x {
        zombie.position = bottomLeft.y
        velocity.y = -velocity.y
    }
    if zombie.position.x >= topRight.x {
        zombie.position = topRight.y
        velocity.y = -velocity.y
    }

}


zombie.position is a CGPoint - it’s a position on the screen, hence it has x and y properties, which a CGFloat wouldn’t have. x and y are CGFloats though.

You can do this:
zombie.position.x = bottomLeft.x // assigns CGFloat to CGFloat
or this:
zombie.position = bottomLeft // assigns CGPoint to CGPoint
but not this, as in your code:
zombie.position = bottomLeft.x // assigns CGFloat to CGPoint

Thanks guys i figured it out. it was a typo:unamused:

The ‘‘zombie.position = bottomLeft.x’’ needed to be ‘‘zombie.position.x= bottomLeft.x’’

that “.x” after “zombie.position” makes it a CGPoint value. otherwise it it is a CGFloat value giving the error

thanks @narrativium. I think i figured it out just as you typed up your answer !! haha timing :slight_smile:

Thanks for the extra explaination on CGFloat and CGPoint. very helpful.