Moin,
ich habe hier ein Lazarus-Projekt (Ruhe, es handelt sich um den Informatikunterricht, mir wär auch was anderes lieber), in dem ich zwei Formulare brauche. Dafür braucht man offenbar auch zwei Units. Jetzt muss ich aber aus der untergeordneten Unit auf eine Variable der oberen Unit zugreifen. Geht das?
Code
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
unit2;
type
{ TCourt }
TCourt = class(TForm)
Ball: TShape;
Racket: TShape;
Timer: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure TimerTimer(Sender: TObject);
private
{ private declarations }
dx, dy: Integer;
isRunning: boolean;
public
{ public declarations }
end;
var
Court: TCourt;
implementation
{$R *.lfm}
{ TCourt }
procedure TCourt.TimerTimer(Sender: TObject);
begin
if isRunning then begin
Ball.top:=Ball.top+dy;
Ball.left:=Ball.left+dx;
if (Ball.top+Ball.Height>=clientheight) or (Ball.top<=0) then dy:=-dy;
if (Ball.width+Ball.left>=clientwidth) then dx:=-dx;
if (Ball.Top>=Racket.Top) and not (Ball.Top>Racket.Top+Racket.Height) and
(Ball.Left<=Racket.Left+Racket.Width) then begin
dx:=-dx;
ControlWindow.pPoints.Caption:=
inttostr(strtoint(ControlWindow.pPoints.Caption)+1);
end;
if (Ball.Left<0) then begin
ControlWindow.pFails.Caption:=
inttostr(strtoint(ControlWindow.pFails.Caption)+1);
isRunning:=false;
end;
end;
end;
procedure TCourt.FormCreate(Sender: TObject);
begin
dx:=10;
dy:=10;
end;
procedure TCourt.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
Racket.Top:=Y;
end;
end.
Alles anzeigen
Code
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls;
type
{ TControlWindow }
TControlWindow = class(TForm)
btStart: TButton;
lPoints: TLabel;
lFails: TLabel;
pPoints: TPanel;
pFails: TPanel;
procedure btStartClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
ControlWindow: TControlWindow;
implementation
{$R *.lfm}
{ TControlWindow }
procedure TControlWindow.btStartClick(Sender: TObject);
begin
Court.isRunning:=true;
end;
end.
Alles anzeigen
73
thosch97