2016-07-18 139 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)