我花了几个星期的时间尝试使用BitMasking完全掌握Sprite-Kit中的碰撞检测。我在youtube上搜索了一下,什么也没找到。我将发布我现在拥有的代码。当我的两个SKSpriteNodes相互联系时,我现在拥有的代码不能正常工作。谁能告诉我如何修复我的代码,并告诉我步骤和他们的意思。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate{
let ballCategory:UInt32 = 0x1 << 0
let boxCategory:UInt32 = 0x1 << 1
let ball = SKSpriteNode(imageNamed: "redball")
let redBall = SKSpriteNode(imageNamed: "redball")
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
let box = SKSpriteNode(imageNamed: "box")
box.position = CGPoint(x: 400, y: 400)
box.physicsBody?.categoryBitMask = boxCategory
box.physicsBody?.contactTestBitMask = boxCategory
box.physicsBody?.collisionBitMask = boxCategory | ballCategory
addChild(box)
redBall.position = CGPoint(x: 200, y: 200)
redBall.physicsBody?.categoryBitMask = ballCategory
redBall.physicsBody?.contactTestBitMask = ballCategory
redBall.physicsBody?.collisionBitMask = ballCategory | boxCategory
addChild(redBall)
//Backround Properties
backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation
}
func didBeginContact(contact: SKPhysicsContact) {
let contactBodyA = SKSpriteNode()
let contactBodyB = SKSpriteNode()
if(contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == boxCategory){
println("contact was made")
}
}
}发布于 2015-09-19 01:47:35
我已经修改了你的一些代码,现在它工作得很好。下面是你的代码:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate{
let ballCategory:UInt32 = 0x1 << 0
let boxCategory:UInt32 = 0x1 << 1
let ball = SKSpriteNode(imageNamed: "redball")
let redBall = SKSpriteNode(imageNamed: "redball")
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
//make gravity 0
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
let box = SKSpriteNode(imageNamed: "redball")
box.position = CGPoint(x: 400, y: 400)
//add physicsbody for collisin detection
box.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: box.size)
//assign a correct cetogary
box.physicsBody?.categoryBitMask = boxCategory
box.physicsBody?.collisionBitMask = ballCategory
box.physicsBody?.contactTestBitMask = ballCategory
addChild(box)
redBall.position = CGPoint(x: 200, y: 200)
//add physics body
redBall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: redBall.size)
//assign correct cetogary
redBall.physicsBody?.categoryBitMask = ballCategory
redBall.physicsBody?.contactTestBitMask = boxCategory
redBall.physicsBody?.collisionBitMask = boxCategory
addChild(redBall)
//Backround Properties
backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation
}
func didBeginContact(contact: SKPhysicsContact) {
// 1. Create local variables for two physics bodies
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
// 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// 3. react to the contact between ball and bottom
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == boxCategory {
//TODO: Replace the log statement with display of Game Over Scene
//add your code here.
print("Called")
}
}
}使用xCode 7进行了测试。
HERE是关于spriteKit的很好的教程,你可以从中学到更多。
https://stackoverflow.com/questions/32656926
复制相似问题