Timer interval problem? lazarus / 31-LCL ---------------------------------------------------------------------- undermanager · 2012-06-14 09:41 I'm using Windows 7 64 bit and Laz 0.9.30.2. I have a shape happily bouncing around a form. However, when I change the timer interval property e.g. from 1000 to 1, the speed of movement stays the same. Any ideas, please? Thanks The code for by shape is below: procedure TForm1.Timer1Timer(Sender: TObject); begin   if moveRight = true then     Shape1.Left := Shape1.left + 1   else     Shape1.Left := Shape1.Left - 1;   if Shape1.Left <= 0 then     moveRight := True;   if Shape1.Left + Shape1.width > form1.width then     moveRight := false;   if moveUp = true then     Shape1.Top := Shape1.Top + 1   else     Shape1.Top := Shape1.Top - 1;   if Shape1.Top <= 0 then     moveUp := True;   if Shape1.Top + Shape1.width > form1.height then     moveUp := false; end; ---------------------------------------------------------------------- Shebuka · 2012-06-14 13:24 check if maybe you reassign the interval in some other place, and i don't remember well, but you may also need to stop the timer before changing interval. Also for a game it's a good idea to leave Interval on something like 33 (it's ~30,3030 frames per second) and change the effective speed: Shape1.left + FVelocity; ---------------------------------------------------------------------- undermanager · 2012-06-14 19:41 Thanks for the reply. There is only one component on the form, the shape. There is no other code apart from what is below. I have disabled the timer on formCreate, and then started the shape moving by pressing'p' but no change. The speed of the shape doesn't alter, regardless of the value I type into interval. Any other suggestions, anyone? ---------------------------------------------------------------------- Fred vS · 2012-06-14 20:03 Hello. Have you tried ? procedure TForm1.Timer1Timer(Sender: TObject); begin Timer1.enabled := false ;   if moveRight = true then     Shape1.Left := Shape1.left + 1   else     Shape1.Left := Shape1.Left - 1;   if Shape1.Left <= 0 then     moveRight := True;   if Shape1.Left + Shape1.width > form1.width then     moveRight := false;   if moveUp = true then     Shape1.Top := Shape1.Top + 1   else     Shape1.Top := Shape1.Top - 1;   if Shape1.Top <= 0 then     moveUp := True;   if Shape1.Top + Shape1.width > form1.height then     moveUp := false; Timer1.enabled := true ; end; ---------------------------------------------------------------------- undermanager · 2012-06-14 21:32 Hi there Thanks for the reply. I've tried your suggestion but no joy. All there is on the form is one shape. This is my code, but adjusting the interval doesn't help: implementation {$R *.lfm} { TForm1 } procedure TForm1.Timer1Timer(Sender: TObject); begin   Timer1.enabled := false ;   if moveRight = true then     Shape1.Left := Shape1.left + 1   else     Shape1.Left := Shape1.Left - 1;   if Shape1.Left <= 0 then     moveRight := True;   if Shape1.Left + Shape1.width > form1.width then     moveRight := false;   if moveUp = true then     Shape1.Top := Shape1.Top + 1   else     Shape1.Top := Shape1.Top - 1;   if Shape1.Top <= 0 then     moveUp := True;   if Shape1.Top + Shape1.width > form1.height then     moveUp := false;   Timer1.enabled := true ; end; procedure TForm1.FormCreate(Sender: TObject); begin    Timer1.Interval:=1; end; procedure TForm1.FormKeyPress(Sender: TObject; var Key: char); begin   if (((Key = 'p') or (Key = 'P')) And (Timer1.Enabled = true)) then     Timer1.Enabled := false   else if (((Key = 'p') or (Key = 'P')) And (Timer1.Enabled = false)) then     Timer1.Enabled := true; end;  end.   Any other suggestions, please? Could this be a bug with the timer? ---------------------------------------------------------------------- undermanager · 2012-06-15 11:26 I've just tried the above code on a different 64bit computer, but still the same problem. Very frustrating. Any other suggestions? How's the code behave on your PC (just a simple shape on a form)? ---------------------------------------------------------------------- Leledumbo · 2012-06-15 13:05 Works here (attached) *click Button1 to toggle between 1000 and 1 timer interval ---------------------------------------------------------------------- ludob · 2012-06-15 13:06 Using lazarus 1.1 on Windows 7 64 bit the following works for me (drop shape,timer and button on form): Code: [Select]var   moveRight,moveUp:boolean; procedure TForm1.Timer1Timer(Sender: TObject); begin   if moveRight = true then     Shape1.Left := Shape1.left + 1   else     Shape1.Left := Shape1.Left - 1;   if Shape1.Left <= 0 then     moveRight := True;   if Shape1.Left + Shape1.width > form1.width then     moveRight := false;   if moveUp = true then     Shape1.Top := Shape1.Top + 1   else     Shape1.Top := Shape1.Top - 1;   if Shape1.Top <= 0 then     moveUp := True;   if Shape1.Top + Shape1.width > form1.height then     moveUp := false; end; procedure TForm1.Button1Click(Sender: TObject); begin   Timer1.Interval:= Timer1.Interval div 2;   Button1.Caption:=IntToSTr(Timer1.Interval); end; ---------------------------------------------------------------------- Shebuka · 2012-06-15 13:56 Working for me. Try attached app, it's sort of bouncing air hockey disk While hold a key try click with mouse on button to change timer interval, you will notice huge difference even between interval on 33 and on 66. (p.s. the problem is the event loop of form repainting, if you not force it with Form1.Repaint on every timer interval you will not see difference between interval 1 to 9, then between 10 to 49 and so on) ---------------------------------------------------------------------- undermanager · 2012-06-15 18:12 Leledumbo and Shebuka Thanks for the attachments. When I try to run both your applications I get this error:   Error Identifier not found RequireDerivedFormResource but by commenting out the offending line, the programs work. I can compile and run shebuka's shapemovement code, but can't get the shape to move. Leldumbo's program, however, works sort of!! The interval certainly changes between 1000 and 1, and speeds up and slows down the shape. But if I change the code for the buttonClick event to say 1000 and 200, the interval changes but in exactly the same way as for 1000 and 1. Very strange. With ludob's code, I dropped a button, timer and shape onto a form, copied the code over and got this error message: Error: method identifier expected on this line: procedure TForm1.Timer1Timer(Sender: TObject); Thanks for the help - don't give up on me, please! I'm really enjoying learning Lazarus and object Pascal and these problems are helping me understand more!! ---------------------------------------------------------------------- Leledumbo · 2012-06-16 03:12 QuoteWhen I try to run both your applications I get this error:   Error Identifier not found RequireDerivedFormResourceWhoops, sorry. We're using newer Lazarus then. Simply commenting it out would work. Quote But if I change the code for the buttonClick event to say 1000 and 200, the interval changes but in exactly the same way as for 1000 and 1Lemme see your changes ---------------------------------------------------------------------- undermanager · 2012-06-17 13:31 Thanks! As I said, changing the interval from 1 to e.g. 200, doesn't change the speed in my program for some reason. Another Sunday disappears .... unit Unit1; {$mode objfpc}{$H+} interface uses   Classes,SysUtils,FileUtil,Forms,Controls,Graphics,Dialogs,ExtCtrls,S tdCtrls; type   { TForm1 }   TForm1 = class(TForm)     Shape1: TShape;     Timer1: TTimer;     Button1: TButton;     procedure Timer1Timer(Sender: TObject);     procedure Button1Click(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1;   moveRight,moveUp: Boolean; implementation {$R *.lfm} { TForm1 } procedure TForm1.Timer1Timer(Sender: TObject); begin   if moveRight = true then     Shape1.Left := Shape1.left + 1   else     Shape1.Left := Shape1.Left - 1;   if Shape1.Left <= 0 then     moveRight := True;   if Shape1.Left + Shape1.width > form1.width then     moveRight := false;   if moveUp = true then     Shape1.Top := Shape1.Top + 1   else     Shape1.Top := Shape1.Top - 1;   if Shape1.Top <= 0 then     moveUp := True;   if Shape1.Top + Shape1.width > form1.height then     moveUp := false; end; procedure TForm1.Button1Click(Sender: TObject); begin   if Timer1.Interval = 1000 then     Timer1.Interval := 200   else     Timer1.Interval := 1000; end; end. ---------------------------------------------------------------------- Leledumbo · 2012-06-17 14:21 The difference is very clear with this code: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); begin   case Button1.Tag of     0: begin       Timer1.Interval:=1000;       Button1.Tag:=1;     end;     1: begin       Timer1.Interval:=100;       Button1.Tag:=2;     end;     2: begin       Timer1.Interval:=1;       Button1.Tag:=0;     end;   end; end; ---------------------------------------------------------------------- undermanager · 2012-06-17 14:46 Thanks. That certainly worked. I can see the difference clearly. I'm not sure why my code didn't though! I will no doubt return when I've played around with timers a bit more. Mu next job is to to start making an object whizz around the screen at top speed next. Thanks for your help. ---------------------------------------------------------------------- avra · 2012-06-18 07:16 If you are developing for Windows, then you should know that having a regular timer interval less then about 10ms is not working (e.g. setting 1ms and 10ms has the same effect). If you need <10ms then use custom multimedia high precision timers which I think do not ship with Lazarus by default. More info here: http://www.lazarus.freepascal.org/index.php/topic,16530.0.html