ios - 以编程方式实现 SKEmitterNode 的 'dragged' 效果(Swift)

标签 ios swift sprite-kit

我有一个 SKEmitterNode 用于在我的游戏中显示射弹。发射器跟随子弹的轨迹,这是通过以下代码实现的:

let bullet = SKSpriteNode(imageNamed: "bullet") // "bullet" is a blank png
// ...Bullet properties...
self.addChild(bullet)

var actionArray = [SKAction]() 
actionArray.append(SKAction.move(to: endPoint, duration: 1))
actionArray.append(SKAction.removeFromParent())

bullet.run(SKAction.sequence(actionArray))

let emitter = SKEmitterNode(fileNamed: "bulletEmitter.sks")
// ...Emitter node properties... (same position as bullet)    
self.addChild(emitter!)

var emitterActionArray = [SKAction]()         
emitterActionArray.append(SKAction.move(to: endPoint, duration: 1))
emitterActionArray.append(SKAction.removeFromParent())

emitter!.run(SKAction.sequence(emitterActionArray))

如上所述,发射器跟随子弹并在子弹移动时进行动画处理。但是,它的动画不受其速度的影响。在 bulletEmitter.sks 文件中,我可以四处拖动鼠标,发射器将从它的来源处留下一条“轨迹”。看这两张图:

Emitter while stationary - this is how the emitter looks like when it is moving with the bullet

上图:静止时的发射器 - 这是发射器随子弹移动时的样子

Trail left by 'dragging' the emitter - I would like the emitter to look like this while moving

上图:通过物理“拖动”.sks 文件中的发射器留下的轨迹 - 我希望发射器在移动时看起来像这样

有没有其他方法可以将发射器连接到子弹上,使其在移动时如第二张图片所示?

谢谢。

最佳答案

由于子弹是一个继承自SKNodeSKSpriteNode,您可以将发射器作为子项添加到子弹中,这样不仅可以获得拖尾效果你正在寻找,但也让你不必给发射器一个 Action 序列来匹配子弹。它只会沿着您为子弹设置的相同轨道移动。

(如果您确实修改了发射器的位置以调整所需的效果,请记住它现在位于其父项目符号的坐标系中,而不是场景本身。)

bullet.addChild(发射器)

关于ios - 以编程方式实现 SKEmitterNode 的 'dragged' 效果(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52687212/

相关文章:

ios - 如何让 SpriteKit Playgrounds 在 Xcode 8 beta 中工作?

ios - 更改 UILabel 文本仅适用于 viewDidLoad()

android - 如何根据操作系统在短信中添加指向 Android 或 iOS 应用商店的链接?

ios - 如何将我的 UILabel 与系统时间同步?

ios - 我什么时候应该重新加载数据()?

ios - clang : error: linker command failed with exit code 1 (use -v to see invocation) when doing unit test on xcode

ios - Swift - 为 UICollectionView 中的每个单元格创建单独的 UIView

swift - 如何在 Swift 中执行curl 请求?

ios - RXSwift 未订阅主线程

ios - 当应用程序进入后台时,如何在 SpriteKit 中正确暂停?