我创建了一个名为chainsawHolder的容器SKSpriteNode,它有一个名为longChainsaw的子SKSpriteNode。我想对chainsawHolder施加一个冲动,这样当它移动时,它的子节点随之移动。我尝试了几件事,但我无法做到这一点。似乎只有我的容器SKSpriteNode才能有一个physicsSize。但是它的childNode不能有大小 - 如果有,它将不会随容器节点一起移动。
我需要将子节点作为物理节点,因为当它与我的英雄发生碰撞时,它需要调用一个函数。
当应用脉冲时,如何让我的子节点与容器节点一起移动?
以下是代码:
let chainsawHolderWidth = size.width
let chainsawHolderHeight = size.height
chainsawHolder.size = CGSize(width: chainsawHolderWidth, height: chainsawHolderHeight)
chainsawHolder.anchorPoint = CGPoint(x: 0, y: 0)
chainsawHolder.position = CGPoint(x: 0, y: 0)
chainsawHolder.physicsBody = SKPhysicsBody(rectangleOfSize: chainsawHolder.size)
chainsawHolder.physicsBody?.dynamic = true
chainsawHolder.physicsBody?.categoryBitMask = bgCategory
chainsawHolder.physicsBody?.contactTestBitMask = boCategory
chainsawHolder.physicsBody?.collisionBitMask = noneCategory
//LONG CHAINSAW
let longChainsawWidth = size.width/3
let longChainsawHeight = size.height*0.20
longChainsaw.size = CGSize(width: longChainsawWidth, height: longChainsawHeight)
let textures = [SKTexture(imageNamed: "chainsaw1"), SKTexture(imageNamed: "chainsaw2")]
let animation = SKAction.animateWithTextures(textures, timePerFrame: 0.1)
let runSawAnimation = SKAction.repeatActionForever(animation)
longChainsaw.runAction(runSawAnimation)
let longChainsawPhysicsSize = CGSizeMake(longChainsawWidth - longChainsawWidth/14, longChainsawHeight - longChainsawHeight/12)
longChainsaw.physicsBody = SKPhysicsBody(rectangleOfSize: longChainsawPhysicsSize)
longChainsaw.position = CGPoint(x: size.width*0.5, y: size.height*0.5)
longChainsaw.physicsBody?.dynamic = true
longChainsaw.physicsBody?.categoryBitMask = chainsawCategory
longChainsaw.physicsBody?.contactTestBitMask = boCategory
longChainsaw.physicsBody?.collisionBitMask = noneCategory
chainsawHolder.addChild(longChainsaw)
self.addChild(chainsawHolder)
答案 0 :(得分:1)
物理引擎没有亲子关系的概念。
您可以做的是用关节连接两个身体。然而,这意味着在向身体施加力时总会有一定的灵活性。
另一种方法是将力施加到支架上,然后在场景的更新方法中将长链锯的位置设置为支架位置加上偏移量。
答案 1 :(得分:0)
实际上,你几乎就在那里。你只需要在两个电锯片之间创建物理连接:
// Where your joint will be .. you really should position your chainsaw
// holder & longchainsaw first before you join them.
let my_joint_point_in_scene_coordinates = chainsawHolder.position
let myJoint = SKPhysicsJointFixed.jointWithBodyA(
chainsawHolder.physicsBody!,
bodyB: longChainsaw.physicsBody!,
anchor: my_joint_pt_in_scene_coords )
self.physicsWorld.addJoint(myJoint)
此外,如果您希望您的长电锯在电锯支架内旋转(如从前臂旋转的手腕),而不是使用SKPhysicsJointFixed,请使用SKPhysicsJointPin,然后您可以旋转longChainsaw节点。