在特定时间后如何删除SKSpriteNodes

不同的

我想添加SKSpriteNodes并将它们随机地以特定的时间间隔放置在场景中。然后,我希望在特定时间后删除这些节点。

问题是删除操作无效。

我尝试在SKAction.sequence中使用removeFromParent(),但未执行代码。

在特定时间后如何删除SKSpriteNodes?

import SpriteKit

class GameScene: SKScene {

override func didMoveToView(view: SKView) {
    runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock(placeFruit),
            SKAction.waitForDuration(1.0)
            ])))
}

func placeFruit() {

    let fruit = SKSpriteNode(imageNamed: "apple")
    fruit.position = CGPoint(x: frame.size.width * random(min: 0, max: 1), y: frame.size.height * random(min: 0, max: 1))

    addChild(fruit)

    runAction(
        SKAction.sequence([
            SKAction.waitForDuration(1.0),
            SKAction.removeFromParent()
            ]))
}

override func update(currentTime: CFTimeInterval) {
}

func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(#min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}

}
尚普

您正在运行序列操作,self而不是您的fruit节点。

像这样简单地更改您的功能:

func placeFruit() {

    let fruit = SKSpriteNode(imageNamed: "apple")
    fruit.position = CGPoint(x: frame.size.width * random(min: 0, max: 1), y: frame.size.height * random(min: 0, max: 1))

    addChild(fruit)

    fruit.runAction(
        SKAction.sequence([
            SKAction.waitForDuration(1.0),
            SKAction.removeFromParent()
        ])
    )

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章