SpriteKit& Swift:即使正确设置了collisionBitMask,节点也不会发生碰撞

时间:2016-12-13 16:52:23

标签: ios swift sprite-kit game-physics

我正在制作2D sidescroller游戏。我有一个名叫龙的角色和各种平台,障碍物和从右到左的敌人等。我的角色与平台发生碰撞,但与障碍物碰撞。我试图让他与障碍物发生碰撞但不知何故它不起作用。

设置categoryBitMasks:

    struct PhysicsCatagory{
static let dragon : UInt32 = 0x1 << 1
static let ground : UInt32 = 0x1 << 2
static let platforms : UInt32 = 0x1 << 3
static let bigGroundPlatform : UInt32 = 0x1 << 4
static let enemy : UInt32 = 0x1 << 5
static let obstacle : UInt32 = 0x1 << 6
static let obstacleDeathObject : UInt32 = 0x1 << 7
static let spring : UInt32 = 0x1 << 8
}

以下是我设置角色的方式:

func addDragon() {

    dragon = SKSpriteNode(imageNamed: "Dragon-Standing")
    dragon.zPosition = 100
    dragon.anchorPoint = CGPointMake(0.6, 0.5)
    dragon.position = CGPoint(x: 300, y: 300)
    dragon.setScale(0.21)
    dragon.name = "Dragon"

    let dragonsPhysicsBody = SKSpriteNode()
    dragonsPhysicsBody.size = CGSize(width: 170, height: 170)
    dragonsPhysicsBody.zPosition = 99
    dragonsPhysicsBody.position.x = dragon.position.x; dragonsPhysicsBody.position.y = dragon.position.y

    dragon.physicsBody = SKPhysicsBody(rectangleOfSize: dragonsPhysicsBody.size)
    dragon.physicsBody?.categoryBitMask = PhysicsCatagory.dragon
    dragon.physicsBody?.contactTestBitMask = PhysicsCatagory.ground | PhysicsCatagory.platforms | PhysicsCatagory.bigGroundPlatform | PhysicsCatagory.obstacle
    dragon.physicsBody?.collisionBitMask = PhysicsCatagory.ground | PhysicsCatagory.obstacle
    dragon.physicsBody?.affectedByGravity = true
    dragon.physicsBody?.dynamic = true
    dragon.physicsBody?.allowsRotation = false
    dragon.physicsBody?.restitution = 0
    dragon.physicsBody?.mass = 0.176455050706863


    worldNode.addChild(dragon)

}

障碍代码:

    func createObstacle() {

    if imageNameObstacles == "Stone_Obstacle1" {

        wasStoneObstacle1Here = true
        wasStoneObstacle2Here = false

        obstacle = SKSpriteNode(texture: stone_Obstacle1_Texture)
        obstacle.xScale = 0.32
        obstacle.yScale = 0.27
        obstacle.zPosition = 3


        let randomObstaclePositions = arc4random_uniform(3) + 1;
        // RANDOM OBSTACLE POSITION STUFF

        obstacle.physicsBody = SKPhysicsBody(rectangleOfSize: obstacle.size)
        obstacle.physicsBody?.categoryBitMask = PhysicsCatagory.obstacle
        obstacle.physicsBody?.contactTestBitMask = PhysicsCatagory.dragon
        obstacle.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        obstacle.physicsBody?.dynamic = false
        obstacle.physicsBody?.affectedByGravity = false
        obstacle.physicsBody?.friction = 0.0
        obstacle.physicsBody?.restitution = 0.0

        groundPair.addChild(obstacle)
    }

我的平台代码:

    func platforms_3() {

    spawnedPlatform3 = true

    bigGroundPlatform = SKSpriteNode(texture: middle_Ground_Platform_Texture)
    bigGroundPlatform.zPosition = 4
    bigGroundPlatform.xScale = 0.45; bigGroundPlatform.yScale = 0.50
    bigGroundPlatform.position = CGPoint(x: 2800, y: 355)

    bigGroundPlatform.physicsBody = SKPhysicsBody(rectangleOfSize: bigGroundPlatform.size)
    bigGroundPlatform.physicsBody?.categoryBitMask = PhysicsCatagory.bigGroundPlatform
    bigGroundPlatform.physicsBody?.contactTestBitMask = PhysicsCatagory.dragon
    bigGroundPlatform.physicsBody?.dynamic = false
    bigGroundPlatform.physicsBody?.affectedByGravity = false
    bigGroundPlatform.physicsBody?.friction = 0.0
    bigGroundPlatform.physicsBody?.restitution = 0.0

    groundPair = SKNode()
    groundPair.zPosition = 4

    groundPair.addChild(bigGroundPlatform)


    let movePlatforms = SKAction.runBlock({
        () in

        let distance = CGFloat(25000)
        let moveGroundObjects = SKAction.moveByX(-distance, y: 0, duration: NSTimeInterval(self.platformSpeed))
        let removeGroundObjects = SKAction.removeFromParent()
        self.moveAndRemove = SKAction.sequence([moveGroundObjects, removeGroundObjects])

        self.groundPair.runAction(self.moveAndRemove)
    })

    groundPair.runAction(movePlatforms)
    worldNode.addChild(groundPair)

}

我的平台的collisionBitMask没有设置,因为如果我的角色在一个平台下并试图跳跃,他会与它碰撞而不是跳过它然后降落在它上面。 此代码解决了上述问题:

    func makePlatformsNil() { // Runs in update function

    if dragon.physicsBody?.velocity.dy < -0.0 {

        platform1.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform2.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform3.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform4.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform5.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform6.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform7.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform8.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform9.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        platform10.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        bigGroundPlatform.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        bigGroundPlatform2.physicsBody?.collisionBitMask = PhysicsCatagory.dragon
        dragon.physicsBody?.collisionBitMask = PhysicsCatagory.ground | PhysicsCatagory.platforms | PhysicsCatagory.bigGroundPlatform
    }

    if dragon.physicsBody?.velocity.dy > 0.0 {

        platform1.physicsBody?.collisionBitMask = 0
        platform2.physicsBody?.collisionBitMask = 0
        platform3.physicsBody?.collisionBitMask = 0
        platform4.physicsBody?.collisionBitMask = 0
        platform5.physicsBody?.collisionBitMask = 0
        platform6.physicsBody?.collisionBitMask = 0
        platform7.physicsBody?.collisionBitMask = 0
        platform8.physicsBody?.collisionBitMask = 0
        platform9.physicsBody?.collisionBitMask = 0
        platform10.physicsBody?.collisionBitMask = 0
        bigGroundPlatform.physicsBody?.collisionBitMask = 0
        bigGroundPlatform2.physicsBody?.collisionBitMask = 0
        dragon.physicsBody?.collisionBitMask = PhysicsCatagory.ground
    }

    if collidingWithGround == true {
        bigGroundPlatform.physicsBody?.collisionBitMask = 0
        dragon.physicsBody?.collisionBitMask = PhysicsCatagory.ground
    }
}

正如你所看到的,我的龙的碰撞障碍和我的障碍都被正确设置但它们仍然没有碰撞。现在有人为什么? 我在didBeginContact中放了一个print语句,看看当龙和障碍物碰撞时它是否被调用,它被调用。

感谢任何帮助。 提前谢谢。

0 个答案:

没有答案