SpriteKit physicsBody:设置碰撞,但不要推动'宾语

时间:2016-12-06 18:01:04

标签: swift sprite-kit swift3

我有一个我正在测试的游戏,它是一个垂直游戏,可以检测左右移动的动作。我试图阻止“玩家”#39;节点离开屏幕的一侧。我已经设置了physicsBody,但问题是,当玩家在屏幕边缘碰到块时,它正在推动块并将其移出屏幕。我需要的是它只是击中阻挡,甚至可能反弹一点但最重要的是我只需要它来阻止玩家精灵离开屏幕。

播放器代码:

player.zPosition = 10

        player.position = CGPoint(x: size.width * 0.1, y: size.height * 0.5)

        player.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Airplane"), size: CGSize(width: player.size.width, height: player.size.height))

        player.physicsBody?.affectedByGravity = false
        player.physicsBody!.categoryBitMask = BodyType.player.rawValue
        player.physicsBody!.contactTestBitMask = BodyType.score.rawValue
        player.physicsBody!.collisionBitMask = BodyType.dontCollide.rawValue

        addChild(player)

边缘代码'阻止':

func sideRestraints() {

        var leftSide:SKSpriteNode!

        leftSide = SKSpriteNode(imageNamed: "block")
        leftSide.anchorPoint = CGPoint.zero
        leftSide.position = CGPoint(x: 0, y: 0)
        leftSide.size = CGSize(width: 10, height: frame.size.height)
        leftSide.zPosition = 2;

        leftSide.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: leftSide.size.width, height: leftSide.size.height))
        leftSide.physicsBody?.affectedByGravity = false
        leftSide.physicsBody!.collisionBitMask = BodyType.player.rawValue
        leftSide.physicsBody!.categoryBitMask = BodyType.sides.rawValue
        leftSide.physicsBody!.contactTestBitMask = BodyType.player.rawValue

        addChild(leftSide)

    }

bodyType结构:

enum BodyType:UInt32 {

    case player = 1
    case score = 2
    case dontCollide = 4
    case sides = 8

}

非常感谢任何输入。

0 个答案:

没有答案