我想添加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
}
}
答案 0 :(得分:2)
您正在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()
])
)
}
答案 1 :(得分:0)
func makeSpriteNode () {
var spriteNodelabel = SKSpriteNode(imageNamed: "imagename")
spriteNodelabel.position = CGPoint(x:CGRectGetMidX(frame), y: frame.size.height*0.90)
spriteNodelabel.size = CGSizeMake(frame.size.width*0.70, frame.size.height*0.06)
spriteNodelabel.alpha = 0.2
//Position of SpriteNode
spriteNodelabel.zPosition = 21.0
self.addChild(spriteNodelabel)
//Action to remove SpriteNode
spriteNodelabel.runAction(
SKAction.sequence([
//Here we define Time duration in With SpriteNode remove!
SKAction.waitForDuration(1.0),
SKAction.removeFromParent()
])
)
}
使用此功能创建SKSpriteNode!它将在1秒延迟后删除!