2015-09-07 18 views
0

例碰撞(在屏幕上的紅色塊),預先設定的節點不與用戶觸摸的創建的節點

var img = "BlockRed.png" 
let sand = SKSpriteNode (imageNamed: img) 
var randLoc = arc4random_uniform(26) 
sand.position = CGPointMake(location.x - CGFloat(10) + CGFloat(randLoc), location.y) 
sand.name = "redSand" 
sand.zPosition = 0 
self.addChild(sand) 

//gravity 
sand.physicsBody?.affectedByGravity = true 

//contact 
sand.physicsBody!.categoryBitMask = PARTICLE 
sand.physicsBody?.collisionBitMask = BLOCK | PARTICLE 
sand.physicsBody?.contactTestBitMask = BLOCK | PARTICLE 
sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.frame.width - 3) 

但是,當它們接觸時,顆粒剛好落在壁上。爲什麼會發生這種情況,我該如何解決?

注:

// Constants 
let NOTHING: UInt32 = 0x1 << 0 
let PARTICLE: UInt32 = 0x1 << 1 
let BLOCK: UInt32  = 0x1 << 2 

回答

0

預設節點的物理主體不是基於體積的物體,因此不會與沙粒碰撞。我從切換:

preWall.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: x, y: y, width: width, height: height)) 

preWall.physicsBody = SKPhysicsBody(rectangleOfSize: preWall.frame.size) 
1

您初始化沙子顆粒的physics body您設置的bit masks後。嘗試改變線條的順序。

sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.frame.width - 3) 
sand.physicsBody?.categoryBitMask = PARTICLE 
sand.physicsBody?.collisionBitMask = BLOCK | PARTICLE 
sand.physicsBody?.contactTestBitMask = BLOCK | PARTICLE 
+0

這沒有奏效。還有其他建議嗎? –

+0

你能告訴我你當前的代碼嗎? – rakeshbs

+0

我想通了,我發佈了一個答案。謝謝你的幫助! –