How do I move an image up and down when a button is clicked

132 Views Asked by At

I am trying to animate a celebratory trophy image that will 'bounce' up and down. I tried using a timer and then I used modulus to determine whether its odd or even, if its odd it goes up 10 if even it goes down 10 etc. I think the problem is looping, I need to use some form of loop right?

unit Unit11;

interface

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

type
  Tfrmwinner = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Image1: TImage;
    Image2: TImage;
    Label3: TLabel;
    Label4: TLabel;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure Label4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmwinner: Tfrmwinner;

implementation

uses Unit12,Unit4;

{$R *.dfm}

procedure Tfrmwinner.Label4Click(Sender: TObject);
var
position:integer;
begin
frmwinner.Hide;
frmboard.show;

unit12.frmboard.memlead.Lines.Add('Position'+#9+'Name'+#9+'ID Number');
unit12.frmboard.memlead.Lines.Add('___________________________________');
while not unit4.frmcontest.ADOLead.Eof do
begin
position:=position+1;
unit4.frmcontest.ADOLead.First;
  unit12.frmboard.memlead.Lines.Add(inttostr(position)+#9+unit4.frmcontest.ADOLead['Name(s)']+#9+inttostr(unit4.frmcontest.ADOLead['ID Number']));
  unit4.frmcontest.ADOLead.Next;
end;




end;

procedure Tfrmwinner.Timer1Timer(Sender: TObject);
var
icount,i:integer;
begin

icount:=0;

icount:=icount+1;






if (icount mod 2)=1 then
begin
  image1.top:= image1.top+10;
  image2.top:= image2.top+10;
end;


  if (icount mod 2)=0 then
begin
  image1.top:= image1.top-10;
  image2.top:= image2.top-10;
end;







if icount=16 then
begin
  timer1.Enabled:=false;

end;
end;

end.

This is what I've tried, with no luck

1

There are 1 best solutions below

0
Tom Brunberg On

The problems related to how the image jumps (or doesn't jump) are in procedure TfrmWinner.Timer1Timer(Sender: TObject);

Note that event handlers like an OnTimer or OnKeyPress etc. are triggered by certain system events, and that the event handler you write should do its task as fast as possible and then exit. Also, anything you need to persist until the event handler is called the next time, must be saved in a "safe place" outside of the event handler.

First, the icount: integer variable cannot be declared in the OnTimer handler, because it would cease to exist every time the procedure exits.

Secondly, you can not initialize it (assign 0 to it) in the OnTimer handler, because then it would obviously never reach the final value of 16.

So, move the declaration of icount: integer to the private section of the form:

private
  { Private declarations }
  icount: integer;

Then, initialize it to 0 and start the timer in the Label4Click() procedure if that is the purpose (it's unclear in your current code).

icount := 0;
Timer1.Enabled := True;