我正在创造一个游戏,对于我的角色,他的腿和躯干有不同的动画,因此它们是具有单独物理体的独立节点。我在连接躯干和腿部时遇到了很多麻烦,然而,连接它们不是问题,保持联系是。在移动的同时,英雄的躯干有时从腿上滑落。有点搞笑但不实用哈哈。这是我的物理编码
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个独立节点的物理体编码?或者我只是错过了一些小代码?任何帮助都有帮助,谢谢。
答案 0 :(得分:2)
问题是两个物理主体会相互碰撞然后因为你将动态设置为true它们会移动你可以将动态设置为false或
在BodyType Struct中添加一个新常量,如下所示:
static let none: UInt32 = 0
然后您可以将此代码添加到物理主体
soldierLegsBody.collisionBitMask = BodyType.none
soldierTorsoBody.collisionBitMask = BodyType.none
或者如果你需要士兵从地面反弹,你需要为地面创造一个新的身体类型:
static let ground: UInt32 = < A Number >
然后你可以设置teh collisionBitMask到BodyType.ground。
希望这有帮助!如果您有任何问题,请发表评论:)
答案 1 :(得分:1)
在宣布物理联合
之后我明白了this.$.child._update();
通过
将联合动态值的第二个节点声明为falselet joint =
SKPhysicsJointFixed.jointWithBodyA(soldierLegs.physicsBody!,
bodyB:soldierTorso.physicsBody!, anchor: soldierLegs.position)
然后使用更新功能重新定位节点
joint.bodyB.dynamic = false
每秒定位额外节点60次应该确保节点不移动,或者对于更高的帧速率或更平滑的运行我确定每秒定位5-20次会很好但是每次定位躯干60次秒没有降低我的任何帧速率