【发布时间】:2018-12-02 12:31:25
【问题描述】:
我的目标是输出 "hit",但不改变card 和anotherCard 的位置。他们应该互相接触,但不能移动。但是没有调用 didBegin。
结构:
struct physicBodyCharacters {
static let cardNumber = 00000001 //1
static let anotherCardNumber = 00000010 //2
static let nobodyNumber = 00000100 //4
}
在 viewDidLoad() 中:
gameScene2.physicsWorld.gravity = CGVector(dx: 0, dy: -9.81)
gameScene2.physicsWorld.contactDelegate = self
第一个节点:
card = SKSpriteNode(texture: cardTexture)
card.position = CGPoint(x: gameScene2.size.width / 2 + 150, y: 95)
card.zPosition = 3
card.setScale(1)
card.physicsBody = SKPhysicsBody(texture: cardTexture, size: card.size)
card.physicsBody?.affectedByGravity = false
card.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.cardNumber)
card.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
card.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.anotherCardNumber)
第二个节点:
anotherCard = SKSpriteNode(texture: anotherCardTexture)
anotherCard.position = CGPoint(x: 31 , y: 532)
anotherCard.zPosition = 2
anotherCard.setScale(1)
anotherCard.physicsBody = SKPhysicsBody(texture: anotherCardTexture, size: battlefieldCard0.size)
anotherCard.physicsBody?.affectedByGravity = false
anotherCard.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.anotherCardNumber)
anotherCard.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
anotherCard.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.cardNumber)
didBegin() 函数:
func didBegin(_ contact: SKPhysicsContact) {
print("contact")
let contanctMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contanctMask
{
case UInt32(physicBodyCharacters.cardNumber) | UInt32(physicBodyCharacters.anotherCardNumber):
print("hit")
default:
break
}
}
对于每一个回答我都非常感谢。
【问题讨论】:
-
您没有显示所有相关的代码行。
-
假设您在 SKScene 中声明了此方法,“请确保在需要检测联系人之前在某处写入“self.physicsWorld.contactDelegate = self”,否则不会调用您的联系人方法。
-
self.physicsWorld.contactDelegate = self已经在我的代码中。问题一定出在其他地方。
标签: swift sprite-kit