如何在两个SpriteKit对象之间创build一个分隔符

我有一个圆形和三angular形内外。 我正在看我的angular色,如果它不是在触摸三angular形的圆圈之外的圆圈,反之亦然。

如果我的angular色出现在我的圈子里,那也没有关系。 事实上,如果我在我的圈子和三angular形外面,所以我的angular色不应该触摸他们。 我如何解决这个问题?

有没有人有一个想法?

我的代码:

func AddCharacter() { BooCharacter.size = CGSize(width: 30, height: 30) BooCharacter.anchorPoint.y = 0 BooCharacter.zRotation = CGFloat(-M_PI_2) //BooCharacter.position.y += circleRadius BooCharacter.position = CGPoint(x:0.0, y:circleRadius) BooCharacter.physicsBody = SKPhysicsBody(texture:BooCharacterSKT, size: CGSize(width: 30, height: 30)) BooCharacter.physicsBody = SKPhysicsBody(circleOfRadius: 15); BooCharacter.physicsBody?.categoryBitMask = heroCategory BooCharacter.physicsBody?.contactTestBitMask = triangleCategory BooCharacter.physicsBody?.collisionBitMask = triangleCategory; } func AddCircle() { Circle = SKShapeNode(circleOfRadius: circleRadius) Circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2) Circle.strokeColor = UIColor.whiteColor() self.addChild(Circle) Circle.addChild(BooCharacter) self.AddTriangleToCircle(Circle, Location: CGFloat(random(1...90)), Inside: true) self.AddTriangleToCircle(Circle, Location: CGFloat(random(90...280)), Inside: false) self.AddTriangleToCircle(Circle, Location: CGFloat(random(280...360)), Inside: false) //self.AddTriangleToCircle(Circle, Location: 90) //self.AddTriangleToCircle(Circle, Location: 180) //self.AddTriangleToCircle(Circle, Location: 240) //self.AddTriangleToCircle(Circle, Location: 300) } func AddPointsLable() { pointsLabel = PointsLabel(num: 0) pointsLabel.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 35) pointsLabel.name = "pointsLabel" addChild(pointsLabel) } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { BooCharacter.zRotation += CGFloat(M_PI) //for touch in touches { //let location = touch.locationInNode(self) let TapLable = childNodeWithName("tap") TapLable?.removeFromParent() //} } override func update(currentTime: NSTimeInterval) { angleRelatedToCircle -= rotationSpeed //BooCharacter.zRotation -= rotationSpeed let newLocation = CGPointMake(circleRadius * cos(angleRelatedToCircle), circleRadius * sin(angleRelatedToCircle)); // BooCharacter.position.x = circleRadius * cos(angleRelatedToCircle) // BooCharacter.position.y = circleRadius * sin(angleRelatedToCircle) BooCharacter.runAction(SKAction.rotateByAngle(-rotationSpeed, duration: 0)); BooCharacter.runAction(SKAction.moveTo(newLocation, duration: 0)); //SKAction.moveTo(CGP, duration: <#T##NSTimeInterval#>) //NSLog("x = %f, y = %f, r = %f",BooCharacter.position.x,BooCharacter.position.y,BooCharacter.zRotation); } func AddTriangleToCircle(Circle: SKShapeNode, Location: CGFloat, Inside: Bool) { let Triangle: SKSpriteNode = SKSpriteNode(imageNamed: "Triangle") Triangle.size = CGSize(width: 30, height: 30) Triangle.anchorPoint.y = 0 if Inside == true { // Inside Triangle Triangle.zRotation = CGFloat(M_PI_2) } else { // Outside Triangle Triangle.zRotation = CGFloat(-M_PI_2) } Triangle.position = CGPoint(x:0.0, y:circleRadius) let rotationSpeed1 = rotationSpeed + Location; var angleRelatedToCircle1 = angleRelatedToCircle; angleRelatedToCircle1 -= rotationSpeed1 Triangle.zRotation -= rotationSpeed1 Triangle.position.x = circleRadius * cos(angleRelatedToCircle1) Triangle.position.y = circleRadius * sin(angleRelatedToCircle1) //Triangle.name = "Triangle"; Triangle.physicsBody = SKPhysicsBody(texture:TriangelSKT, size: CGSize(width: 30, height: 30)) //TODO: Make this a polygon body Triangle.physicsBody?.categoryBitMask = triangleCategory Triangle.physicsBody?.contactTestBitMask = heroCategory Triangle.physicsBody?.collisionBitMask = heroCategory Circle.addChild(Triangle); } func didBeginContact(contact: SKPhysicsContact) { // --------------------------------------------- // Hero Hit Triangle // --------------------------------------------- if (contact.bodyA.categoryBitMask == triangleCategory) { // Play Sound Effect // Remove Triangle //contact.bodyA.node?.removeFromParent(); // Update Score NSLog("Hero hit Triangle"); } } func random(range: Range<Int> ) -> Int { var offset = 0 if range.startIndex < 0 // allow negative ranges { offset = abs(range.startIndex) } let mini = UInt32(range.startIndex + offset) let maxi = UInt32(range.endIndex + offset) return Int(mini + arc4random_uniform(maxi - mini)) - offset } } 

在这里输入图像说明

你可能想要的就是像这样设置angular色的物理体:

 BooCharacter.physicsBody = SKPhysicsBody(circleOfRadius: 15); 

因为目前字符大小是一个30×30的矩形,它的主体是一个直径为60(d = 2r)的圆。 你需要一个直径为30的身体。

此外,你正在改变人物的定位点,但要记住, 物理人体不会受到这个动作的影响 。 它保持以节点的位置为中心…阅读更多在这个例子 。

而关于三angular形节点。 目前它有一个矩形的物理体,即使它是一个三angular形。 不知道你是否想要,但可能会导致你遇到的问题。 你有几个select来解决这个问题:

  1. 手动创build物理体,就像之前提到的一个问题 ,或者

  2. 从纹理创build一个物理体 。 请记住,这可能是性能密集型的,但对于您的游戏来说可能会很好,因为您在场景中没有多less物体。