pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size)
在这段代码中,我使用了rectangleOfSize
来进行碰撞物理体的处理,但如果我想使用图像本身的像素形状进行处理,我应该使用什么替代rectangleOfSize
呢?
pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size)
在这段代码中,我使用了rectangleOfSize
来进行碰撞物理体的处理,但如果我想使用图像本身的像素形状进行处理,我应该使用什么替代rectangleOfSize
呢?
您需要创建一个CGPath
,该路径应该是您对象的形状,并使用SKPhysicsBody
的init(polygonFromPath path: CGPath)
,具体操作在此处描述。
pipeUp.physicsBody = SKPhysicsBody(texture:pipeUp.texture)
extension SKPhysicsBody
{
convenience init(texture: SKTexture)
self.init(texture:texture,size:texture.size())
}
size
参数,像这样:pipeUp.physicsBody = SKPhysicsBody(texture: pipeUp.texture, size: pipeUp.size)
我相信使用 CGPath
存在/曾经存在内存泄漏问题。
/**
Creates a body from the alpha values in the supplied texture.
@param texture the texture to be interpreted
@param alphaThreshold the alpha value above which a pixel is interpreted as opaque
@param size of the generated physics body
*/
@available(iOS 8.0, *)
public /*not inherited*/ init(texture: SKTexture, alphaThreshold: Float, size: CGSize)
那么代码将会是:
pipeUp.physicsBody = SKPhysicsBody(texture: pipeUp.texture, alphaThreshold: 0.3, size: pipeUp.size)