Добрый вечер, столкнулся с проблемой. Стал писать программу на подобии салюта и чего то не догоняю. Надо писать на ООП. Использую паскалевский TCollection. Проблема в том, что при выходе шарика за границы экрана я его хочу удалить. Мне выдает ошибку 216, когда я пытаюсь удалить элемент "c^.Free(t)". Уже запарился, не понимаю в чем проблема и как это исправить. И выполняется ли дестрактор при вызове коллекшионской процедуры Free?
UNIT:
Код:
Unit SaluteRockets;
interface
uses graph,objects,Wincrt;
type TUnilocation = object(Tobject)
x,y,dx,dy:real;
color:word;
Constructor Init(InitX, InitY,InitColor:word);
Destructor Done; virtual;
Function GetX: Real;
Function GetY: Real;
Procedure PutX(InX:Real);
Procedure PutY(InY:Real);
Function GetColor: word;
Procedure PutColor;
Procedure Show; virtual;
Procedure Hide; virtual;
end;
PRocket = ^TRocket;
Trocket = object(Tunilocation)
Radius: integer;
Constructor Init(initx,inity:real;InitRadius:integer);
Function GetRadius: integer;
Procedure Show; Virtual;
Procedure Hide; Virtual;
destructor done; virtual;
end;
implementation
{TUnilocation methods' realization}
Constructor Tunilocation.Init(Initx,InitY,Initcolor:word);
begin
end;
Destructor Tunilocation.Done;
begin
end;
Function TUnilocation.GetX:Real;
begin
Getx:=x;
end;
Function TUnilocation.GetY:Real;
begin
Gety:=y;
end;
Procedure TUnilocation.PutX(inx:Real);
begin
x:=inx;
end;
Procedure TUnilocation.PutY(iny:Real);
begin
y:=iny;
end;
Function TUnilocation.GetColor:word;
begin
GetColor:=color;
end;
Procedure TUnilocation.PutColor;
begin
color:=random(15)+1;
end;
Procedure TUnilocation.Show;
begin
abstract;
end;
Procedure TUnilocation.Hide;
begin
abstract;
end;
{Trocket methods realization}
Constructor TRocket.
Init(initx,inity:real;InitRadius:Integer);
begin
x:=initx;
y:=inity;
dx:=(Random(6)-3)*0.05;
dy:=(Random(5)+1)*0.05;
Radius:=initRadius;
PutColor;
end;
Function Trocket.GetRadius:integer;
begin
GetRadius:=radius;
end;
Procedure Trocket.Show;
begin
SetColor(color);
Circle(Round(x),Round(y),radius);
end;
Procedure Trocket.Hide;
begin
SetColor(black);
Circle(Round(x),Round(y),radius);
end;
Destructor Trocket.Done;
begin
end;
end.
PROGRAM:
Код:
program SalutOOP;
uses Graph,Objects,SaluteRockets,wincrt;
Procedure MovingCollection(C:Pcollection);
procedure MovingProc(T:procket);
begin
t^.putx(t^.dx+t^.getX);
t^.Puty(-t^.dy+t^.getY);
t^.Show;
end;
procedure DelHide(T:Procket; C:Pcollection);
var k:integer;
begin
//Delay(3);
t^.hide;
if (t^.y<0) or (t^.x<0) or (t^.x>getmaxX) then c^.Free(t);{ОШИБКА 216}
// if t^.y>(GetmaxY div 2)+(GetmaxY div 4) then
end;
begin
C^.ForEach(@Movingproc);
C^.ForEach(@DelHide);
end;
const nn=5;
var gd,gm,i,tmp:integer;
Rockets : pCollection;
dx,dy:Real;
begin
randomize;
gd:=detect;
InitGraph(gd,gm,'');
Rockets:=new(Pcollection, Init(10,5));
for i:=1 to pred(nn) do begin
tmp:=random(15)+1;
Rockets^.Insert(New(Procket, Init(GetmaxX/3+Random(GetmaxX div 3),GetmaxY,Random(3)+2)));
end;
Repeat
MovingCollection(rockets);
until keypressed;
Readkey;
end.
Собственно вот код. Ошибка в процедуре DelHide. Не понимаю в чем ошибка. Надеюсь на помощь.