ios - SpriteKit |连击爆破虫

标签 ios sprite-kit swift4 xcode9 2d-games

我目前正在对我的 Space Shooter 进行收尾工作。在这个射击游戏中,你击中了小行星,击中时会播放爆炸动画。在游戏的后期,当它变得更快并且 2 颗小行星同时出现在屏幕上时,如果它们同时爆炸,那么第一次爆炸功能将不会完成,而在屏幕上留下第二次爆炸之前显示的最后一张图像。这最终会堆积起来,直到屏幕上的节点太多以至于游戏崩溃,尽管它看起来很糟糕。

我当前的爆炸函数代码是:

func spawnEpxlotion(spawnPosition: CGPoint){

    let explosion1 = SKTexture(imageNamed: "explosion-1")
    let explosion2 = SKTexture(imageNamed: "explosion-2")
    let explosion3 = SKTexture(imageNamed: "explosion-3")
    let explosion4 = SKTexture(imageNamed: "explosion-4")
    let explosion5 = SKTexture(imageNamed: "explosion-5")
    let explosion6 = SKTexture(imageNamed: "explosion-6")
    let explosion7 = SKTexture(imageNamed: "explosion-7")
    let explosion8 = SKTexture(imageNamed: "explosion-8")
    let explosion9 = SKTexture(imageNamed: "explosion-9")

    let animateExplosion = SKAction.sequence([
        SKAction.wait(forDuration: 0, withRange: 0),
        SKAction.animate(with: [explosion1,explosion2,explosion3,explosion4,explosion5,explosion6,explosion7,explosion8,explosion9], timePerFrame: 0.03)])

    explosion = SKSpriteNode(texture: explosion1)
    explosion.position = spawnPosition
    explosion.setScale(2)
    self.addChild(explosion)
    explosion.run(explosionSound)
    explosion.run(animateExplosion, completion : {self.explosion.removeFromParent()})
}

在出现双重打击之前,这一切都像魅力一样有效。我尝试了一些可能的修复方法,但都没有成功。他们中的大多数只是一起关闭了爆炸功能,这显然不是目标。

如果有人可以帮助我,那会很酷。也许有一种方法可以强制完成一个函数,无论它是否同时被调用两次。

最佳答案

您的explosion 变量没有在您的函数中声明,这表明它必须是一个实例变量。当您调用 spawnEpxlotion 而前一个动画仍在运行时,您将到达 explosion = SKSpriteNode(texture: explosion1) 行,您将吹走之前的节点运行动画。使 explosion 成为局部变量。祝你好运。

关于ios - SpriteKit |连击爆破虫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49445659/

相关文章:

objective-c - Restkit - didLoadObjects 正在接收一个空数组

ios - 如何消除不同背景颜色的 UIPickerView 组件之间的差距

ios - Swift 中图像/剪影的轮廓路径

Swift 中带有普通和粗体文本标签的 iOS 选择器 View

ios - avqueueplayer 在后台模式下播放问题

ios - Sprite Kit 是在后台渲染,还是在主线程渲染?它是如何工作的?

ios - 对象在不应该发生碰撞时相互碰撞(SpriteKit)

swift - + 不可用 : Please use explicit type conversions or Strideable methods for mixed-type arithmetics

Swift4 中的 JSONDecoder 来自嵌套 JSON

iOS swift 4 :How to perform encryption/decryption with DES-ECB-PKCS5Padding?