Swift 碰撞无效。我认为我在 didBeginContact 中的代码有问题
Swift collision not working. I think something is wrong with my code in didBeginContact
我一直在开发一个游戏,每次我的测试节点与我的检查节点接触时都必须执行测试。由于这两个节点是游戏中唯一会检查碰撞的节点,因此我决定使用这种方法来启动我的测试。
问题是当 2 个节点物理交叉时,我一直无法让我的碰撞起作用。
当前代码将移动测试方块直到它到达检查方块,但是现在它们相遇时什么也没有发生。
我错过了什么吗?任何帮助将不胜感激。谢谢。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var test:SKSpriteNode!
var testTexture = SKTexture(imageNamed: "redsquare.png")
var check:SKSpriteNode!
var checkTexture = SKTexture(imageNamed: "bluesquare.png")
var moving:SKNode!
let testCategory: UInt32 = 1 << 0
let checkCategory: UInt32 = 1 << 2
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
self.physicsWorld.contactDelegate = self
moving = SKNode()
self.addChild(moving)
//create test block
test = SKSpriteNode(texture: testTexture)
test.xScale = 0.1
test.yScale = 0.1
test.position = CGPointMake(0, self.frame.height - test.size.height)
test.physicsBody = SKPhysicsBody(rectangleOfSize: test.size)
test.physicsBody?.dynamic = false
test.physicsBody?.categoryBitMask = testCategory
test.physicsBody?.collisionBitMask = checkCategory
test.physicsBody?.contactTestBitMask = checkCategory
self.addChild(test)
//creates check block
check = SKSpriteNode(texture: checkTexture)
check.xScale = 0.1
check.yScale = 0.1
check.position = CGPointMake(self.frame.width, self.frame.height - check.size.height)
check.physicsBody = SKPhysicsBody(rectangleOfSize: check.size)
check.physicsBody?.dynamic = false
check.physicsBody?.categoryBitMask = checkCategory
check.physicsBody?.collisionBitMask = testCategory
check.physicsBody?.contactTestBitMask = testCategory
self.addChild(check)
// move action
let actionMove = SKAction.moveToX(self.frame.width, duration: 1.5)
let actionMoveDone = SKAction.moveToX(0, duration: 0)
let moveForever = SKAction.repeatActionForever(SKAction.sequence([actionMove,actionMoveDone]))
test.runAction(moveForever)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
func didBeginContact(contact: SKPhysicsContact) {
// help here!
var firstBody:SKPhysicsBody
var 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 & testCategory) != 0 && (secondBody.categoryBitMask & checkCategory) != 0){
println("contact!")
}
}
}
根据 Apple 的文档,dynamic
属性 控制基于体积的物体是否受到重力、摩擦、与其他物体的碰撞以及直接施加到物体上的力或冲量的影响.
dynamic property : The default value is true. If the value is false, the physics body ignores
all forces and impulses applied to it. This property is ignored on
edge-based bodies; they are automatically static.
所以将你的 dynamic
属性 设为真。
node.physicsBody?.dynamic = true
我一直在开发一个游戏,每次我的测试节点与我的检查节点接触时都必须执行测试。由于这两个节点是游戏中唯一会检查碰撞的节点,因此我决定使用这种方法来启动我的测试。
问题是当 2 个节点物理交叉时,我一直无法让我的碰撞起作用。
当前代码将移动测试方块直到它到达检查方块,但是现在它们相遇时什么也没有发生。
我错过了什么吗?任何帮助将不胜感激。谢谢。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var test:SKSpriteNode!
var testTexture = SKTexture(imageNamed: "redsquare.png")
var check:SKSpriteNode!
var checkTexture = SKTexture(imageNamed: "bluesquare.png")
var moving:SKNode!
let testCategory: UInt32 = 1 << 0
let checkCategory: UInt32 = 1 << 2
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
self.physicsWorld.contactDelegate = self
moving = SKNode()
self.addChild(moving)
//create test block
test = SKSpriteNode(texture: testTexture)
test.xScale = 0.1
test.yScale = 0.1
test.position = CGPointMake(0, self.frame.height - test.size.height)
test.physicsBody = SKPhysicsBody(rectangleOfSize: test.size)
test.physicsBody?.dynamic = false
test.physicsBody?.categoryBitMask = testCategory
test.physicsBody?.collisionBitMask = checkCategory
test.physicsBody?.contactTestBitMask = checkCategory
self.addChild(test)
//creates check block
check = SKSpriteNode(texture: checkTexture)
check.xScale = 0.1
check.yScale = 0.1
check.position = CGPointMake(self.frame.width, self.frame.height - check.size.height)
check.physicsBody = SKPhysicsBody(rectangleOfSize: check.size)
check.physicsBody?.dynamic = false
check.physicsBody?.categoryBitMask = checkCategory
check.physicsBody?.collisionBitMask = testCategory
check.physicsBody?.contactTestBitMask = testCategory
self.addChild(check)
// move action
let actionMove = SKAction.moveToX(self.frame.width, duration: 1.5)
let actionMoveDone = SKAction.moveToX(0, duration: 0)
let moveForever = SKAction.repeatActionForever(SKAction.sequence([actionMove,actionMoveDone]))
test.runAction(moveForever)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
func didBeginContact(contact: SKPhysicsContact) {
// help here!
var firstBody:SKPhysicsBody
var 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 & testCategory) != 0 && (secondBody.categoryBitMask & checkCategory) != 0){
println("contact!")
}
}
}
根据 Apple 的文档,dynamic
属性 控制基于体积的物体是否受到重力、摩擦、与其他物体的碰撞以及直接施加到物体上的力或冲量的影响.
dynamic property : The default value is true. If the value is false, the physics body ignores all forces and impulses applied to it. This property is ignored on edge-based bodies; they are automatically static.
所以将你的 dynamic
属性 设为真。
node.physicsBody?.dynamic = true