The SKSpriteNode anchor point is set to (0.5, 0).
How can you create a physics body with SKPhysicsBody(texture: size:)
centered at (0.5,0.5), without changing the (0.5, 0) anchor point.
The SKSpriteNode anchor point is set to (0.5, 0).
How can you create a physics body with SKPhysicsBody(texture: size:)
centered at (0.5,0.5), without changing the (0.5, 0) anchor point.
You can use + bodyWithRectangleOfSize:center: or + bodyWithCircleOfRadius:center:, like this:
let sprite = SKSpriteNode(color: SKColor.whiteColor(), size: CGSize(width: 100.0, height: 100.0))
sprite.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
sprite.anchorPoint = CGPoint(x: 0.5, y: 0.0)
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size, center: CGPoint(x: 0, y: sprite.size.height/2.0))
sprite.physicsBody?.affectedByGravity = false // just added for easier debugging
addChild(sprite)
Center parameter represents the origin of the square/circle in the owning node’s coordinate system.