Sprite Kit从未检测到我的碰撞

时间:2016-03-31 17:44:18

标签: swift sprite-kit skscene

当玩家身体遇到怪物身体时,永远不会调用函数func didBeginContact(contact: SKPhysicsContact)

我的班级工具SKPhysicsContactDelegate

class GameScene: SKScene, SKPhysicsContactDelegate

属性collisionBitMask我对它进行了评论,因为我读到如果我们不这样做,节点就可以与所有节点进行竞争

我像这样添加我的播放器:

bear.position = CGPoint(x:self.frame.width/20, y:self.frame.height/5)
bear.size = CGSize(width:  self.frame.width/7, height:  self.frame.height/5)
bear.physicsBody = SKPhysicsBody(rectangleOfSize: bear.frame.size)
bear.physicsBody?.dynamic = false;
bear.physicsBody?.categoryBitMask = PhysicsCategory.Player
bear.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
//bear.physicsBody?.collisionBitMask = PhysicsCategory.Monster

我像这样添加怪物:

           let thePosition = CGPoint(x: positionX, y: (self.frame.height/7.4)*positionY)

        let monster =  SKSpriteNode(imageNamed: pattern as String)
        monster.size = CGSize(width:  self.frame.width/16, height:  self.frame.height/11)
        monster.name = name
        monster.position = thePosition


         monster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.frame.size)



        monster.physicsBody?.dynamic = false
        monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster // 3
        monster.physicsBody?.affectedByGravity = false
        monster.physicsBody?.contactTestBitMask = PhysicsCategory.Player // 4
        //platformNode.physicsBody?.collisionBitMask = PhysicsCategory.Player // 5


        addChild(monster)

my PhysicalCategory

struct PhysicsCategory {
  static let None      : UInt32 = 0
  static let All       : UInt32 = UInt32.max
  static let Player: UInt32 = 0b1      // 1
  static let Monster   : UInt32 = 0b10       // 2
  static let Projectile: UInt32 = 0b100      // 3
  static let Plateforme: UInt32 = 0b1000      // 4
}

我有多个纹理,例如当玩家步行时我称之为:

    func walkingBear() {
    bear.runAction(    SKAction.repeatActionForever(SKAction.animateWithTextures(bearCoursFrames, timePerFrame: 0.1, resize: false, restore: true)), withKey:"cours")
  }

我宣布像这样的熊队游说队伍

  var walkFrames = [SKTexture]()
    var numImages : Int = bearAnimatedAtlas.textureNames.count
    for var i=1; i<=numImages/2; i++ {
        let bearTextureName = "cours\(i)"
        walkFrames.append(bearAnimatedAtlas.textureNamed(bearTextureName))
    }

    bearCoursFrames = walkFrames

请问我的错误在哪里,我在其他帖子中找不到解决方案 任何帮助将不胜感激

--------------------------------- EDIT ------------- ----------------- 解决方案:我只需添加self.physicsWorld.contactDelegate = self,它现在可以正常工作。

2 个答案:

答案 0 :(得分:2)

答案很简单。有一条规则规定,为了注册联系人,必须至少将一个主体定义为动态。

将玩家或其他身体更改为动态,并检测联系人。

另外,如果你已经没有了,那就启用物理视觉表示......它可能会有所帮助,因为有时候没有检测到接触,因为实际的物体彼此不相交,例如。当节点的锚点发生变化时,纹理和正文都是错位的(可以使用SKPhysicsBody类的相应初始化器来修复,但这是一个非常常见的错误)。

答案 1 :(得分:1)

我只是添加

       self.physicsWorld.contactDelegate = self 

现在有效。