Xcode SpriteKit - 删除Sprite并停止动作 - repeatActionForever [英] Xcode SpriteKit - Removing Sprites and stopping an action - repeatActionForever

查看:559
本文介绍了Xcode SpriteKit - 删除Sprite并停止动作 - repeatActionForever的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift和SpritKit的新手并且在我的游戏中遇到了一些问题。

I am new to Swift and SpritKit and having a few issues with my game.

在我的 didMoveToView(查看:SKView){} 我的代码部分我调用下面的语句来填充屏幕上的怪物。在我的 func中,addMonster(){}
然后怪物动画从右侧移动到屏幕的左侧。一旦他们离开屏幕对面,精灵就会被移除。

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.

代码A

    runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock(addMonster),
            SKAction.waitForDuration(1.0),SKAction.
            ])
        ))

在添加Mons†er功能中,我调用以下代码将怪物移动到屏幕上。

In the add Mons†er function, i call the following code which moves the Monster across the screen.

    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.

当用户杀死了X量的怪物时,我想要所有其他的屏幕上的怪物消失并停止产卵。

When the user has killed an X amount of monsters, I want all the other monsters of the screen to disappear and stop spawning.

我的问题是,我如何a)停止产生怪物的代码A和b)如何获得任何怪物在视图上,被删除?

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?

谢谢,

Ryann

推荐答案

当你运行动作时,使用

monster.runAction(SKAction.sequence([actionMove, actionMoveDone]), withKey: "actionA")

then取消它

monster.removeActionForKey("actionA")

这篇关于Xcode SpriteKit - 删除Sprite并停止动作 - repeatActionForever的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆