一些Delphi错误

时间:2012-09-19 13:32:01

标签: delphi oop delphi-7

我尝试制作应该在Unit中的Ball类,然后我需要使用Canvas在表单上绘制Ball。实际上我以前从来没有在Delphi中尝试过OOP(所有我都是在Pascal学校简单的练习)所以我遇到了很多问题。哦。 所以,这里的代码 单元与Ball类

unit Unit2;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  MyPoint = record
    x, y: integer;
  end;

  Ball = class
    Pos:MyPoint;
    Vel:MyPoint;
    Rad:integer;
    Can:TCanvas;
    procedure BallCreate(crd, spd:MyPoint; Sender: TObject);
    procedure BallDraw(Sender: TObject);
    procedure BallMove();
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  posX, posY, speedX, speedY, radius:Integer;

implementation
procedure Ball.BallMove;
begin
   if((posX + radius > 700) or (posX - radius  < 0)) then speedX:= (-speedX);
   if((posY + radius > 500) or (posY - radius < 0)) then speedY:= (-speedY);
    posX:=posX+speedX;
    posY:=posY+speedY;
end;

procedure Ball.BallCreate(crd, spd:MyPoint; Sender: TObject);
begin
  Vel.x:=3;
  Vel.y:=3;
  pos.X:=crd.x;
  pos.Y:=crd.y;
  radius:=30;
end;



procedure Ball.BallDraw(Sender: TObject);
begin
with Can do
 begin
  brush.Style:=bsSolid;
  brush.Color:=clRed;
  ellipse((pos.X-radius),(pos.Y-radius),(pos.X+radius),(pos.Y+radius));
 end;
end;

end.

单位与表格

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Unit2;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  x1,y1,x2,y2,x,y:integer;
  posX, posY, speedX, speedY, radius:Integer;
  f:boolean;
  obj:Ball;
  p:MyPoint;
  s:MyPoint;
implementation

{$R *.dfm}
{procedure TForm1.BallMove;
begin
   if((posX + radius > ClientWidth) or (posX - radius  < 0)) then speedX:= (-speedX);
   if((posY + radius > ClientHeight) or (posY - radius < 0)) then speedY:= (-speedY);
    posX:=posX+speedX;
    posY:=posY+speedY;
end;              }

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Enabled:=false;
  Timer1.Interval:=5;
  p.x:= Round(ClientWidth/2);
  p.y:= Round(ClientHeight/2);
  s.y:=3;
  s.x:=s.y;
  obj.BallCreate(p,s,Sender);

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if not f then
 begin
  Timer1.Enabled:=true;
  Button1.Caption:='Ñòîï';
  f:=not f;
 end
else
 begin
  Timer1.Enabled:=false;
  Button1.Caption:='Ïóñê';
  f:=not f;
 end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 obj.BallDraw(Sender);
 obj.BallMove;
end;

end.

当我尝试运行它时会说

raised exception class EAccessViolation with message 'Access violation at address 0044DE7B in module Project1.exe. Write of address 000000C'

并且在代码中,这些笔划突出显示为红色

Vel.x:= 3; 可以做

我不明白什么是错的,以及我如何正确地宣布和使用Canvas。也许你在Delphi的Canvas单元中有一些OOP的例子?

1 个答案:

答案 0 :(得分:1)

你宣布了一个Can:TCanvas;变量但它不会在任何地方创建。

你可以使用Main表单画布,因为你应该在Ball构造函数中将它传递给Ball for exmaple,如:

TBall = class
...
public
constructor Create(crd, spd:MyPoint; ACanvas:TCanvas);
....
implementation
...
constructor TBall.Create(crd, spd:MyPoint; ACanvas:TCanvas);
begin
  Can := ACavas;
...

然后,你没有正确创建Ball的实例:

obj.BallCreate(P,S,发件人);

创建一个实例,你必须调用类构造函数,如

obj := TBall.Create(crd, spd, Self.Canvas);

顺便说一句,Ball之前的“T”只是在Delphi中命名一个类的惯例