2017-09-25 64 views
3

我有两个SKSpriteNodes,我想保持相同的位置。 newLeftObject和newLeftObjectBackground。什么是最好的办法,使两个物体始终保持相同的位置,即使施加力量。我尝试设置它们的质量和密度相等,但是它们在落下时仍然具有略微不同的位置。谢谢,任何帮助表示赞赏!如何让两个SKSpriteNode始终具有相同的位置?

func createNewObjectLeftAndMoveToCenter() { 

    colorBackground.removeFromParent() 
    blackBackground.removeFromParent() 

    var newLeftObject = SKSpriteNode() 
    var newRightObject = SKSpriteNode() 

    var newLeftObjectBackground = SKSpriteNode() 
    var newRightObjectBackground = SKSpriteNode() 


    var distance = sqrt(((colorBackground.size.width/-2) - (verticalTarget.position.x)) * ((colorBackground.size.width/-2) - (verticalTarget.position.x))) 

    var distanceToCenter = sqrt(((0 - (verticalTarget.position.x))) * (0 - (verticalTarget.position.x))) 



    newLeftObject.size = CGSize(width: distance, height: colorBackground.size.height) 
    newLeftObject.position = CGPoint(x: verticalTarget.position.x - (newLeftObject.size.width/2), y: colorBackground.position.y) 
    newLeftObject.color = colorBackground.color 
    newLeftObject.zPosition = 30 
    addChild(newLeftObject) 

    newLeftObjectBackground.size = CGSize(width: distance + 14, height: blackBackground.size.height) 
    newLeftObjectBackground.position = CGPoint(x: verticalTarget.position.x - (newLeftObject.size.width/2), y: colorBackground.position.y) 
    newLeftObjectBackground.color = UIColor.black 
    newLeftObjectBackground.zPosition = 20 

    addChild(newLeftObjectBackground) 

    newRightObject.size = CGSize(width: distanceToCenter + colorBackground.size.width/2, height: colorBackground.size.height) 
    newRightObject.position = CGPoint(x: verticalTarget.position.x + (newRightObject.size.width/2), y: colorBackground.position.y) 
    newRightObject.color = colorBackground.color 
    newRightObject.zPosition = 40 
    addChild(newRightObject) 

    newRightObjectBackground.size = CGSize(width: (distanceToCenter + colorBackground.size.width/2) + 14, height: blackBackground.size.height) 
    newRightObjectBackground.position = CGPoint(x: verticalTarget.position.x + (newRightObject.size.width/2), y: colorBackground.position.y) 
    newRightObjectBackground.color = blackBackground.color 
    newRightObjectBackground.zPosition = 35 
    addChild(newRightObjectBackground) 

    newLeftObject.physicsBody = SKPhysicsBody(rectangleOf: newLeftObject.size) 
    newLeftObject.physicsBody?.isDynamic = true 
    newLeftObject.physicsBody?.affectedByGravity = true 
    newLeftObject.physicsBody?.allowsRotation = true 
    newLeftObject.physicsBody?.collisionBitMask = 0 
    newLeftObject.physicsBody?.density = 1 
    newLeftObject.physicsBody?.mass = 1 
    newLeftObject.physicsBody?.applyAngularImpulse(1) 

    newLeftObject.physicsBody?.applyForce(CGVector(dx: -10000, dy: 10000)) 

    newLeftObjectBackground.physicsBody = SKPhysicsBody(rectangleOf: newLeftObjectBackground.size) 
    newLeftObjectBackground.physicsBody?.isDynamic = true 
    newLeftObjectBackground.physicsBody?.affectedByGravity = true 
    newLeftObjectBackground.physicsBody?.collisionBitMask = 0 
    newLeftObjectBackground.physicsBody?.density = 1 
    newLeftObjectBackground.physicsBody?.mass = 1 
    newLeftObjectBackground.physicsBody?.allowsRotation = true 
    newLeftObjectBackground.physicsBody?.applyAngularImpulse(1) 

    newLeftObjectBackground.physicsBody?.applyForce(CGVector(dx: -10000, dy: 10000)) 

    newLeftObject.position = newLeftObjectBackground.position 

    var moveNewObjectToCenter = SKAction.moveTo(x: 0, duration: 0.25) 

    newRightObject.run(moveNewObjectToCenter) 
    newRightObjectBackground.run(moveNewObjectToCenter) 

    colorBackground = newRightObject 
    blackBackground = newRightObjectBackground 

} 

回答

2

使一个其他使用addChild()

+0

简短的回答我见过的whiel :) +1的孩子 – Fluidity

相关问题