根据另一个节点

时间:2017-08-16 19:21:04

标签: swift sprite-kit

我想这样做,所以我的节点(球)总是最终根据块1的最终位置重置在我的另一个节点block1之上。但是,我想不出任何可以帮助我做到这一点的代码。我正在考虑制作一个函数 ball.position = block1 + 20 ,但我不知道如何去做。谢谢你的帮助!

代码:

func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}


func resetScene (){

     let ball = childNode(withName: BallCategoryName) as! SKSpriteNode
    ball.removeFromParent()

    ball.physicsBody?.velocity = CGVector( dx: 0, dy: 0 )
    ball.physicsBody?.collisionBitMask = BallCategory
    ball.physicsBody?.collisionBitMask = BorderCategory | PaddleCategory
    ball.zRotation = 0.0
    addChild(ball)
    let block1 = childNode(withName: Block1Name) as! SKSpriteNode
    block1.removeFromParent()
    let actualX = random(min:85, max: 300)
    block1.position = CGPoint(x: actualX, y: 190)
    addChild(block1)

//ball.position = CGPoint(x: , y: )

//This is the line i would like to change



    canRestart = false


}

1 个答案:

答案 0 :(得分:1)

假设您只是想设置球的Y位置,您可以使用

ball.position.y = block1.position.y + 20

或者如果你需要设置x和y

ball.position = CGPoint(x: block1.position.x, y: block1.position.y + 20)