SKPhysicsBody对象碰撞未对齐

时间:2015-05-16 13:33:16

标签: ios swift sprite-kit

我有一名球员。他必须跳过一些街区。

然而,似乎SKPhysicsBody(rectangleOfSize: ... )未正确对齐。当角色击中块的上半部分时,它就会通过而无法通过它,因为它会与不可见的SKPhysicsBody发生碰撞。

根据我如何设置锚点,有时甚至会发生物理体位于其左侧或右侧的SKSpriteNode

我的代码:

    self.smallBlock.anchorPoint = CGPointMake(0.5, 0.5)
    self.smallBlock.position = CGPointMake(CGRectGetMaxX(self.frame) + self.smallBlock.size.width, CGRectGetMinY(self.frame) + self.runningBar.size.height * 2)

    self.smallBlock.physicsBody = SKPhysicsBody(rectangleOfSize: self.smallBlock.size)
    self.smallBlock.physicsBody?.dynamic = false
    self.smallBlock.physicsBody?.categoryBitMask = colliderType.Block.rawValue
    self.smallBlock.physicsBody?.contactTestBitMask = colliderType.Character.rawValue
    self.smallBlock.physicsBody?.collisionBitMask = colliderType.Character.rawValue
    self.smallBlock.physicsBody?.usesPreciseCollisionDetection = true

    self.addChild(self.smallBlock)

    self.character.anchorPoint = CGPointMake(0, 0.25)
    self.character.position = CGPointMake(CGRectGetMinX(self.frame) - self.character.size.width, CGRectGetMinY(self.frame) + self.character.size.height)
    self.initPosX = CGRectGetMinX(self.frame) + (self.character.size.width * 1.25)

角色代码

    self.character.anchorPoint = CGPointMake(0, 0.25)
    self.character.position = CGPointMake(CGRectGetMinX(self.frame) - self.character.size.width, CGRectGetMinY(self.frame) + self.character.size.height)
    self.initPosX = CGRectGetMinX(self.frame) + (self.character.size.width * 1.25)

    self.character.physicsBody = SKPhysicsBody(rectangleOfSize: self.character.size)
    self.character.physicsBody?.affectedByGravity = true
    self.character.physicsBody?.categoryBitMask = colliderType.Character.rawValue
    self.character.physicsBody?.contactTestBitMask = colliderType.Block.rawValue
    self.character.physicsBody?.collisionBitMask = colliderType.Block.rawValue
    self.character.physicsBody?.usesPreciseCollisionDetection = true
    self.character.physicsBody?.affectedByGravity = true
    self.character.physicsBody?.allowsRotation = false

    self.addChild(self.character)

1 个答案:

答案 0 :(得分:1)

解决方案:

我已将skView.showsPhysics = true添加到我的View Controller中。当您在模拟器中运行应用程序时,它将概述每个对象,如here所示。

事实证明,设置自定义锚点会使rectangleOfSize - 位置变得混乱。