swift - Sprite Kit 从未检测到我的碰撞

标签 swift sprite-kit skscene

当玩家 body 遇到怪物 body 时,函数 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)

我的物理类别

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")
  }

我像这样初始化 bearCoursFrames

  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

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

--------------------------------编辑------------------------- ----------------- 解决方案:我只需添加 self.physicalWorld.contactDelegate = self 现在就可以了。

最佳答案

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

将玩家或其他 body 更改为动态,并且将检测到接触。

作为补充,如果您还没有启用物理视觉表示...这可能会有所帮助,因为有时由于实际物体未彼此相交而无法检测到接触,例如。当节点的 anchor 更改时,纹理和主体会错位(可以使用 SKPhysicsBody 类的适当初始化程序修复,但这是一个非常常见的错误)。

关于swift - Sprite Kit 从未检测到我的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340196/

相关文章:

ios - 无法识别的选择器发送到实例 Swift 2.1 - 日期选择器

ios - 如何在 SpriteKit 中包裹 x 和 y 轴?

swift - 如何在演示前更改 SKScene 位置?

ios - animateWithTextures 错误,XCode 6 beta,.swift SpriteKit

ios - 如何在 SKScene 中引用我的 UIViewController?

ios - 如何在 Skscene 中呈现 UIAlert

ios - 如何使用通向另一个表的表来为应用程序实现 MVC

ios - 如何在解析表中排名

php - 在 WKWebView 上从服务器读取 docx 文件

ios - 如何通过 SKScene 播放视频