我的程序哪里错了?(100分)

  • 主题发起人 主题发起人 jxjlsun
  • 开始时间 开始时间
J

jxjlsun

Unregistered / Unconfirmed
GUEST, unregistred user!
一个弹性小球,当它撞着区域的边缘,就会向相反的方向运动,运动过程中小球的半径会不停
的变化,利用TRACKBAR和SPINEDIT可以改变小球的运行速度,我编的程序如下(不知哪里出错了
,按BUTTON1,小球不动)[?][?]:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
TrackBar1: TTrackBar;
SpinEdit1: TSpinEdit;
Panel1: TPanel;
Image1: TImage;
Timer1: TTimer;
Shape1: TShape;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
i:integer;
implementation

{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
begin
timer1.Interval:=trackbar1.Position;
if i=1 then
begin
if shape1.Left>0 then
begin
shape1.Left:=shape1.Left-10;
shape1.Width:=(shape1.Width+1)mod 70;
shape1.Height:=(shape1.Height+1)mod 70;
end
else
i:=2;
end;
if i=2 then
begin
if shape1.Left<(panel1.width-shape1.width-5) then
begin
shape1.Left:=shape1.Left+10;
shape1.Width:=(shape1.Width+1)mod 70;
shape1.Height:=(shape1.Height+1)mod 70;
end
else
i:=1;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.Interval:=trackbar1.Position;
timer1.Enabled:=true;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
timer1.Enabled:=false;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
spinedit1.Value:=trackbar1.Position;
end;

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
trackbar1.Position:=spinedit1.Value;
end;

initialization
i:=1;
end.

 
》timer1.Interval:=trackbar1.Position;

if trackbar1.Position>0 then
timer1.Interval:=trackbar1.Position
else
timer1.Interval:=10;
 
好象问题不在timer1.Interval:=trackbar1.Position;
 
你试了吗?
 
到QQ上找我,我帮你搞掂。23555873 或 584740
 
i未初始化, 所以i是个随机数, =1和2的可能性很小. 而你的程序中如果i<>1并且<>2时, ontimer里其实什么都不做。

我改了改你程序源代码的缩进, 你自己看看如果i=3时程序会执行哪句?
if i=1 then // i=3, 程序将直接跳到 if i=2 then那句
begin
if shape1.Left>0 then
begin
shape1.Left:=shape1.Left-10;
shape1.Width:=(shape1.Width+1)mod 70;
shape1.Height:=(shape1.Height+1)mod 70;
end
else
i:=2;
end;

if i=2 then // i=3, 什么都不做
begin
if shape1.Left<(panel1.width-shape1.width-5) then
begin
shape1.Left:=shape1.Left+10;
shape1.Width:=(shape1.Width+1)mod 70;
shape1.Height:=(shape1.Height+1)mod 70;
end
else
i:=1;
end;

 
应当在onformcreate中进行i初始化。
 
后退
顶部