我正在尝试制作一个简单的SpriteKit游戏。屏幕上有两个精灵。其中一个是射击激光枪。当激光穿过屏幕时,它应该被遗漏。 子弹和精灵彼此没有联系,但联系检测功能。我不知道发生了什么?我是用SkAction func从上层精灵拍摄的。所有的精灵和激光枪都有不同的UInt32类别掩码值。
感谢您的帮助。
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
}
答案 0 :(得分:1)
所以我有完全相同的问题。我不知道确切的问题是什么,但我通过将精灵的hitbox更改为圆形而不是矩形来修复它。 请尝试将spices 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))
我认为问题在于如何在精灵周围放置矩形命中框,但如果将其更改为圆形则应该有效。