使用tap创建多个节点

时间:2017-02-01 01:30:30

标签: swift skspritenode

我尝试做的是当用户点击屏幕时,我希望它在同一位置创建多个精灵,中间有延迟,下一个节点添加到上一个节点之上。

当创建节点时,它的大小会逐渐淡出,每个节点也是不同的颜色,所以我应该能够看到最后一个节点。

我尝试过多次不同的迭代尝试让它发挥作用。以下是最新的。

func createShape(location: CGPoint){
    var positionz:CGFloat = 1

    for i in 1...6{
    let square = SKSpriteNode(color: randomColor(), size: CGSize(width: 40, height: 40))
    square.position = location
    square.zPosition = positionz
    addChild(square)

  let  shapeIncrease = SKAction.resize(toWidth: square.size.width + 50, height: square.size.height + 50, duration: 0.7)
  let fadeOut = SKAction.fadeOut(withDuration:0.5)
    //let remove = SKAction(square.removeFromParent())
    let shapeSequence = SKAction.sequence([shapeIncrease, fadeOut, SKAction.removeFromParent()])
square.run(shapeSequence)
        positionz += 1
        print("\(square.color)")
    }

}

我遇到的问题是我只能看到一个节点。节点计数器增加,我已经确认每个节点有不同的颜色,但我无法弄清楚如何使它们正确显示。

1 个答案:

答案 0 :(得分:0)

管理解决这个问题。对于那些试图做类似事情的人,我最终将节点创建放在了运行块中,作为接触的一部分,从下面开始。

我确信有更好更清洁的方法,但这对我有用,所以我会坚持下去。

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches {
        let location = touch.location(in: self)
          let wait = SKAction.wait(forDuration: 0.15)
        let s1 = SKAction.run {
            self.createShape(location: location, zposition: 1)
        }
        let s2 = SKAction.run {
            self.createShape(location: location, zposition: 2)
        }
        let s3 = SKAction.run {
            self.createShape(location: location, zposition: 3)
        }
        let s4 = SKAction.run {
            self.createShape(location: location, zposition: 4)
        }
        let s5 = SKAction.run {
            self.createShape(location: location, zposition: 5)
        }
        let s6 = SKAction.run {
            self.createShape(location: location, zposition: 6)
        }

          let sequence1 = SKAction.sequence([s1, wait, s2, wait, s3, wait, s4, wait, s5, wait, s6])
       run(sequence1)