func didBeginContact(contact: SKPhysicsContact) {
var item: SKSpriteNode
var character: SKSpriteNode
//Change this on the basis of the order of your categoryBitMask values
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
item = contact.bodyA.node as SKSpriteNode
character = contact.bodyB.node as SKSpriteNode
}
else
{
item = contact.bodyB.node as SKSpriteNode
character = contact.bodyA.node as SKSpriteNode
}
//Perform necessary operations on item and character
}
//New Class
class ItemNode: SKSpriteNode {
static var itemInstance
}
//In the Item class declare the itemNode using this class
let itemNode = ItemNode()
itemNode.itemInstance = self
1 回答
SKPhyicsBody类有一个node property,它指向它所附加的SKNode实例 .
您的碰撞检测代码可能如下所示:
EDIT: 为了访问声明节点的Item实例,您必须在节点中存储一个指向Item实例的变量 . 为此,您可以子类化SKSpriteNode并包含属性,或使用SKNode的userData属性
子类:
UserData属性: