Recent

Author Topic: <SOLVED>Timer interval problem?  (Read 16172 times)

undermanager

  • Jr. Member
  • **
  • Posts: 58
<SOLVED>Timer interval problem?
« on: June 14, 2012, 11:41:28 am »
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

 :D

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;
« Last Edit: June 17, 2012, 04:47:09 pm by undermanager »

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: Timer interval problem?
« Reply #1 on: June 14, 2012, 03:24:09 pm »
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;
« Last Edit: June 14, 2012, 03:34:08 pm by Shebuka »

undermanager

  • Jr. Member
  • **
  • Posts: 58
Re: Timer interval problem?
« Reply #2 on: June 14, 2012, 09:41:35 pm »
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?

 :D

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Timer interval problem?
« Reply #3 on: June 14, 2012, 10:03:31 pm »
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;
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

undermanager

  • Jr. Member
  • **
  • Posts: 58
Re: Timer interval problem?
« Reply #4 on: June 14, 2012, 11:32:40 pm »
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?
« Last Edit: June 15, 2012, 01:09:50 pm by undermanager »

undermanager

  • Jr. Member
  • **
  • Posts: 58
Re: Timer interval problem?
« Reply #5 on: June 15, 2012, 01:26:02 pm »
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

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Timer interval problem?
« Reply #6 on: June 15, 2012, 03:05:09 pm »
Works here (attached)
*click Button1 to toggle between 1000 and 1 timer interval

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Timer interval problem?
« Reply #7 on: June 15, 2012, 03:06:35 pm »
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

  • Sr. Member
  • ****
  • Posts: 427
Re: Timer interval problem?
« Reply #8 on: June 15, 2012, 03:56:22 pm »
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

  • Jr. Member
  • **
  • Posts: 58
Re: Timer interval problem?
« Reply #9 on: June 15, 2012, 08:12:53 pm »
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!!

 :D
« Last Edit: June 15, 2012, 09:37:12 pm by undermanager »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Timer interval problem?
« Reply #10 on: June 16, 2012, 05:12:02 am »
Quote
When I try to run both your applications I get this error:

  Error Identifier not found RequireDerivedFormResource
Whoops, 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 1
Lemme see your changes

undermanager

  • Jr. Member
  • **
  • Posts: 58
Re: Timer interval problem?
« Reply #11 on: June 17, 2012, 03:31:51 pm »
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 ....

 :D


unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,FileUtil,Forms,Controls,Graphics,Dialogs,ExtCtrls,StdCtrls;

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

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Timer interval problem?
« Reply #12 on: June 17, 2012, 04:21:15 pm »
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

  • Jr. Member
  • **
  • Posts: 58
Re: Timer interval problem?
« Reply #13 on: June 17, 2012, 04:46:54 pm »
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.

 :D :D :D

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: <SOLVED>Timer interval problem?
« Reply #14 on: June 18, 2012, 09:16:34 am »
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
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018