即使父级移动,如何在屏幕上为每个节点赋予相同的起始位置?

时间:2018-08-03 10:10:24

标签: swift xcode nodes skspritenode cgpoint

我正在用SpriteKit开发游戏。我有一个多维数据集,它是所有其他节点的父级。如果用户点击屏幕,则Node1开始从屏幕中间掉落。在Node1落在多维数据集上之后,多维数据集向下移动了一点。当用户再次轻按时,Node2开始下降,但Node2的位置比Node1低,因为父级向下移动。

有没有一种方法可以将每个节点的位置设置为屏幕上某个点,而不管父级在哪里?

父节点:

Cube.setScale(0.85)
Cube.zPosition = 3
Cube.position.x = self.size.width / 2
Cube.physicsBody = SKPhysicsBody(texture: CubeTexture,size: CGSize(width: CubeTexture.size().width * 0.9, height: CubeTexture.size().height * 0.82)) 
    Cube.physicsBody?.isDynamic = false
    Cube.physicsBody?.affectedByGravity = false
    self.addChild(Cube)

子节点:

    node1.setScale(0.95)
    node1.position = CGPoint(x: -350, y: 310)  //<-- I want this position to be a constant value on the screen
    node1.zPosition = 1

    node1.physicsBody = SKPhysicsBody(texture: node1Texture,size: CGSize(width: node1Texture.size().width * 1.1, height: node1Texture.size().height * 0.85)) 
    node1.physicsBody?.isDynamic = true
    node1.physicsBody?.affectedByGravity = false
    node1.physicsBody?.usesPreciseCollisionDetection = true
    node1.physicsBody?.restitution = 0
    Cube.addChild(node1):


    lazy var node2 = node1.copy() as! SKSpriteNode
    lazy var node3 = node1.copy() as! SKSpriteNode
    lazy var node4 = node1.copy() as! SKSpriteNode
    lazy var node5 = node1.copy() as! SKSpriteNode
    lazy var node6 = node1.copy() as! SKSpriteNode
    lazy var node7 = node1.copy() as! SKSpriteNode
    lazy var node8 = node1.copy() as! SKSpriteNode
    lazy var node9 = node1.copy() as! SKSpriteNode
    lazy var node10 = node1.copy() as! SKSpriteNode

每次节点掉落时立方体移动:

  func addMovement3(Cube:SKSpriteNode) {
    var actionArray = [SKAction]()

    actionArray.append(SKAction.moveTo(y: Cube.position.y - 61.8, duration: 0.1))

    Cube.run(SKAction.sequence(actionArray))
}

1 个答案:

答案 0 :(得分:0)

您将要创建或引用多维数据集节点的父级。无需将节点添加到多维数据集中,而是将节点添加为多维数据集父级的子级,因为多维数据集向下移动时,节点将能够保持其原点。