我有一个雪碧球,我只想在一个正方形区域内弹跳。
我如何在主框架内创建这个正方形区域,而不是像代码片段中那样约束整个框架:
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
最佳答案
请尝试此代码段。它创建一个带有SKPhysicsBody的SKNode。然后可以将精灵放置在此节点中:
// Create a boundary rectangle. Adjust with your size:
let boundary = CGRect(x: 100, y: 100, width: 100, height: 100)
// Create a boundary node
let boundaryNode = SKNode()
// Create the PhysicsBody and assign it to the node
let borderBody = SKPhysicsBody(edgeLoopFromRect: boundary)
borderBody.friction = 0
boundaryNode.physicsBody = borderBody
// Add boundary node to the scene
self.addChild(boundaryNode)
// Add your Sprite to the boundaryNode
boundaryNode.addChild(yoursprite)