Change Particle texture on the fly

edited October 2015 in How To...

Hi,

I am currently using the Particles example by Dan Shiffman and I would like to change the Particle texture by keypress to a different PNG. As the PNG is loaded n the setup-phase I can't do this there. Is there a way to switch a texture which has already been loaded (maybe in some internal buffer)? Or should I change the code so that I load all images upfront and the try to adapt the Particle system?

Any help is greatly appreciated, maybe someone has already coded a similar thing?

Thanks, Robo

Answers

  • Definitely load images up front; or possibly in an event listener if you've got a lot of images - though there may be a noticeable delay whilst each one loads. How you swap images depends on your actual implementation: a condition against a global in Particle or your event handler updating each Particle instance, etc.

  • Hello blindfish,

    thanks for the answer, I thought loading upfront is the best way to go. I am currently stuck at figuring out how to switch a shape, I tried the following:

    partSize = random(10,30);
    part2 = createShape();
    part2.beginShape(QUAD);
    part2.noStroke();
    part2.texture(sprite);
    part2.normal(0, 0, 1);
    part2.vertex(-partSize/2, -partSize/2, 0, 0);
    part2.vertex(+partSize/2, -partSize/2, sprite.width, 0);
    part2.vertex(+partSize/2, +partSize/2, sprite.width, sprite.height);
    part2.vertex(-partSize/2, +partSize/2, 0, sprite.height);
    
    part.endShape();
    
    
    // this line below does not work
    part = part2;
    

    It seems that it is not possible to assign a Shape to a new shape. Any suggestions on that?

Sign In or Register to comment.