Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Inc , dec width..

Here's what i got :

1 form (form1)

1 Timage Object (img)

1 timer (timer1)

1 Button (btn)

1 variable named X

----
Button code:
if timer1.enabled then
timer1.enabled:=false
else
timer1.enabled:=true;
-----
timer code:
var x:integer;

timer1.interval:=20;

x:=img.width

if (x>=10) and not(x<=215) then
begin
img.width:=img.width + 1;
end else begin
img.width := img.width - 1;
end;
-------

As you can see im trying to increase img's width
and decrease it back to custom size (in both cases).
(in infinite loop)

Well , for some reason it justs goes from its current position to 10.

Can someone fix my code ?

thanks for reading my thread :>



Comments

  • zibadianzibadian Posts: 6,349Member
    : Here's what i got :
    :
    : 1 form (form1)
    :
    : 1 Timage Object (img)
    :
    : 1 timer (timer1)
    :
    : 1 Button (btn)
    :
    : 1 variable named X
    :
    : ----
    : Button code:
    : if timer1.enabled then
    : timer1.enabled:=false
    : else
    : timer1.enabled:=true;
    : -----
    : timer code:
    : var x:integer;
    :
    : timer1.interval:=20;
    :
    : x:=img.width
    :
    : if (x>=10) and not(x<=215) then
    : begin
    : img.width:=img.width + 1;
    : end else begin
    : img.width := img.width - 1;
    : end;
    : -------
    :
    : As you can see im trying to increase img's width
    : and decrease it back to custom size (in both cases).
    : (in infinite loop)
    :
    : Well , for some reason it justs goes from its current position to 10.
    :
    : Can someone fix my code ?
    :
    : thanks for reading my thread :>
    :
    :
    The easiest way is to store the direction of the change in the X variable. If the width reaches maximum value, you switch the direction around, until it reaches the minimum value. In code:
    [code]
    Image1.Width := Image1.Width + X;
    if Image1.Width = 215 then
    X := -1;
    if Image1.Width = 10 then
    X := 1;
    [/code]
    For this trick to work, X must be a field in the form containing the OnTimer event. Also X must be set to 1 in the OnCreate() of the form.
  • porodoroporodoro Posts: 230Member
    : : Here's what i got :
    : :
    : : 1 form (form1)
    : :
    : : 1 Timage Object (img)
    : :
    : : 1 timer (timer1)
    : :
    : : 1 Button (btn)
    : :
    : : 1 variable named X
    : :
    : : ----
    : : Button code:
    : : if timer1.enabled then
    : : timer1.enabled:=false
    : : else
    : : timer1.enabled:=true;
    : : -----
    : : timer code:
    : : var x:integer;
    : :
    : : timer1.interval:=20;
    : :
    : : x:=img.width
    : :
    : : if (x>=10) and not(x<=215) then
    : : begin
    : : img.width:=img.width + 1;
    : : end else begin
    : : img.width := img.width - 1;
    : : end;
    : : -------
    : :
    : : As you can see im trying to increase img's width
    : : and decrease it back to custom size (in both cases).
    : : (in infinite loop)
    : :
    : : Well , for some reason it justs goes from its current position to 10.
    : :
    : : Can someone fix my code ?
    : :
    : : thanks for reading my thread :>
    : :
    : :
    : The easiest way is to store the direction of the change in the X variable. If the width reaches maximum value, you switch the direction around, until it reaches the minimum value. In code:
    : [code]
    : Image1.Width := Image1.Width + X;
    : if Image1.Width = 215 then
    : X := -1;
    : if Image1.Width = 10 then
    : X := 1;
    : [/code]
    : For this trick to work, X must be a field in the form containing the OnTimer event. Also X must be set to 1 in the OnCreate() of the form.
    :
    Cool , thanks :)

Sign In or Register to comment.