Sprite-Kit障碍游戏 - PhysicsBody问题

时间:2016-10-08 16:58:02

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

我使用SpriteKit创建了一个简单的2D街机游戏,我正在尝试添加一个评分系统。基本上游戏是一个方形精灵,必须跳过各种障碍。

到目前为止,我所尝试的是创建一个SKSpriteNode(),当它与玩家的physicsBody接触时,它会在得分计数器标签上添加+1。我的问题是,当他们彼此联系时,玩家精灵漂浮在间隙的PhysicsBody上。我已将其collisionBitMask设置为0并使其为isDynamic = false。有人可以帮忙吗?

好的,这是我的差距代码

    var gap = SKNode()

    gap.position = CGPoint(x: obstacle.position.x, y:  platform.position.y + 210)

    gap.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 30, height:  70))

    gap.physicsBody!.isDynamic = true
    gap.physicsBody!.affectedByGravity = false

    gap.physicsBody?.contactTestBitMask = PhysicsCategory.Player
    gap.physicsBody?.categoryBitMask = PhysicsCategory.Gap
    gap.physicsBody?.collisionBitMask = PhysicsCategory.None


    gap.physicsBody?.restitution = 0
    gap.physicsBody?.friction = 0

    self.addChild(gap)

这是我的球员代码

    let playerTexture = SKTexture(imageNamed: "white.jpg")

    player = SKSpriteNode(texture: playerTexture)

    player.size = CGSize(width: 40, height: 40)

    player.physicsBody = SKPhysicsBody(rectangleOf: player.size)

    player.physicsBody!.isDynamic = false

    player.position = CGPoint(x: self.frame.midX - 100 , y: self.frame.midY + 50 )

    player.physicsBody!.restitution = 0

    player.physicsBody!.friction = 0


    player.physicsBody?.categoryBitMask = PhysicsCategory.Player
    player.physicsBody?.contactTestBitMask = PhysicsCategory.Obstacles | PhysicsCategory.Gap
    player.physicsBody?.collisionBitMask = PhysicsCategory.Obstacles | PhysicsCategory.Floor

    self.addChild(player)

物理类别

struct PhysicsCategory {
static let None      : UInt32 = 0
static let Obstacles : UInt32 = 0b1     // 1
static let Player    : UInt32 = 0010
static let Floor     : UInt32 = 0100
static let Gap       : UInt32 = 1000

}

0 个答案:

没有答案