未检测到 SpriteKit 碰撞
Posted
技术标签:
【中文标题】未检测到 SpriteKit 碰撞【英文标题】:SpriteKit collisions not getting detected 【发布时间】:2015-06-23 13:59:21 【问题描述】:好的,我已经与这段代码斗争了几天了。我无法弄清楚为什么没有检测到碰撞。
我正在尝试沿着一个逆时针旋转的圆画一根弦,我想要一个对撞机圆顺时针绕弦。
这是我的代码:
class GameScene : SKScene, SKPhysicsContactDelegate
var circleString:String = "some string to draw in a circle"
private struct ColliderType
static let Symbol: UInt32 = 0x1 << 0
static let OrbitingBall: UInt32 = 0x1 << 1
override func didMoveToView(view: SKView)
self.physicsWorld.contactDelegate = self
createSpinningText()
func createSpinningText()
//circleNode serves as a container node to simplify placing and rotating the symbolNodes
let circleNode = SKShapeNode(circleOfRadius: self.size.width * 0.40)
circleNode.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5)
circleNode.strokeColor = UIColor.purpleColor()
self.addChild(circleNode)
//count elements and calculate rotation per character
let l = countElements(symbolString)
let charRotation = -(2.0 * M_PI) / Double(l)
for x in 0..<l
let character = circleString.substringWithRange(Range<String.Index>(start: advance(circleString.startIndex, x), end: advance(circleString.startIndex, x + 1)))
//charSprite is a container for charNode, to rotate the starting
//position of each character. (because I can't set a frame or anchorPoint
//on a SKLabelNode)
let charSprite = SKSpriteNode(color: UIColor.clearColor(), size: circleNode.frame.size)
let charNode = SKLabelNode(fontNamed: "Arial")
charNode.text = character
charNode.fontSize = 25
charNode.position = CGPoint(x: 0, y: circleNode.frame.size.width / 2.27)
charNode.fontColor = UIColor.purpleColor()
charNode.physicsBody = SKPhysicsBody(rectangleOfSize: charNode.frame.size)
charNode.physicsBody?.dynamic = false
charNode.physicsBody?.categoryBitMask = ColliderType.Symbol
charNode.physicsBody?.contactTestBitMask = ColliderType.OrbitingBall | ColliderType.Symbol
charNode.physicsBody?.usesPreciseCollisionDetection = true
charNode.physicsBody?.collisionBitMask = ColliderType.OrbitingBall | ColliderType.Symbol
let rotate = SKAction.rotateByAngle(CGFloat(Double(x) * charRotation), duration: 0)
charSprite.runAction(rotate)
charSprite.addChild(charNode)
circleNode.addChild(charSprite)
let turnSlowlyCCW = SKAction.rotateByAngle(1, duration: 20)
let repeat = SKAction.repeatActionForever(turnSlowlyCCW)
circleNode.runAction(repeat)
let orbitingColliderContainer = SKShapeNode(circleOfRadius: self.size.width * 0.35)
orbitingColliderContainer.strokeColor = UIColor.redColor()
orbitingColliderContainer.position = CGPointZero
let xy = CGPoint(x: orbitingColliderContainer.frame.size.width * 0.5, y: orbitingColliderContainer.frame.size.height * 0.25)
orbitingColliderContainer.physicsBody = SKPhysicsBody(circleOfRadius: 70, center:xy)
orbitingColliderContainer.physicsBody?.dynamic = false
orbitingColliderContainer.physicsBody?.contactTestBitMask = ColliderType.Symbol | ColliderType.OrbitingBall
orbitingColliderContainer.physicsBody?.collisionBitMask = ColliderType.Symbol | ColliderType.OrbitingBall
orbitingColliderContainer.physicsBody?.categoryBitMask = ColliderType.OrbitingBall
orbitingColliderContainer.physicsBody?.usesPreciseCollisionDetection = true
circleNode.addChild(orbitingColliderContainer)
let turnSlowlyCW = SKAction.rotateByAngle(-2, duration: 20)
let repeat2 = SKAction.repeatActionForever(turnSlowlyCW)
orbitingColliderContainer.runAction(repeat2)
func didBeginContact(contact: SKPhysicsContact)
println("Contact between \(contact.bodyA) and \(contact.bodyB) detected")
这是应用运行时的样子(打开 .showPhysics):
一切都按预期放置和移动,但我没有收到任何 didBeginContact 调用。那么,我在这里做错了什么?
【问题讨论】:
【参考方案1】:通过设置让它工作
self.physicsWorld.gravity = CGVectorMake(0, 0)
还有……
orbitingColliderContainer.physicsBody?.dynamic = true
然后将charNodes和orbitingColliderContainer的collisionBitMask设置为0。
【讨论】:
以上是关于未检测到 SpriteKit 碰撞的主要内容,如果未能解决你的问题,请参考以下文章