如何在 sprite kit/SKphysics joint 中创建多个节点角色?

How to create multiple node characters in sprite kit /SKphysics joint?

我正在创建一个游戏,对于我的角色,他的腿和躯干有单独的动画,因此它们是具有单独物理体的单独节点。我在将躯干和腿连接在一起时遇到了很多麻烦,但是,将它们连接起来不是问题,让它们保持连接才是问题。在四处走动时,英雄的躯干有时会从腿上滑落。有点有趣但不实用哈哈。这是我的物理编码

enum BodyType:UInt32 {

case bullet1 = 2

case enemy1 =  4
case enemy2 =  16
case enemy3 =  32

case desertForegroundCase = 64

case tank11 =  128
case tank12 =  256
case tank13 =  512

case tank21 =  1024
case tank22 =  2048
case tank23 =  4056

case tank31 =  8112
case tank32 =  16224
case tank33 =  32448

case cliff1 =  64856
case cliff2 =  129212

case soldierT = 258424
case soldierL = 516848

static let ground: UInt32 = 1
}
     func CreateScene (){

    desertBackground.position = CGPoint(x: frame.size.width / 2, y: 
    frame.size.height / 2)
    desertBackground.size = CGSize (width: 1136, height: 640)
    desertBackground.zPosition = -5
    desertForegroundImage.position = CGPointMake(568, 100)
    desertForegroundImage.zPosition = -1
    let desertForeground = SKSpriteNode(texture: 
    desertForegroundTexture, size:CGSize(width: 1136, height: 200))
        desertForeground.position = CGPointMake(568, 100)
    let desertForegroundBody = SKPhysicsBody(texture: 
    desertForegroundTexture, size: CGSize(width: 1136, height: 200))
    desertForegroundBody.dynamic = false
    desertForegroundBody.affectedByGravity = false
    desertForegroundBody.allowsRotation = false
    desertForegroundBody.categoryBitMask = BodyType.ground
    desertForegroundBody.contactTestBitMask = BodyType.enemy1.rawValue 
    | BodyType.enemy2.rawValue | BodyType.enemy3.rawValue  | 
    BodyType.soldierL.rawValue | BodyType.soldierT.rawValue
    desertForeground.physicsBody = desertForegroundBody
    desertForeground.zPosition = -1

    self.addChild(desertForegroundImage)
    self.addChild(desertForeground)
    self.addChild(desert gully)

    }
   func CreateHero (){

    soldierLegs.position = CGPoint(x: 405 , y: 139)
    soldierLegs.zPosition = 1
    soldierLegs.anchorPoint.x = 0.6
    soldierLegs.anchorPoint.y = 0.7

    let soldierLegsBody:SKPhysicsBody = SKPhysicsBody(rectangleOfSize: 
    soldierLegs.size)
    soldierLegsBody.dynamic = true
    soldierLegsBody.affectedByGravity = true
    soldierLegsBody.allowsRotation = false
    //body.restitution = 0.4
    soldierLegsBody.categoryBitMask = BodyType.soldierL.rawValue
    soldierLegsBody.contactTestBitMask = BodyType.enemy1.rawValue | 
    BodyType.enemy2.rawValue | BodyType.enemy3.rawValue  | 
    BodyType.desertForegroundCase.rawValue
    soldierLegs.physicsBody = soldierLegsBody

    soldierTorso.position = soldierLegs.position
    soldierTorso.zPosition = 2
    soldierTorso.anchorPoint.x = 0.25
    soldierTorso.anchorPoint.y = 0.1

    let soldierTorsoBody:SKPhysicsBody = SKPhysicsBody(rectangleOfSize: 
    soldierTorso.size)
    soldierTorsoBody.dynamic = true
    soldierTorsoBody.affectedByGravity = true
    soldierTorsoBody.allowsRotation = false
    soldierTorsoBody.categoryBitMask = BodyType.soldierT.rawValue
    soldierTorsoBody.contactTestBitMask = BodyType.enemy1.rawValue | 
    BodyType.enemy2.rawValue | BodyType.enemy3.rawValue  | 
    BodyType.desertForegroundCase.rawValue
    soldierTorso.physicsBody = soldierTorsoBody
    let joint  = 
    SKPhysicsJointFixed.jointWithBodyA(soldierLegs.physicsBody!, bodyB: 
    soldierTorso.physicsBody!, anchor: soldierLegs.position)

    soldierLegsBody.collisionBitMask = BodyType.ground
    soldierTorsoBody.collisionBitMask = BodyType.ground

    self.addChild(soldierTorso)
    self.addChild(soldierLegs)
    self.physicsWorld.addJoint(joint)

}

这就是他将滑落的程度。有没有办法只用 2 个独立的节点编写一个物理体?还是我只是缺少一些代码?有帮助就是帮助,谢谢。

问题是两个物理物体会相互碰撞,因为你将动态设置为真,它们会移动,你可以将动态设置为假或

在 BodyType 结构中添加一个新常量,如下所示:

static let none: UInt32 = 0

然后你可以将这段代码添加到物理体中

soldierLegsBody.collisionBitMask = BodyType.none
soldierTorsoBody.collisionBitMask = BodyType.none

或者如果您需要士兵从地面弹起,您将需要为地面创建一个新的 body 类型,如下所示:

 static let ground: UInt32 = <  A Number >

然后您可以将 collisionBitMask 设置为 BodyType.ground。

希望对您有所帮助!如果您有任何问题,请发表评论:)

我明白了,在声明物理关节后

let joint = 
SKPhysicsJointFixed.jointWithBodyA(soldierLegs.physicsBody!, 
bodyB:soldierTorso.physicsBody!, anchor: soldierLegs.position)

通过

声明关节动态值的第二个节点为false
    joint.bodyB.dynamic = false

然后使用更新函数重新定位节点

 override func update(currentTime: CFTimeInterval) {

   soldierTorso.position = soldierLegs.position

}

每秒定位额外节点 60 次应该确保节点不会移动,或者为了更高的帧速率或更平滑 运行 我确定每秒定位 5 - 20 次会很好但是每秒将躯干定位 60 次并没有降低我的任何帧速率