cocos2d 从未来特定时间开始粒子

发布于 2024-11-19 05:30:14 字数 301 浏览 6 评论 0原文

我正在开发一个基于 cocos2d 的具有太空背景的应用程序,其中我利用 CCQuadParticleSystem 来制作闪烁的星星。我用 ParticleDesigner 生成了这个粒子系统。一旦我加载粒子系统,代表星星的白点就开始出现在背景中,过了一会儿它们就会消失,这样,在粒子系统达到状态状态几秒钟后,就会出现充满星星的夜空。

我的问题是,我想知道是否有一种方法可以使粒子系统从未来的特定时间(例如 t0 = 3 秒)开始,这样我就不必等待所有的开始都闪烁。

我希望我已经清楚地解释了这个问题,

提前谢谢你

安德里亚

I am developing a cocos2d based app with a space background in which I am exploiting a CCQuadParticleSystem to make blinking stars. I have generated this particle system with ParticleDesigner. As soon as I load the particle system white dots representing stars start appearing in the background and after a while they fade out so that, after few seconds in which the particle system reaches the regime state, a night sky full of stars comes out.

My problem is that I would like to know if there is a way to make the particle system starting from a specific time in the future (for instance t0 = 3sec) so that I do not have to wait to have all the starts blinking.

I hope I have clearly explained the problem

thank you in advance

Andrea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

难如初 2024-11-26 05:30:14

我这样做了,结果完全按照我想要的方式工作。

for (int i = 0 ; i < 300 ; i++)
    [emitter update:.1];

I did this and it worked exactly the way I wanted it to.

for (int i = 0 ; i < 300 ; i++)
    [emitter update:.1];
北方的巷 2024-11-26 05:30:14

你有没有尝试过

id actions = [CCSequence actions:[CCDelayTime actionWithDuration:3.0f],
              [CCCallFunc actionWithTarget:self selector:@selector(onStallComplete)],
              nil];
[self runAction:actions];

,好吧,虽然它可能滥用了 API 的初衷,但很有用
如果您有多个此类延迟反应,请注意 onStallComplete 中的重新进入。

注意:SO 新手,希望代码片段看起来正确

Have you tried a

id actions = [CCSequence actions:[CCDelayTime actionWithDuration:3.0f],
              [CCCallFunc actionWithTarget:self selector:@selector(onStallComplete)],
              nil];
[self runAction:actions];

ok, granted it is probably abusing the original intent of the API's , but is useful
watch for re-entrancy in onStallComplete if you have multiple such delayed reactions.

note: newbie at SO, hope the code snippet comes out looking right

反目相谮 2024-11-26 05:30:14

我假设您在游戏循环中使用某种 updateWithDelta: 方法来更新粒子。如果您希望粒子在一定时间间隔后启动,请创建自己的计时器。

编辑:根据您下面的评论,我的方法仍然很好,只是需要一些调整。您只需删除粒子系统上的 updateWithDelta: 方法中的条件即可。这样,它仍然会更新这 3 秒,但不会渲染,因此看起来像您所描述的那样。

在 .h 文件中:

BOOL particleShouldUpdate;
float particleTimer;

在您的 init 方法中:

particleShouldRender = NO;
particleTimer = 3.0f;

在您的 updateWithDelta: 方法中:

if(!particleShouldRender){
  particleTimer -= delta;
  if(particleTimer < 0){
    particleShouldRender = YES;
  }
}
// update your particle.

最后,在您的渲染方法中:

if(particleShouldRender){
  // render your particle.
}

注意 从这一点开始,如果你想停止渲染,只需要像init方法中那样重置2个变量,就会出现同样的效果。

EDIT2:经过进一步说明,我们只需要调整粒子的 init 方法即可。我将在这里做出两个假设,您只需稍微更改它们即可满足您的需求。假设您的更新周期为每秒 60 帧,最小粒子寿命为 1.01,并且您希望在开始游戏之前有 3 秒的更新。然后在 init 方法中,尝试:

for(float delta = 0.0f; delta < 3.0f; delta += (1/60)){
  [particle updateWithDelta:(float)(1/60)];
}

这将像平常一样更新您的粒子,但不会在每个间隔以及其他任何内容更新之前进行渲染。或者,如果您担心更新粒子时的速度,您可以尝试:

for(int i = 0; i < 3; i++){
  [particle updateWithDelta:1];
  [particle updateWithDelta:0.02];
}

这会更快,但可能会出现一些问题,具体取决于您的粒子参数。

EDIT3:所以进一步研究一下,cocos2D 出于某种原因不允许你这样做。我在网上发现了一个类似的问题,他们建议你玩 < code>posVar 和 speed 以使它们在您过渡到场景时足够大,并且在完全过渡到场景后,将这些值重置为正常。您可能想尝试一下!

希望有帮助!

I assume you are using some kind of updateWithDelta: method in your game loop in order to update the particles. If you want the particles to start after a certain interval, make your own timer.

Edit: Based on your comment below, my method is still good, it just needs some tweaking. You need only remove the condition in the updateWithDelta: method on the particle system. That way, it will still update for those 3 seconds, but will not render, and therefore look the way you are describing.

In the .h file:

BOOL particleShouldUpdate;
float particleTimer;

In your init method:

particleShouldRender = NO;
particleTimer = 3.0f;

In your updateWithDelta: method:

if(!particleShouldRender){
  particleTimer -= delta;
  if(particleTimer < 0){
    particleShouldRender = YES;
  }
}
// update your particle.

Finally, in your render method:

if(particleShouldRender){
  // render your particle.
}

Note that from this point, if you want to stop it rendering, you need only reset the 2 variables like as in the init method, and the same effect will occur.

EDIT2: Upon further clarification, we only need to adapt the init method of your particle. I will make 2 assumptions here, and you need only change them slightly to fit your needs. Suppose that your update cycle is 60 frames per second, the minimum particle lifespan is 1.01, and that you want 3 seconds of updates before you start the game. Then in the init method, try:

for(float delta = 0.0f; delta < 3.0f; delta += (1/60)){
  [particle updateWithDelta:(float)(1/60)];
}

This will update your particle like it normally would, but without rendering at each interval, and before anything else gets updated. Alternatively, if you are worried about speed when updating your particle, you can try:

for(int i = 0; i < 3; i++){
  [particle updateWithDelta:1];
  [particle updateWithDelta:0.02];
}

This will be faster, but may have a few issues depending on your particles parameters.

EDIT3: So looking into this further, cocos2D does not let you do this for some reason. I found a similar question online to this, and they suggested you play with the posVar and speed to make them large enough while you are transitioning into the scene, and once you have fully transitioned into the scene, reset the values to normal. You may want to give that a try!

Hope that Helps!

岁月静好 2024-11-26 05:30:14

我认为没有办法让你的粒子系统快进 3 秒到未来。或者,我可以根据您的情况想象两种不同的解决方案:

  1. 将粒子加载到另一个场景后面的场景(例如空的黑色场景)。 3 秒后切换到具有现在漂亮的粒子效果的场景。如果用户需要等待 3 秒,并且您只是不希望他们在一切都混在一起时看到粒子系统,或者如果您在带有粒子系统的场景之前有另一个场景,那么这可能会起作用。
  2. 记录粒子系统,将其存储在文件中,然后在场景中重播。通过记录,我的意思是存储每个粒子的位置和颜色。缺点是,它每次看起来都一样,如果你想运行它比你录制的时间更长,你需要确保循环重播它仍然看起来不错。

I do not think there is a way to fast forward your particle system 3 seconds into the future. Alternatively I can imagine two different solutions depending on your circumstances:

  1. Load the scene with the particle behind another scene (e.g. an empty black scene). After 3 seconds switch to the scene with the now nice looking particle effect. This could work f it is ok for you that the user needs to wait for 3 seconds and you only do not want them to see the particle system while everything is clunked together or if you have another scene before the scene with the particle system anyway.
  2. Record the particle system, store it in a file then replay it in your scene. With recording I mean store the position and color of each of your particles. The drawback is, that it will look the same everytime and if you want to run it longer than what you recorded you need to make sure replaying it in a loop still looks good.
终难遇 2024-11-26 05:30:14

我想不出直接实现这一点的方法,但是你可以尝试这样的方法作为解决方法吗?恐怕由于其他错误,我还无法对此进行测试,但正在努力。

{ 
    ...

    //This attempts to make 3 seconds pass 100 times quicker
    [[CCScheduler sharedScheduler] setTimeScale:100];
    [self schedule:@selector(cancelIncreasedTimeScale) interval:3];

    ...
}


int numberOfTimesCancelled = 0;
-(void) cancelIncreasedTimeScale
{
    numberOfTimesCancelled ++;
    //Two because the scheduler is also called straight away? Remove if it's only called after waiting an initial (3/100) seconds
    if (numberOfTimesCancelled == 2) {
        [self unschedule:@selector(cancelIncreasedTimeScale)];
        [[CCScheduler sharedScheduler] setTimeScale:1];
    }
}

我真正担心的问题是场景中的其他项目的运行速度也会快 100 倍。这是一个问题吗?

I can't think of a way to implement this directly, but could you try something like this as a workaround? I'm afraid I haven't been able to test this yet due to other errors, but working on it.

{ 
    ...

    //This attempts to make 3 seconds pass 100 times quicker
    [[CCScheduler sharedScheduler] setTimeScale:100];
    [self schedule:@selector(cancelIncreasedTimeScale) interval:3];

    ...
}


int numberOfTimesCancelled = 0;
-(void) cancelIncreasedTimeScale
{
    numberOfTimesCancelled ++;
    //Two because the scheduler is also called straight away? Remove if it's only called after waiting an initial (3/100) seconds
    if (numberOfTimesCancelled == 2) {
        [self unschedule:@selector(cancelIncreasedTimeScale)];
        [[CCScheduler sharedScheduler] setTimeScale:1];
    }
}

The issue I do worry about is the other items on your scene would also run 100 times faster. Is that an issue?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文