问题描述
在我的游戏中,当游戏开始时,一些节点已经联系但我找不到如何检测这些联系人。我只能成功检测节点移动时发生的联系,并在游戏过程中使用函数didBeginContact进行联系。
In my game, when the game begins, some nodes are already in contact but I don't find how to detect these contacts. I only succeed to detect contacts that happen when nodes are moving and getting in contact during the game using the function didBeginContact.
任何人都有想法,请问如何检测这些联系人?
Anyone has an idea please how to detect these contacts?
如果需要,这是我的didBeginContact:
Here is my didBeginContact if needed:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask == contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
contactsList.append([firstBody.node!,secondBody.node!])
}
}
推荐答案
我刚刚尝试从一开始就让两个精灵重叠,联系方式是为我检测到。下面是代码:
I've just tried to make two sprites overlap from the very beginning and contact is detected for me. Here is the code:
import SpriteKit
struct Collider {
static let SmallSquare : UInt32 = 1 << 0
static let BigSquare : UInt32 = 1 << 1
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
let smallSquare = SKSpriteNode(color: .orangeColor(), size: CGSize(width: 50, height:50))
smallSquare.zPosition = 2
smallSquare.position = CGPoint(x: frame.midX, y: frame.midY)
smallSquare.physicsBody = SKPhysicsBody(rectangleOfSize: smallSquare.size)
smallSquare.physicsBody?.affectedByGravity = false
smallSquare.physicsBody?.categoryBitMask = Collider.SmallSquare
smallSquare.physicsBody?.contactTestBitMask = Collider.BigSquare
smallSquare.physicsBody?.collisionBitMask = 0
addChild(smallSquare)
let bigSquare = SKSpriteNode(color: .purpleColor(), size: CGSize(width: 200, height: 200))
bigSquare.zPosition = 1
bigSquare.position = CGPoint(x: frame.midX, y: frame.midY)
bigSquare.physicsBody = SKPhysicsBody(rectangleOfSize: bigSquare.size)
bigSquare.physicsBody?.affectedByGravity = false
bigSquare.physicsBody?.categoryBitMask = Collider.BigSquare
bigSquare.physicsBody?.contactTestBitMask = Collider.SmallSquare
bigSquare.physicsBody?.collisionBitMask = 0
addChild(bigSquare)
}
func didBeginContact(contact: SKPhysicsContact) {
print("Contact detected")
}
}
稍后,要适当检测某些机构之间的联系,您应该这样做:
Later on, to appropriately detect contact between certain bodies, you should do something like this:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody, secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & Collider.SmallSquare) != 0 &&
(secondBody.categoryBitMask & Collider.BigSquare != 0)) {
print ("Contact detected")
}
}
但即使没有这个,第一次开始游戏时也会打印这条消息,因为物理世界会检测到接触。
But even without that, the message will be printed when game is started for the first time because contact is detected by physics world anyways.
这篇关于SpriteKit / Swift - 如何在两个节点已经联系时检查它们的联系方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!