SKEmitterNode将粒子速度更改为已发射的粒子 [英] SKEmitterNode change particle speed to particles already emitted

查看:70
本文介绍了SKEmitterNode将粒子速度更改为已发射的粒子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改已经发射的粒子的速度?SKEmitterNode具有粒子速度属性,但这仅适用于将来将要发射的粒子.

Is it possible to change the speed of particles already emitted? SKEmitterNode has a particleSpeed property, but this only applies to particles that will be emitted in the future.

作为一个例子,假设我们正在构建一个太空飞船模拟器.想象一下,在星空上方渲染的太空飞船.通过使用SKEmitterNode渲染的粒子文件创建了星空.通过该模拟的飞船从不改变位置.SKEmitterNode由在负x方向上以10的恒定速度移动的粒子组成.为了模拟宇宙飞船的加速,最好能够改变已经发射的粒子的速度.截至目前,如果要更改粒子的速度,则已发射的粒子的速度将比速度更改后正在发射的粒子的速度慢.

As an example, let's say we're building a spaceship simulator. Imagine a spaceship rendered above an a starfield. The starfield was created through a particle file, rendered using SKEmitterNode. The spaceship through this simulation never changes position. The SKEmitterNode is composed of particles that move at a constant speed of 10 in the negative x direction. To simulate the spaceship accelerating, it would be nice to have the ability to change the speed of particles that have already been emitted. As of right now, if the speed of the particles were to change, the particles already emitted will be going at a slower speed than the particles that are being emitted after the speed change.

一种解决方案是定义一个粒子系统,该系统以0速度和0加速度发射例如100 x 100的粒子.该粒子系统将使用较低的zPosition值进行渲染以保留在背景中.为了模拟宇宙飞船的运动,SKEmitterNode的x位置将相对于宇宙飞船的速度进行更改.在场景之外添加了更多SKEmitterNode,然后随着较旧的SKEmitterNode滚下并移除而滚动到视图中.

One solution is to define a particle system that emits particles in a range of, for example, 100 x 100 with 0 speed and 0 acceleration. This particle system would be rendered with a low zPosition value to stay in the background. To simulate spaceship movement, the x position of the SKEmitterNode would change relative to the speed of the spaceship. More SKEmitterNodes are added off of the scene, then scroll into view as the older SKEmitterNodes roll off and are removed.

我已经测试了该解决方案并且它可以工作,但是我很好奇是否有人有一个更优雅的解决方案.

I've tested that solution and it works, but I was curious if anyone has a more elegant solution.

推荐答案

一种可能的解决方法是将粒子添加到单独的层(空SKNode)中.

A possible way around is to add the particles to a separate layer (empty SKNode).

var particleLayer = SKNode()
self.addChild(particleLayer)

var flowEmitter = SKEmitterNode(fileNamed: "yourEmitter.sks")
emitter?.targetNode = particleLayer

然后您可以通过使用SKAction移动该层来更改粒子速度.

Then you can change the particles speed by moving this layer with SKAction.

particleLayer.run(.move(by: CGVector(dx: 100, dy: 0), duration: 5.0))

这篇关于SKEmitterNode将粒子速度更改为已发射的粒子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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