2016-07-18 136 views
3
pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size) 

在这个编码我用rectangleOfSize碰撞物理身体,但如果我想通过像素的图像只是形状用,有什么我应该使用的rectangleOfSizeSpriteKit PhysicsBody非矩形碰撞

+0

做笔记,那didBeginContact会触发多次由于它们是多个联系点,因此实际上,您必须在代码 – Knight0fDragon

回答

1

你应该做一个CGPath这是你的物体的形状,并使用SKPhysicsBodyinit(polygonFromPath path: CGPath)described here

0

可基于纹理创建一个物理体:

pipeUp.physicsBody = SKPhysicsBody(texture:pipeUp.texture) 

它将创建一个alpha遮罩纹理,因此任何具有alpha 0的像素都不会被包含在物理体中。

编辑:

@classenApps让我意识到我犯了一个扩展做到这一点,所以我会添加它的答案是正确的。

extension SKPhysicsBody 
{ 
    convenience init(texture: SKTexture) 
    self.init(texture:texture,size:texture.size()) 
} 
0

我会使用KnightOfDragon的方法与添加size的:参数是这样的:

pipeUp.physicsBody = SKPhysicsBody(texture: pipeUp.texture, size: pipeUp.size) 

我相信是有/曾经是使用CGPath内存泄漏。

+0

中处理此问题,您的方式是基于AppleDocs执行此操作的方式,我有一个扩展名,它没有大小 – Knight0fDragon

0

如果您需要明确地告诉什么alphaThreshold使用纹理,另一种解决方案可以是:

/** 
    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)