PDA

View Full Version : Image change on collision



Wizard
14-12-2006, 06:20 AM
Hi there.

I'm busy with a game using delphiX and would like to change the image to an explosion when my bullet/bomb hits my enemies...I had a look at the DelphiX Shoot sample and tried the code but it has no effect.

Here's my Bullet.DoCollision:


procedure TBullet.DoCollision(Sprite: TSprite; var Done: Boolean);
begin
If (Sprite is TEnimy) then
begin
Sprite.Collisioned := False;
Sprite.Dead;
Enimies := Enimies -1;
Form1.DXWaveList1.Items.Find('hit').Play(False);
dead;
// Image change
Image := Form1.DXImageList1.Items.Find('Explode');
Width := Image.Width;
Height := Image.Height;

Dead;
Points := Points + 1000;
Done := False;
end;
if (Sprite is TDisk) then
begin
Sprite.Collisioned := False;
Sprite.Dead;
Form1.Disk_No := Form1.Disk_No -1;
Form1.DXWaveList1.Items.Find('hit').Play(False);
// Image change
Image := Form1.DXImageList1.Items.Find('Explode');
Width := Image.Width;
Height := Image.Height;

dead;
Points := Points + 2000;
SpaceMines := SpaceMines + 1;
if collisioned = true then
Form1.NewDisk;
Done := False;
end;
end;

Any help/suggestions?

FNX
14-12-2006, 08:42 AM
Hello,
if you call "Dead;" just after switching images you won't have any
effect because the sprite is destroyed immediately after.

If you want to show an animation you have to switch images, get the
pattern count and start animating the new image. When AnimPos reaches
PatternCount of the explosion image you actually call Dead.

Of course you have to disable collision detection when the sprite is
exploding so a good way is to set a boolean like "isDying" where if true
you won't check for collisions anymore, or something like that.

Hope this make sense ;)

Wizard
14-12-2006, 10:11 AM
Thanks :-) The call to dead was the problem. I took out dead and it works 100% :-)

Well not 100%, now the bullet lives on and kills other enemies.....I have to think about the correct solution...



If (Sprite is TEnimy) then
begin
Sprite.Collisioned := False;
Sprite.Dead;
Enimies := Enimies -1;
Form1.DXWaveList1.Items.Find('hit').Play(False);
// Image change
Image := Form1.DXImageList1.Items.Find('Explode');
Width := Image.Width;
Height := Image.Height;
AnimCount := Image.PatternCount;
AnimLooped := true;
AnimSpeed := 15/1000;
AnimPos := 0;
Points := Points + 1000;
Done := False;
end;

jasonf
14-12-2006, 10:38 AM
Normally what happens is, the bullet dies but before it does, it tells the engine to apply an effect at the location where the bullet hit.

If you build in a generic effects system, you'll be able to make your bullets do all sorts of things when they die and the explosions can live on long after the bullet has died.

I'd personally create an explosion sprite and a bullet sprite. They both have quite different tasks to do.

You should generally make a class do *one thing* and do it well.

If you wanted to make other things explode in the same way, if you put the animation and sound code in the bullet, you'd have to copy this code to other objects. This common code would be better placed in another class.