PDA

View Full Version : ExplodeSprite


zehlein
11-05-2006, 09:14 PM
At the first sight the use of the particle engine (look at the nice explosions in the particles demo!) seems a bit confusing. For use in my app I put it all together in one procedure and like to share it with all who might be interested.

proc ExplodeBall(sprite$)
// code by Alain Deschenes (c) 2004, ArianeSoft, modified by Jens Zeh
// Create particle "parent" sprite for explosion
Particle$ = NewSprite(NULL);
// Make particle sprite a little rectangle
SetSpriteWidth(Particle$, 3);
SetSpriteHeight(Particle$, 3);
// Set particle's alpha blending value
SetSpriteAlpha(Particle$, 255);
// Set particle's color (randomly)
SetSpriteColor(Particle$, G_RGB(random(155) + 100, random(155) + 100, random(155) + 100));
// Create between min 75 max 150 particles for explosion
For (i$, 1, random(75) + 75)
// Set particle's speed x-direction
Speed$ = (Random(99) + 1) * 0.02;
RandomSet(velx$, Speed$, -Speed$);
// Set particle's speed y-direction
Speed$ = (Random(99) + 1) * 0.02;
RandomSet(vely$, Speed$, -Speed$);
// Create particle, place center of explosion in the middle of the exploding sprite
NewParticle(s$, Particle$, SpriteX(sprite$) +SpriteWidth(sprite$)/2, SpriteY(sprite$) + SpriteHeight(Sprite$)/2, 0, 0, velx$, vely$, random(100), false, SpriteAlpha(Particle$), false,true);
end;
end;

As a parameter the procedure takes the andle of the sprite to explode.

All you have to do is put a
RenderParticles(0);
in the WM_Paint event (right beneath the RenderSprites call) and put a
ProcessParticles(0);
in the WM_Timer event (right beneath the WM_Timer:)

This procedure does not delete the sprite. If you wish so do it by hand with
DelSprite(sprite$).

The explosion made is pretty simple, I would like to add a feature later that breaks the sprite in fractions and throws them around.
Enjoy! And let me know if it works for you!

kornalius
11-06-2006, 06:07 PM
I have planned to finaly include an Explode internal function in 1.1. It will create new sprites that you can just then assign to particles.

Explode(Sprite$, 10, 10, Spr$);
ForEach(Spr$)
NewParticle(1, Spr$, ...);
end;

This will break the sprite (Sprite$) in 10 x 10 sprites create 100 new sprites with their handles stored in list variable Spr$.

zehlein
11-06-2006, 08:13 PM
ah, that woul be very helpful, kornalius. thx in advance for this!