在 Swift Xcode 中设置碰撞
Setting up collisions in Swift Xcode
我在 swift 中设置碰撞的过程中遇到了这个我似乎无法摆脱的错误。我可以更改一行代码来修复该错误,但是当我这样做时,会出现更多错误。
let collisionHitBox = CGRect(x: 201, y: 139, width: 398, height: 212)
barCollisions.physicsBody? = SKPhysicsBody(edgeLoopFromRect: collisionHitBox)
let ballCategory: UInt32 = 0x1 << 0
let barCategory: UInt32 = 0x1 << 1
ball.physicsBody?.categoryBitMask = ballCategory
ball.physicsBody?.usesPreciseCollisionDetection = true
ball.physicsBody?.collisionBitMask = ballCategory | barCategory
ball.physicsBody?.contactTestBitMask = ballCategory | barCategory
barCollisions.physicsBody?.categoryBitMask = barCategory
barCollisions.physicsBody?.usesPreciseCollisionDetection = true
func didBeginContact(contact: SKPhysicsContact) {
let firstNode = contact.bodyA.node as! SKSpriteNode
// This is where I get an error saying "Initialization of immutable value was never used, consider replacing it for removing it"
let secondNode = contact.bodyB.node as! SKSpriteNode
if (contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == barCategory)
{
let contactPoint = contact.contactPoint
let contact_y = contactPoint.y
let target_y = secondNode.position.y
let margin = secondNode.frame.size.height/2 - 25
if (contact_y > (target_y - margin)) && contact_y < (target_y + margin) {
print("GameOver")
}
}
就是说你设置了firstNode但是你没有使用它,所以你可以去掉这一行
我在 swift 中设置碰撞的过程中遇到了这个我似乎无法摆脱的错误。我可以更改一行代码来修复该错误,但是当我这样做时,会出现更多错误。
let collisionHitBox = CGRect(x: 201, y: 139, width: 398, height: 212)
barCollisions.physicsBody? = SKPhysicsBody(edgeLoopFromRect: collisionHitBox)
let ballCategory: UInt32 = 0x1 << 0
let barCategory: UInt32 = 0x1 << 1
ball.physicsBody?.categoryBitMask = ballCategory
ball.physicsBody?.usesPreciseCollisionDetection = true
ball.physicsBody?.collisionBitMask = ballCategory | barCategory
ball.physicsBody?.contactTestBitMask = ballCategory | barCategory
barCollisions.physicsBody?.categoryBitMask = barCategory
barCollisions.physicsBody?.usesPreciseCollisionDetection = true
func didBeginContact(contact: SKPhysicsContact) {
let firstNode = contact.bodyA.node as! SKSpriteNode
// This is where I get an error saying "Initialization of immutable value was never used, consider replacing it for removing it"
let secondNode = contact.bodyB.node as! SKSpriteNode
if (contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == barCategory)
{
let contactPoint = contact.contactPoint
let contact_y = contactPoint.y
let target_y = secondNode.position.y
let margin = secondNode.frame.size.height/2 - 25
if (contact_y > (target_y - margin)) && contact_y < (target_y + margin) {
print("GameOver")
}
}
就是说你设置了firstNode但是你没有使用它,所以你可以去掉这一行