2016-11-28 18 views
0

我在写游戏。它没有检测到任何碰撞,即使我可以看到物理机构,因为我已经打开了该视图。在子类节点中未检测到碰撞

要建立物理世界中的游戏场景,我编写的类声明之上以下几点:

struct PhysicsCategory { 
    static let None: UInt32 = 0 
    static let Chicken: UInt32 = 0b1 
    static let Edge: UInt32 = 0b10 
} 

然后,实际的类的场景中:

override func didMove(to view: SKView) { 
    setupNodes() 
    setupTrial() 

    let playableRect = CGRect(x: 0, y: 0, width: size.width/2, height: size.height/2) 
    self.physicsBody = SKPhysicsBody(edgeLoopFrom: playableRect) 
    self.physicsWorld.contactDelegate = self 
    self.physicsBody!.categoryBitMask = PhysicsCategory.Edge 
    self.physicsBody!.contactTestBitMask = PhysicsCategory.Chicken 

    // This is important for handling all the custom events 
    enumerateChildNodes(withName: "//*", using: { node, _ in 
     // we need to limit this to chickens only 
     if let customNode = node as? CustomNodeEvents { 
      customNode.didMoveToScene() 
     } 
    }) 
} 

这里是检测碰撞代码:

func didBegin(_ contact: SKPhysicsContact) { 
    print("something happened") 
    let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask 

    if collision == PhysicsCategory.Chicken | PhysicsCategory.Edge { 
     print("it works!") 
    } 
} 

我想动画的节点是鸡。我希望游戏能够检测到它们与上面的世界的边缘相碰撞。

我的鸡子是这样的:

class TargetNode: SKSpriteNode, CustomNodeEvents, InteractiveNode { 

func didMoveToScene() { 
    isUserInteractionEnabled = true 

    anchorPoint = CGPoint(x: 0, y: 0) 
    let playableRect = CGRect(x: self.anchorPoint.x, y: self.anchorPoint.y, width: self.size.width, height: self.size.height) 

    physicsBody = SKPhysicsBody(edgeLoopFrom: playableRect) 
    physicsBody!.isDynamic = true 
    physicsBody!.categoryBitMask = PhysicsCategory.Chicken 
    physicsBody!.contactTestBitMask = PhysicsCategory.Edge | PhysicsCategory.Chicken 
    physicsBody!.velocity = CGVector(dx: 100, dy: 0) 
} 
} 

编辑:节点正在使用游戏中的场景文件这种方法添加到场景。

func generateItems(targetNumber: Int, target: Bool) { 
    let movingItems = true 
    for _ in 0...(targetNumber - 1) { 
     if (target) { 
      let name = createTarget() 
      let targetNode = TargetNode(imageNamed: name) 
      targetNode.name = name 
      fgNode.addChild(targetNode) 
      targetNode.position = generateRandomLocation() 
      //if movingItems { animateTargets(targetNode) } 
     } 
} 
+0

你在哪里添加节点到现场接触? –

+0

我正在使用上面编辑中添加的功能生成一堆节点。 – illuminatedtype

+0

它看起来像是认为边缘和小鸡都有由edgeLoops构建的physicsBody。我不认为这些会碰撞。 –

回答

1

将您的鸡肉设置为SKPhysicsBody(edgeLoopFrom :_)是个坏主意。而是尝试使用SKPhysicsBody(rectangleOf :_)在您的鸡周围绘制矩形形状,您要绘制的形状类型可能会有所不同,请查看文档以获取更多信息。

而且检查的天气鸡取得了联系与playableRect

删除self.physicsBody!.contactTestBitMask = PhysicsCategory.Chicken,因为我们不想检查,如果RECT已与我们想知道,如果鸡已经取得了联系与鸡接触可玩的Rect。所以请保留contactTestBitMask

删除:| PhysicsCategory.Chicken从你的鸡亚类,除非你想检查天气鸡也相互碰撞。

最后:检查是否有冲突,如果鸡使得与playableRect

func didBegin(_ contact: SKPhysicsContact) { 

    if contact.bodyA.categoryBitMask == PhysicsCategory.Chicken && contact.bodyB.categoryBitMask == PhysicsCategory.Edge { 
     print("Chickens have collided with edge") 
    } 

    } 
+0

谢谢。出于好奇,为什么改变SKPhysicsBody形状会改变什么? – illuminatedtype

+1

那是因为基于边缘的物理实体不能与其他基于边的物体进行交互。我可以竖起大拇指吗? :) –