【问题标题】:creating multiple nodes on top of each other with tap用点击在彼此之上创建多个节点
【发布时间】:2017-02-01 01:30:30
【问题描述】:

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

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

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

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)")
    }

}

我遇到的问题是我只能看到一个节点。节点计数器增加,我已经确认每个节点都有不同的颜色,但我不知道如何让它们正确显示。

【问题讨论】:

    标签: swift skspritenode


    【解决方案1】:

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

    我确信有一种更好、更清洁的方法可以做到这一点,但这对我有用,所以我会坚持下去。

       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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 2018-02-09
      • 2019-05-10
      • 1970-01-01
      • 2016-03-06
      相关资源
      最近更新 更多