如何在swift中正确复制SKTileMapNode

时间:2017-04-17 06:28:57

标签: swift sprite-kit sktilemapnode

当我在节点上运行loop时,一切正常,节点移动正确。但是当next动作中断时next到达屏幕的左边界(中途)时,我会运行循环。我认为复制有问题。谢谢!

func moveBackground (name: String, speed: CGFloat = 0.01) {


    guard let node = childNode(withName: name) as? SKTileMapNode else {
        fatalError("\(name) node not loaded")
    }


    let width = node.frame.size.width
    let startPositionX = width


    node.position = CGPoint(x: startPositionX , y: 0)


    let next = node.copy() as! SKTileMapNode
    next.tileSet = node.tileSet
    next.position = CGPoint(x: startPositionX , y: 0)
    self.addChild(next)


    let distance = width * 2
    let duration = TimeInterval(speed * distance)


    let moveAction = SKAction.moveBy(x: -distance, y: 0, duration: duration)
    let resetAction = SKAction.moveTo(x: startPositionX, duration: 0)


    let sequence = SKAction.sequence([moveAction, resetAction])
    let loop = SKAction.repeatForever(sequence)


    //node.run(loop)
    next.run(loop)

}

0 个答案:

没有答案