1

I'm very new to Delphi and still learning.

I have a procedure which will take an image as an input and will make the image to move down to certain pixels using loop, during this process I would like to show the images. I have tried to use "Timer.Interval" method to show the image in between intervals but I'm sure I'm doing things wrong. Is there a way of handling this? If not, I'm open to any other suggestions on how I could simultaneously show image while doing some iteration.

Thanks in advance.

procedure BlockSpawn(var Image1: TImage; var Timer1: TTimer);
begin
    Timer1.Enabled := True;
    WITH Image1 do begin
         repeat
               Timer1.Interval := 600;
               Top := Top + 66;
               Image1.Show;
         until (Top = (TForm1.Bottom - Height)); {repeat}
    end; {WITH}
end; {begin}

The reason behind this is that I would need to run this procedure multiple times in every iteration, and would need user to see the image being simultaneously shown to them. Like in Tetris how the new block will always move down from the same position and keeps on repeating until certain event occurs.

dnTosh
  • 183
  • 1
  • 3
  • 12
  • 2
    Indeed doing wrong. You need to use the OnTimer event handler of the Timer to move the image. No loops, with each fire get the position, set the position. BlockSpawn should just start the timer (enabled). You can study [VCL.Meteor](https://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE6/Object%20Pascal/VCL/Meteor/). – Sertac Akyuz Oct 08 '19 at 19:08
  • 2
    That's certainly not the way to do it, you need to enable the timer and do the work in it's TimerEvent. Forget about TImage, you need sprites and a gaming engine, I used [DelphiX](http://www.micrel.cz/Dx/) in the past with some good results. – whosrdaddy Oct 08 '19 at 19:10
  • 2
    This is an example I often show beginning students of Delphi: https://stackoverflow.com/a/7224075/282848 – Andreas Rejbrand Oct 08 '19 at 19:13
  • 1
    Unfortunately this learning takes times. You are probably attempting to write a program that is a little too challenging for your current level. I suggest picking an easier task. – David Heffernan Oct 08 '19 at 19:35
  • 1
    Indeed, I've been using Delphi for about 10 years now, and have even written visualizations... yet my attempt to write a Tetris game is still very challenging and unfinished. It may seem easy conceptually, but actually writing code to make it useful is very difficult. – Jerry Dodge Oct 08 '19 at 19:54
  • thanks all! I know it will take tremendous amount of time since I have very recently started learning Delphi. I'll give different methods a try. – dnTosh Oct 08 '19 at 19:57

1 Answers1

3

The correct way to handle this is to use the TTimer.OnTimer event, eg:

procedure BlockSpawn(Image: TImage; Timer: TTimer);
begin
  Timer.Tag := NativeInt(Image);
  Timer.Interval := 600;
  Timer.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  Image: TImage;
begin
  Image := TImage(Timer1.Tag);
  Image.Top := Image.Top + 66;
  Image.Show;
  if Image.Top >= (Bottom - Image.Height) then
    Timer1.Enabled := False;
end;

Note, however, that this approach makes BlockSpawn() run asynchronously. If you really want BlockSpawn() to run synchronously , get rid of the TTimer and use Sleep() instead:

procedure BlockSpawn(Image: TImage);
begin
  repeat
    Sleep(600);
    Image1.Top := Image1.Top + 66;
    Image1.Show;
    Form1.Update;
  until Image1.Top >= (Form1.Bottom - Image1.Height);
end;

However, this approach will likely make your UI feel sluggish and be less responsive to your users.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770