孩子一旦从父母中移出后不会再出现

时间:2019-10-25 17:45:02

标签: swift xcode sprite-kit

我正在制作一个超级简单的游戏,其中该游戏从一组形状(代码块中的形状)创建一个随机的,可点击的形状。点击后,我在点击的节点上调用node.removefromparent(),并通过addchild将同一数组中的新节点添加到rootnode。

这是我的问题:如果创建的随机节点与先前删除的节点相同,则不会出现。我该如何解决?

以下是创建功能和删除功能的代码:

创造

func CreateShape() {

        let shape = shapes.randomElement()!
        shape.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        shape.colorBlendFactor = 1.0
        shape.color = colors.randomElement() ?? UIColor.systemPink
        shape.name = "figur"
        shape.zPosition = 100
        shape.physicsBody = SKPhysicsBody(circleOfRadius: 150)
        shape.physicsBody?.isDynamic = false
        shape.position = CGPoint(x: randomPos().x, y: randomPos().y)
        print(shape)

        addChild(shape)

    }

删除:

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

        let touch = touches.first
        let location = touch!.location(in: self)
        let tappedNodes = nodes(at: location)

        guard let tapped = tappedNodes.first else { return }

        if tapped.name == "figur" {

            let fadeOut = SKAction.fadeAlpha(to: 0, duration: 0.5)
            let randomAction = actions.randomElement()!
            let remove = SKAction.removeFromParent()
            let sequence = SKAction.sequence([randomAction, fadeOut, remove])
            sequence.timingMode = .easeInEaseOut

            tapped.run(sequence)

            self.isUserInteractionEnabled = false

            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5, execute: {

                self.isUserInteractionEnabled = true
                self.CreateShape()

            })
        }
    }

1 个答案:

答案 0 :(得分:1)

您将Alpha设置为0,但从未重置过。

func CreateShape() {

        let shape = shapes.randomElement()!
        shape.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        shape.colorBlendFactor = 1.0
        shape.color = colors.randomElement() ?? UIColor.systemPink
        shape.name = "figur"
        shape.zPosition = 100
        shape.physicsBody = SKPhysicsBody(circleOfRadius: 150)
        shape.physicsBody?.isDynamic = false
        shape.position = CGPoint(x: randomPos().x, y: randomPos().y)
        shape.alpha = 1.0 //<-----insert here
        print(shape)


        addChild(shape)

    }