Ball bouncing off the wall doesn't work

偶尔善良 提交于 2019-12-08 03:36:49

问题


I am trying to let the ball bounce back when it is about to go off the screen.

I thought it should work with this:

 bal.physicsBody?.velocity.dx = -bal.physicsBody?.velocity.dx

but it doesn't..

I am getting this error: Could not find an overload for "-" that accepts the suplied arguments.

How to solve this?


回答1:


You could do either:

if let physicsBody = bal.physicsBody {
    physicsBody.velocity.dx *= -1
}

Or

bal.physicsBody?.velocity.dx *= -1

Or, if you're absolutely certain bal has a physics body you could force-unwrap, with either of the following methods:

bal.physicsBody!.velocity.dx *= -1
bal.physicsBody!.velocity.dx = -bal.physicsBody!.velocity.dx


来源:https://stackoverflow.com/questions/30278390/ball-bouncing-off-the-wall-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!