Ios 以编程方式为精灵在帧内创建区域
我有一个精灵球,我只想在一个正方形区域内反弹 如何在主框架内以渐进方式创建此方形区域,而不是像代码片段中那样约束整个框架:Ios 以编程方式为精灵在帧内创建区域,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我有一个精灵球,我只想在一个正方形区域内反弹 如何在主框架内以渐进方式创建此方形区域,而不是像代码片段中那样约束整个框架: let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame) borderBody.friction = 0 self.physicsBody = borderBody 请尝试此代码段。它使用SKPhysicsBody创建一个SKNode。然后可以将精灵放置在此节点中: // Create a bound
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)
请尝试此代码段。它使用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)