Forum > Beginners

Simple animation with TTimer

(1/3) > >>

heebiejeebies:
Hi all,

I'm trying to do a very simple animation that slides one box off the screen when the user presses a button, and simultaneously slides in a set of several other boxes.  Have tried various different configurations and also some other fancy timer, and I keep having the same problem.  Sometimes it works and sometimes it doesn't - when it doesn't, the screen appears to just freeze for a second and then the boxes are in their final position, without me seeing any of the animation. 

The following script worked well for a while and then just suddenly stopped, for reasons I cannot detect.  The only changes I made were adding one more item to the list of controls moving in, and after the final "if PPBoxDB.Left < -400", I added one final piece of code to move all the controls into their final position at Left :=520.  That's it.  Of course, I added the final move to 520 as part of the "if" clause.  And even after removing those items, the code no longer works.

The OnTimer event of my TTimer is the following, and my interval is 5.   PPBox is the original box, which is slid off to the left, to be replaced by the other boxes.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if PPNickNameLabel.Left > -400 thenbeginPPBoxDB.Left := (PPBoxDB.Left - 12); PPNicknameLabel.Left := (PPNicknameLabel.Left -12);PPNickName.Left := (PPNickName.Left -12);PPCharacteristicsLabel.Left := (PPCharacteristicsLabel.Left -12);PPCharacteristics.Left := (PPCharacteristics.Left -12);PPBarriersLabel.Left := (PPBarriersLabel.Left -12);PPBarriers.Left := (PPBarriers.Left -12);PPUserNotesLabel.Left := (PPUserNotesLabel.Left -12);PPUserNotes.Left := (PPUserNotes.Left -12);end;if PPBoxDB.Left < -400 then KillPPBox.Enabled :=false;                                                           
Any ideas?  Or is there a better way to do this? Thanks!

howardpc:
A compilable project would be more helpful.
But a timer interval of 5 ms for so much successive screen redrawing (and no Application.ProcessMessages) may be unrealistic for your hardware.

heebiejeebies:
I can't possibly conceive how any modern hardware would choke on moving 9 simple items across the screen, but in any case I've tried with all sorts of different intervals and it makes no difference.  You'd also expect the pattern to be more random, whereas this works perfectly for long stretches at a time and then a slight breeze causes it to permanently break until I try something else.

Handoko:
For issues 'why my code doesn't run as what I think', usually is very helpful if the user can provide the compile-able source code, so others can run and inspect the problem.

In the link below, you can find many simple animation demos:
https://wiki.freepascal.org/Portal:HowTo_Demos

Click the link above and find these names:
- Moving an Object by pressing a key
- Control an Object's direction using the keyboard
- Simple animation
- Move a shape with a mouse
- Button with animation


--- Quote from: heebiejeebies on May 28, 2022, 03:10:26 am ---I can't possibly conceive how any modern hardware would choke on moving 9 simple items across the screen
--- End quote ---

LCL components are build for compatibility not optimized for performance, you won't get the performance the hardware can offer. If performance is important, you should consider other graphics libraries that are hardware accelerated.

TTimer is okay for simple animations. But it won't give you any difference if the interval is too small. I am not sure, but I usually won't use interval < 16.

If time precision is important, you should use different technique. Like EpikTimer + delta time method.

For animating several components with same direction and speed, you should put them in a container, TPanel for example. Like the demo:
simple transition effect (in User Interface category, in the link above)

heebiejeebies:
Thanks for the input.  For some reason, it has started working when I up the interval to 16. Doesn't make a lot of sense because I've had the interval set even higher before and still had problems.  Anyway, 16 interval is too slow, so I just set 2 simultaneous timers running, both doing the same thing.  For some reason this works.

Thanks for the link, too.  I will check those out if I need to do any more of this stuff.

Navigation

[0] Message Index

[#] Next page

Go to full version