0

我尝试制作应该在 Unit 中的 Ball 类,然后我需要使用 using 在表单上绘制 Ball Canvas。实际上,我以前从未在 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 的例子?

4

1 回答 1

1

你声明了一个 Can:TCanvas; 变量,但它不是在任何地方创建的。

您可以使用 Main 表单画布,为此您应该在 Ball 构造函数中将其传递给 Ball,例如:

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

Then, you are not properly creating and instance of Ball:

obj.BallCreate(p,s,Sender);

to create an instance you have to call the class constructor like

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

By the way the "T" before Ball is just a convention to name a class in Delphi

于 2012-09-19T20:25:08.617 回答