2012-09-19 74 views
0

我嘗試製作類球應該在單位,然後我需要使用Canvas在形式上畫球。其實我從來沒有嘗試過在Delphi中使用OOP(我只是在帕斯卡爾的學校簡單的練習),所以我遇到了很多問題。哦。 所以,這裏的代碼 單元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;用能做到

我不明白什麼是錯,如何我sholud聲明,並在這裏正確使用畫布。也許你在Delphi中使用Canvas的單元中有OOP的東西的例子嗎?

+0

「CALSS」來命名一個類的實例 - >你可以複製和粘貼文本:用鼠標標記文本,按ctrl + c,去你想要的地方,按ctrl + v –

+0

哦對不起。實際上,它是錯誤的窗口,我不能做這樣的事情,所以我只是決定鍵入它 – DanilGholtsman

+1

你聲明一個TCanvas變量,並試圖使用它沒有實例化它。事先需要'Can:= TCanvas.Create'。當你完成它時,不要忘記釋放它。在'Ball'中相同,在嘗試調用'BallCreate'之前,您需要'obj:= Ball.Create'。 –

回答

1

您聲明瞭一個Can:TCanvas;可變的但它不是在任何地方創建的。

您可以使用主窗體的畫布,對你應該把它傳遞給球〔實施例中的球構造,如:

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

那麼,你是不是正確的創建和球的實例:

obj.BallCreate(p,s,Sender);

創造你必須調用類的構造函數一樣

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

順便說了「T」之前球僅僅是一個慣例在Delphi