我在新游戏中一直致力于接触/碰撞。我刚刚对此进行了编码,但它无法正常工作。这是我的代码:
方形细节:
square.position = CGPointMake(self.size.width/2, self.size.height/1.5)
square.zPosition = 35
square.size = CGSize(width: 40, height: 40)
square.physicsBody = SKPhysicsBody(rectangleOfSize: square.size)
square.physicsBody?.affectedByGravity = false
square.physicsBody?.categoryBitMask = squareGroup
square.physicsBody?.collisionBitMask = obstacleGroup
square.physicsBody?.contactTestBitMask = obstacleGroup
self.addChild(square)
障碍细节(障碍1和障碍2):
obstacle1.physicsBody = SKPhysicsBody(rectangleOfSize: obstacle1.size)
obstacle1.physicsBody?.affectedByGravity = false
obstacle1.physicsBody?.dynamic = true
obstacle1.physicsBody?.mass = 10000
obstacle1.physicsBody?.categoryBitMask = obstacleGroup
obstacle1.physicsBody?.collisionBitMask = squareGroup
obstacle1.physicsBody?.contactTestBitMask = squareGroup
以下是我们与类别组之间的联系人代码:
var squareGroup : UInt32 = 0x1 << 0
var obstacleGroup : UInt32 = 0x1 << 1
func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
firstBody = contact.bodyA
}
if firstBody.categoryBitMask == 1 && secondBody.categoryBitMask == 2{
let newScene = GameScene(size: self.size)
_ = SKTransition.fadeWithDuration(1)
self.view?.presentScene(newScene)
}
}
如果有人能帮助我,那就太好了。在评论中提问。
答案 0 :(得分:0)
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
firstBody = contact.bodyA
}
在else语句中,您有firstBody = contact.bodyB
和firstBody = contact.bodyA
我想你想写
secondBody = contact.bodyA
同时为联系人添加双重检查
if (firstBody.categoryBitMask == 0x1 << 1 && secondBody.categoryBitMask == 2) || (firstBody.categoryBitMask == 0x1 << 2 && secondBody.categoryBitMask == 1){
//enter code here
}