Xcode SpriteKit - 删除精灵并停止操作 - repeatActionForever
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26870224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode SpriteKit - Removing Sprites and stopping an action - repeatActionForever
提问by Ryann786
I am new to Swift and SpritKit and having a few issues with my game.
我是 Swift 和 SpritKit 的新手,我的游戏有一些问题。
In my didMoveToView(view: SKView) { }
section of my code I call the below statement which populates monsters on the screen. In my func addMonster() { }
The monsters then animated to move from the right hand side, to the left hand side of the screen. Once they are off the screen the opposite side, the sprite gets removed.
在我didMoveToView(view: SKView) { }
的代码部分中,我调用下面的语句来填充屏幕上的怪物。在我func addMonster() { }
的怪物然后动画从右手边移动到屏幕的左手边。一旦它们离开对面的屏幕,精灵就会被移除。
CODE A
代码 A
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addMonster),
SKAction.waitForDuration(1.0),SKAction.
])
))
In the add Mons?er function, i call the following code which moves the Monster across the screen.
在 add Mons?er 函数中,我调用下面的代码来移动怪物在屏幕上。
let actualDuration = random(min: CGFloat(6.0), max: CGFloat(10.0))
let actionMove = SKAction.moveTo(CGPoint(x: -monster.size.width/2, y: actualY), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
monster.runAction(SKAction.sequence([actionMove, actionMoveDone]))
All of the code above is working fine.
上面的所有代码都运行良好。
When the user has killed an X amount of monsters, I want all the other monsters of the screen to disappear and stop spawning.
当用户杀死 X 数量的怪物时,我希望屏幕上的所有其他怪物消失并停止生成。
My questions are, how do I a) Stop CODE A from spawning monsters and b) how do I get any monsters that are on the view, the be removed?
我的问题是,我如何 a) 阻止 CODE A 产生怪物,以及 b) 我如何获得视野中的任何怪物,并将其移除?
Thanks,
谢谢,
Ryann
瑞恩
回答by jonogilmour
When you run the action, use
运行操作时,请使用
monster.runAction(SKAction.sequence([actionMove, actionMoveDone]), withKey: "actionA")
then cancel it with
然后取消它
monster.removeActionForKey("actionA")