【发布时间】:2012-09-19 13:32:00
【问题描述】:
我尝试制作应该在 Unit 中的 Ball 类,然后我需要使用 Canvas 在表格上绘制 Ball。实际上,我以前从未在 Delphi 中尝试过 OOP(我记得只是在学校里用 Pascal 进行的简单练习),所以我遇到了很多问题。哦。
所以,这里的代码
球类单位
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 内容的示例?
【问题讨论】:
-
"calss" -> 你可以复制和粘贴文本:用鼠标标记文本,按 ctrl+c,转到你想要的地方,按 ctrl+v
-
对不起。实际上这是错误窗口,我无法做出那样的事情,所以我决定输入它
-
您正在声明一个 TCanvas 变量并尝试在没有实例化它的情况下使用它。你需要事先
Can := TCanvas.Create。完成后不要忘记释放它。 'Ball' 也一样,在尝试调用 'BallCreate' 之前您需要obj := Ball.Create。 -
你昨天问了几乎相同的困惑问题。请不要把我接下来说的话弄错了。我想帮忙。您的根本问题是您不了解有关 Delphi 的最基本事实。尝试绘画和制作动画是相对先进的概念,当然是从您目前所处的位置开始的。你需要找到最基本的 Delphi 教程并掌握它。如果您不准备这样做,那么我们无法帮助您。如果你在学校学习这个,当然课程解释了基础知识。再次咨询您的课程资料。
-
@DanilGholtsman 表单的
OnPaint很好。这也有效。当您只想绘制表单的子区域时,通常会使用绘制框。表格OnPaint允许您在整个表格表面上绘画。关键是这是一个真实的TCanvas,它附加到系统绘制事件的处理程序。