cocos2d starting particles from a specific time in the future

拜拜、爱过 提交于 2020-01-04 01:44:08

问题


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


回答1:


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!




回答2:


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

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



回答3:


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




回答4:


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.



回答5:


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?



来源:https://stackoverflow.com/questions/6623611/cocos2d-starting-particles-from-a-specific-time-in-the-future

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!