Hi...
I did create a class to do multiple explosions, which is working fine to me!
Code:
TParticleExplosions = class
private
FExplosionsList: TObjectList;
public
constructor create;
destructor destroy;
procedure Add(Tipo, X, Y: Integer);
procedure draw;
procedure update;
class function GetInstance: TParticleExplosions;
class var
FInstance: TParticleExplosions;
end;
{ TParticleExplosions }
constructor TParticleExplosions.create;
begin
FExplosionsList := TObjectList.Create;
FExplosionsList.OwnsObjects := True;
end;
destructor TParticleExplosions.destroy;
begin
FExplosionsList.Free;
FExplosionsList := nil;
FInstance := nil;
end;
procedure TParticleExplosions.Add(Tipo, X, Y: Integer);
var
temp: TXParticleSystem;
PSS: TXParticleSystemSettings;
Stream: TFileStream;
begin
try
temp := TXParticleSystem.Create(PSS);
case Tipo of
1: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\gota-spark.pss', fmOpenRead);
2: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\book-spark.pss', fmOpenRead);
3: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\fire-spark.pss', fmOpenRead);
4: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\green-spark.pss', fmOpenRead);
5: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\red-spark.pss', fmOpenRead);
6: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\black-spark.pss', fmOpenRead);
7: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\pearl-spark.pss', fmOpenRead);
8: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\cristal-spark.pss', fmOpenRead);
9: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\spawn.pss', fmOpenRead);
10: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\mapexplosion.pss', fmOpenRead);
11: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\forcefield-orange.pss', fmOpenRead);
12: Stream := TFileStream.Create(ExtractFilePath(Application.ExeName) + 'Images\forcefield-blue.pss', fmOpenRead);
end;
if (not temp.SettingsLoadFromStream(Stream)) then ShowMessage('Unable to load ParticleSystem settings');
Stream.Free();
temp.Texture := Main.Images[2];
temp.AddEmitters([point(0, 0)]);
temp.StartAt(x, y);
FExplosionsList.Add(temp);
except
Temp.Free();
end;
end;
procedure TParticleExplosions.draw;
var i: integer;
temp: TXParticleSystem;
begin
for i := 0 to FExplosionsList.count - 1 do
begin
temp := (FExplosionsList.items[i] as TXParticleSystem);
if temp.ParticlesAlive > 0 then
temp.Render(Main.Canvas);
end;
end;
class function TParticleExplosions.GetInstance: TParticleExplosions;
begin
if FInstance = nil then
begin
FInstance := TParticleExplosions.Create;
end;
Result := FInstance;
end;
procedure TParticleExplosions.update;
var i: integer;
temp: TXParticleSystem;
FDeadList: TObjectList;
begin
try
FDeadList := TObjectList.Create;
FDeadList.OwnsObjects := False;
for i := 0 to FExplosionsList.count - 1 do
begin
temp := (FExplosionsList.items[i] as TXParticleSystem);
temp.Update(Ceil(1000 / Main.Timer.Speed));
if (temp.ParticlesAlive <= 0) and (temp.age < 1.9) then
begin
FDeadList.Add(temp);
end;
end;
for i := 0 to FDeadList.count - 1 do
begin
temp := (FDeadList.items[i] as TXParticleSystem);
FExplosionsList.Remove(FDeadList.items[i]);
end;
finally
FDeadList.Free;
end;
end;
Here is my old code that was with randoms "access violations"....
Code:
GameParticleManager: TXParticleManager;
GameParticleSystem: TXParticleSystem;
FGotaExplosion: TXParticleSystemSettings;
GameParticleManager := TXParticleManager.Create;
GameParticleManager.Canvas := Canvas;
FGotaExplosion := LoadParticleFromASDB('gota-explosion.pss');
GameParticleManager.Update(FDelta); //In TimerProcess
GameParticleManager.Render; //In AsphyreRender
GameParticleSystem := GameParticleManager.SpawnPS(Main.FGotaExplosion, Main.Images[2], X, Y); //To Spawn a new Explosion
Using this way, it was happenings random access violatios in XParticles code.