为什么 sprite 没有碰撞却相互接触?
Why are sprites in contact with each other although they do not collide ?
我正在尝试制作一个简单的 SpriteKit 游戏。屏幕上有两个精灵。其中之一是射击激光枪。当激光穿过屏幕时,它应该被错过。 子弹和 sprite 没有相互接触但接触检测函数调用。我不明白这是怎么回事?我正在使用 SkAction 功能从上层精灵射击。所有精灵和激光枪都有不同的 UInt32 categorybitmask 值。
感谢您的帮助。
var sprite = SKSpriteNode(imageNamed: "Spaceship")
var sprite02 = SKSpriteNode(imageNamed: "Spaceship")
var actionMove = SkAction.scaleYTo(12, duration: 0.5)
sprite.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 3/4)
sprite.yScale = 0.2
sprite.xScale = 0.2
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.physicsBody!.dynamic = true
sprite.physicsBody!.affectedByGravity = false
sprite.physicsBody!.categoryBitMask = BodyType.ship02.rawValue
sprite.physicsBody!.contactTestBitMask = BodyType.laser.rawValue
sprite.physicsBody!.collisionBitMask = 0
self.addChild(sprite)
sprite02.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 1/4)
sprite02.yScale = 0.2
sprite02.xScale = 0.2
sprite02.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite02.physicsBody!.dynamic = true
sprite02.physicsBody!.affectedByGravity = false
sprite02.physicsBody!.categoryBitMask = BodyType.ship01.rawValue
sprite02.physicsBody!.contactTestBitMask = BodyType.laser.rawValue
sprite02.physicsBody!.collisionBitMask = 0
self.addChild(sprite02)
我是这样检查接触检测的
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
println(contactMask)
}
// Laser gun properties
func laserNode(positionNode: CGPoint, rotationNode: CGFloat, name: String) -> SKSpriteNode {
var laser = SKSpriteNode()
laser.color = UIColor.greenColor()
laser.position = positionNode
laser.anchorPoint = CGPoint(x: 0.0, y: 0.0)
laser.size = CGSize(width: 3, height: 50)
laser.zPosition = -1
laser.zRotation = rotationNode
laser.name = name
laser.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: laser.size.width * 2, height: laser.size.height*2))
laser.physicsBody!.dynamic = true
laser.physicsBody!.affectedByGravity = false
laser.physicsBody!.categoryBitMask = BodyType.laser.rawValue
laser.physicsBody!.contactTestBitMask = BodyType.ship01.rawValue | BodyType.ship02.rawValue | BodyType.ship03.rawValue | BodyType.ship04.rawValue
laser.physicsBody!.collisionBitMask = 0
laser.runAction(actionMove)
laserArray.append(laser)
self.addChild(laser)
return laser
}
所以我遇到了完全相同的问题。我不完全知道确切的问题是什么,但我通过将精灵的 hitbox 更改为圆形而不是矩形来修复它。
尝试将 sprites Physic Bodies 的声明更改为此。
sprite.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size)
sprite02.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size)
laser.physicsBody = SKPhysicsBody(circleOfRadius: CGSize(width: laser.size.width * 2, height: laser.size.height*2))
我认为问题在于如何将矩形碰撞框放置在 sprite 周围,但如果将其更改为圆形,它应该可以正常工作。
我正在尝试制作一个简单的 SpriteKit 游戏。屏幕上有两个精灵。其中之一是射击激光枪。当激光穿过屏幕时,它应该被错过。 子弹和 sprite 没有相互接触但接触检测函数调用。我不明白这是怎么回事?我正在使用 SkAction 功能从上层精灵射击。所有精灵和激光枪都有不同的 UInt32 categorybitmask 值。
感谢您的帮助。
var sprite = SKSpriteNode(imageNamed: "Spaceship")
var sprite02 = SKSpriteNode(imageNamed: "Spaceship")
var actionMove = SkAction.scaleYTo(12, duration: 0.5)
sprite.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 3/4)
sprite.yScale = 0.2
sprite.xScale = 0.2
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.physicsBody!.dynamic = true
sprite.physicsBody!.affectedByGravity = false
sprite.physicsBody!.categoryBitMask = BodyType.ship02.rawValue
sprite.physicsBody!.contactTestBitMask = BodyType.laser.rawValue
sprite.physicsBody!.collisionBitMask = 0
self.addChild(sprite)
sprite02.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 1/4)
sprite02.yScale = 0.2
sprite02.xScale = 0.2
sprite02.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite02.physicsBody!.dynamic = true
sprite02.physicsBody!.affectedByGravity = false
sprite02.physicsBody!.categoryBitMask = BodyType.ship01.rawValue
sprite02.physicsBody!.contactTestBitMask = BodyType.laser.rawValue
sprite02.physicsBody!.collisionBitMask = 0
self.addChild(sprite02)
我是这样检查接触检测的
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
println(contactMask)
}
// Laser gun properties
func laserNode(positionNode: CGPoint, rotationNode: CGFloat, name: String) -> SKSpriteNode {
var laser = SKSpriteNode()
laser.color = UIColor.greenColor()
laser.position = positionNode
laser.anchorPoint = CGPoint(x: 0.0, y: 0.0)
laser.size = CGSize(width: 3, height: 50)
laser.zPosition = -1
laser.zRotation = rotationNode
laser.name = name
laser.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: laser.size.width * 2, height: laser.size.height*2))
laser.physicsBody!.dynamic = true
laser.physicsBody!.affectedByGravity = false
laser.physicsBody!.categoryBitMask = BodyType.laser.rawValue
laser.physicsBody!.contactTestBitMask = BodyType.ship01.rawValue | BodyType.ship02.rawValue | BodyType.ship03.rawValue | BodyType.ship04.rawValue
laser.physicsBody!.collisionBitMask = 0
laser.runAction(actionMove)
laserArray.append(laser)
self.addChild(laser)
return laser
}
所以我遇到了完全相同的问题。我不完全知道确切的问题是什么,但我通过将精灵的 hitbox 更改为圆形而不是矩形来修复它。 尝试将 sprites Physic Bodies 的声明更改为此。
sprite.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size)
sprite02.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size)
laser.physicsBody = SKPhysicsBody(circleOfRadius: CGSize(width: laser.size.width * 2, height: laser.size.height*2))
我认为问题在于如何将矩形碰撞框放置在 sprite 周围,但如果将其更改为圆形,它应该可以正常工作。