我使用 VCL 中的多态性来实现形状(矩形、平行四边形和六边形)的旋转,使用从初始形状到下一个形状的过渡,以及形状本身。面临以下问题:级联出现以下错误:
它没有显示这发生在哪一行。但是,如果以某种方式快速从第一种形式移动到第二种形式并开始移动图形,则不会出现错误,这可能是什么原因?
我附上了表格的代码(除了最初的代码,那里没有什么有趣的):
unit Unit3;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, ComCtrls, ExtCtrls;
type
TForm3 = class(TForm)
Image: TImage;
Button1: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
public
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
uses unit4;
var
t: single=0.0;
H: THexagon;
S: TSquare;
P: TParall;
Moving:Boolean=true;
procedure TForm3.Button1Click(Sender: TObject);
begin
close;
end;
procedure TForm3.FormActivate(Sender: TObject);
begin
H:=THexagon.Create(250,250,50,Image);
S:=TSquare.Create(475,250,50,Image);
P:=Tparall.Create(700,175,900,275, 150,Image);
Timer1.Enabled:=true;
end;
procedure TForm3.Timer1Timer(Sender: TObject);
begin
H.Move(t);
S.Move(-0.2*t);
P.Move(0.5*t);
t:=t+0.5;
end;
initialization
finalization
H.Free;
S.Free;
P.Free;
end.
以及具有多态性的形式:
Unit unit4;
Interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, ComCtrls, ExtCtrls;
Type
TFigure=Class
private
x, y, halflen, dx, dy: integer;
Image: TImage;
procedure Draw; virtual; abstract;
procedure Rel(t:real); virtual;
public
constructor Create(ax,ay,ah:integer;aImage:TImage);
procedure Move(t:single);
end;
THexagon=Class(TFigure)
private
r: integer;
procedure Draw; override;
public
constructor Create(ax, ay, ar: integer; aimage: timage);
end;
TSquare=Class(TFigure)
private procedure Draw; override;
end;
TParall=Class(TFigure)
private
h:integer;
x1, y1: integer;
procedure Draw;override;
public
constructor Create(ax,ay, ax1, ay1, ah: integer; aImage:TImage );
end;
Implementation
Constructor TFigure.Create;
Begin
inherited Create;
x := ax;
y := ay;
halflen := ah;
Image := aImage;
End;
Procedure TFigure.Rel;
Begin
dx := 0;
dy:=round(25*sin(t));
End;
Procedure TFigure.Move;
Begin
Image.Canvas.Pen.Color:=clWhite;
Draw;
Image.Canvas.Pen.Color:=clBlack;
Rel(t);
Draw;
End;
constructor THexagon.Create;
begin
inherited Create(ax,ay,ar,aImage);
r := ar;
end;
procedure THexagon.Draw;
var
p:array[1..7] of TPoint;
i:integer;
a:real;
begin
a:=0;
for i:=1 to 7 do begin
p[i].X:=x+round(r*cos(a));
p[i].Y:=y+dy-round(r*sin(a));
a:=a+pi/3;
end;
p[7]:=p[1];
with Image.Canvas do begin
brush.Style:=bsClear;
Polygon(p);
end;
end;
Procedure TSquare.Draw;
Begin
image.Canvas.rectangle(180,100+dy,230,150+dy);
End;
Constructor TParall.Create;
Begin
inherited Create(ax, ay, ah, aImage);
h := ah;
x1:= ax1;
y1:= ay1;
End;
Procedure TParall.Draw;
Begin
with image.Canvas do begin
moveto(x, y + dy);
lineto(x+h,y + dy);
lineto(x1, y1 + dy);
lineto(x1 - h, y1 + dy);
lineto(x, y + dy);
end;
End;
end.
你
Timer1
最初Enabled
。Enabled
把它放在“对象检查器” -efalse
中。或转移在
TForm3.FormCreate
.