【问题标题】:SpriteKit categoryBitMask not recognizedSpriteKit 类别BitMask 无法识别
【发布时间】:2016-06-13 17:27:19
【问题描述】:

我有三个物理类别:英雄、地面和墙。我的问题是地面的物理类别没有被识别。为了解决这个问题,当英雄与墙壁和地面发生碰撞时,我打印了位掩码。墙按预期工作,并显示其位掩码 2。地面,但是,显示 4294967295。(应该是 4。)地面有一个边缘物理体,它工作是因为英雄没有穿过它,它只是没有被识别为地面。

物理类别

enum PhysicsCategory:UInt32
{
    case hero = 1
    case wall = 2
    case ground = 4
}

地面等级:

class Ground: SKSpriteNode
{
    var groundTexture = SKTexture(imageNamed: "ground4")
    var jumpWidth = CGFloat()
    var jumpCount = CGFloat(1)

    func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
    {
        parentNode.addChild(self)
        self.size = size
        self.position = position
        self.zPosition = 2
        self.anchorPoint = CGPointMake(0, 1)
        self.texture = SKTexture(imageNamed: "ground4")
        self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
        self.physicsBody?.affectedByGravity = false
        self.physicsBody?.dynamic = false

        let pointTopRight = CGPoint(x: size.width, y: 0)
        self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)
    }

didMoveToView:

let groundPosition = CGPoint(x: -self.size.width, y: 30)
let groundSize = CGSize(width: self.size.width * 3, height: 0) 
ground.spawn(world, position: groundPosition, size: groundSize)

didBeginContact

let firstBody = contact.bodyA
let secondBody = contact.bodyB

if firstBody.categoryBitMask == PhysicsCategory.hero.rawValue && secondBody.categoryBitMask == PhysicsCategory.ground.rawValue || firstBody.categoryBitMask == PhysicsCategory.ground.rawValue && secondBody.categoryBitMask == PhysicsCategory.hero.rawValue
        {
            print("contact with the ground!")
        }

【问题讨论】:

    标签: swift sprite-kit game-physics skphysicsbody bitmask


    【解决方案1】:

    您实际上是在尝试设置 categoryBitMask 之后创建了physicsBody。所以你试图将 categoryBitMask 设置为 nil...

    你只需要向上移动那一行...

    func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
    {
        parentNode.addChild(self)
        self.size = size
        self.position = position
        self.zPosition = 2
        self.anchorPoint = CGPointMake(0, 1)
        self.texture = SKTexture(imageNamed: "ground4")
        let pointTopRight = CGPoint(x: size.width, y: 0)
    
        self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)
    
        self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
        self.physicsBody?.affectedByGravity = false
        self.physicsBody?.dynamic = false
    }
    

    【讨论】:

    • 我知道我应该避免在 cmets 中说谢谢,但我会为此冒险...谢谢!!!!
    猜你喜欢
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    相关资源
    最近更新 更多