一个类当中有TTimer控件,而我想在另外一个类当中调用,请问如何使用?

时间:2021-08-12 20:36:05
unit Unit1;

interface

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

/////////******************************
type
  TCarMove=class    //record
    TTimerXUp:TTimer;
    TTimerXDown:TTimer;
    TTimerYUp:TTimer;
    TTimerYDown:TTimer;
  private
    { Private declarations}
  public
    { Public declarations}
    constructor Create(AOwner:TComponent);
    destructor Destroy;Override;
    procedure TTimerXUpTimer(x,y:integer);
    procedure TTimerXDownTimer(x,y:integer);
    procedure TTimerYUpTimer(x,y:integer);    
    procedure TTimerYDownTimer(x,y:integer);
    
  end;
///////////////***************
type
  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    Button1: TButton;
    PaintBox1: TPaintBox;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  FCarMove:TCarMove;
  xu,xd,yu,yd:integer;
  a:integer;
implementation

{$R *.dfm}

////////////////////////////////////////*********************
//我想把这些都写成函数,可是中间出了点差错哦
constructor TCarMove.Create(AOwner:TComponent);
begin
    TTimerXUp:=TTimer.Create(AOwner);
    TTimerXDown:=TTimer.Create(AOWner);
    TTimerYUp:=TTimer.Create(AOwner);
    TTimerYDown:=TTimer.Create(AOwner);
end;
destructor TCarMove.Destroy;
begin
  TTimerXUp.Free;
  TTimerXDown.Free;
  TTimerYUp.Free;
  TTimerYDown.Free;
end;
procedure TCarMove.TTimerXUpTimer(x,y:integer);   var
  TemBit:TBitMap;
begin
  Form1.Repaint;
  TemBit:=TBitMap.Create;
  try
    TemBit.Assign(Form1.Image2.Picture.Graphic);
    Form1.PaintBox1.Canvas.Draw(x-Form1.Image2.Picture.Bitmap.Width,y-Form1.Image2.Picture.Bitmap.Height div 2,Form1.Image2.Picture.Bitmap);  
    x:=x+5;
    //TTimerXUp.Enabled:=False;
    TTimerXDown.Enabled:=False;
    TTimerYUp.Enabled:=False;
    TTimerYDown.Enabled:=False;
  finally
    TemBit.Free;
  end;
  Form1.Image2.Visible:=False;
  xu:=x;//记下X1,之后需要的
end;
procedure TCarMove.TTimerXDownTimer(x,y:integer);
var
  TemBit:TBitMap;
begin
  Form1.Repaint;
  TemBit:=TBitMap.Create;
  try
    TemBit.Assign(Form1.Image2.Picture.Graphic);
    Form1.PaintBox1.Canvas.Draw(x,y-Form1.Image2.Picture.Bitmap.Height div 2,Form1.Image2.Picture.Bitmap);  
    x:=x-5;
    TTimerXUp.Enabled:=False;
    //TTimerXDown.Enabled:=False;
    TTimerYUp.Enabled:=False;
    TTimerYDown.Enabled:=False;
   finally
    TemBit.Free;
  end;
  Form1.Image2.Visible:=False;
  xd:=x;
end;
procedure TCarMove.TTimerYUpTimer(x,y:integer); 
var
  TemBit:TBitMap;
begin
  TTimerYUp.Enabled:=True;
  Form1.Repaint;
  TemBit:=TBitMap.Create;
  try
    TemBit.Assign(Form1.Image2.Picture.Graphic);
    Form1.PaintBox1.Canvas.Draw(x-Form1.Image2.Picture.Bitmap.Width div 2,y-Form1.Image2.Picture.Bitmap.Height,Form1.Image2.Picture.Bitmap);
    y:=y+5;
    TTimerXUp.Enabled:=False;
    TTimerXDown.Enabled:=False;
    //TTimerYUp.Enabled:=False;
    TTimerYDown.Enabled:=False;
   finally
    TemBit.Free;
  end;
  Form1.Image2.Visible:=False;
  yu:=y;
end;
procedure TCarMove.TTimerYDownTimer(x,y:integer);
var
  TemBit:TBitMap;
begin
  Form1.Repaint;
  TemBit:=TBitMap.Create;
  try
    TemBit.Assign(Form1.Image2.Picture.Graphic);
    Form1.PaintBox1.Canvas.Draw(x-Form1.Image2.Picture.Bitmap.Width div 2,y,Form1.Image2.Picture.Bitmap);
    y:=y-5;
    TTimerXUp.Enabled:=False;
    TTimerXDown.Enabled:=False;
    TTimerYUp.Enabled:=False;
    //TTimerYDown.Enabled:=False;
  finally
    TemBit.Free;
  end;
  Form1.Image2.Visible:=False;
  yd:=y;
end;
/////////////////////////////////////******************************************************************

procedure TForm1.FormCreate(Sender: TObject);
begin
  PaintBox1.Left:=Image1.Left;
  PaintBox1.Top:=Image1.Top;
  PaintBox1.Width:=Image1.Width;
  PaintBox1.Height:=Image1.Height;
  FCarMove:=TCarMove.Create(Self);
  self.DoubleBuffered:=True;

  FCarMove.TTimerXUp.Enabled:=False;    //FCarMove.TTimerXUp.OnTimer:=FCarMove.TTimerXUpTimer;
  FCarMove.TTimerXUp.Interval:=100;
  FCarMove.TTimerXDown.Enabled:=False;
  FCarMove.TTimerXDown.Interval:=100;
  FCarMove.TTimerYUp.Enabled:=False;
  FCarMove.TTimerYUp.Interval:=100;
  FCarMove.TTimerYDown.Enabled:=False;
  FCarMove.TTimerYDown.Interval:=100;

  //FCarMove.TTimerXUp.OnTimer:=FCarMove.TTimerXUpTimer; 
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //FCarMove.TTimerYUp.Enabled:=False;
  FCarMove.TTimerYUp.Enabled:=true;
  FCarMove.TTimerYUpTimer(152+7,a);        //Ontime
  a:=a+5;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  //
end;
end.
现在我不是用Form1中的Timer空间,而是使用TCarMove这个类当中的那些Timer控件,于是我就用一个Button控件去控制Timer控件的事件,可是我这里的运行结果没有达到预期的结果。好像那个TTimerYUpTimer事件只是画一个图而已,而其他的就没有了。我主要是想发挥TTimer控件的Ontimer事件,人家也这样说,可是我经常出现问题,我想用://FCarMove.TTimerXUp.OnTimer:=FCarMove.TTimerXUpTimer;可是结果是行不通,说什么TObject与Integer不符哦,所以我现在根本就不知道问题到底出现在哪里了哦

5 个解决方案

#1


说过了,Ontimer事件格式是这样的 procedure TimerTimer(sender: tobject);

#2


只有参数一致,才能对过程赋值

#3


那如果要改,如何改?

#4


该回复于2009-08-24 16:26:00被版主删除

#5


类型不同
当然不能赋值了

#1


说过了,Ontimer事件格式是这样的 procedure TimerTimer(sender: tobject);

#2


只有参数一致,才能对过程赋值

#3


那如果要改,如何改?

#4


该回复于2009-08-24 16:26:00被版主删除

#5


类型不同
当然不能赋值了