Sprites don't move smoothly

Hello everyone. I’m learning how to make games with OpenFL and I’m having a problem with how the sprites move. When I launch the .swf file, the sprites move smoothly but they “jump” for just a little bit every couple seconds. But when I launch the .exe (Neko) file, the sprites are not moving smoothly at all.
My code - https://gist.github.com/anonymous/6d1c78d21ae0859f8b8d.
My project folder (FlashDevelop) with the program compiled for both (Flash and Windows) platforms - https://mega.nz/#!74QWET7Z!6EtbyaHUCILWNQTDkMmIG8w9sL9QZaTdtZype9QE04o
I’ve tested the .exe file on two different computers (they are a few years old) and the program stuttered on both of them. So I guess it the problem is in my code. Maybe there is something wrong with the project settings (project.xml), or with the method which handles ENTER_FRAME event? Any kind of help is appreciated.
Also I wanted to thank people who worked on OpenFL because this is such a great platform for making games!

Maybe it’s in your handleEnterFrameEvent()? I don’t know how reliable Lib.getTimer() is, maybe there are some discrepancies between different platforms?

I see that’s the function used in the enter frame example at http://www.openfl.org/documentation/basics/animation/enter-frame/

I don’t know much more than that, your code seems consistent with the example code. I thought maybe the value of getTimer() should be a Float, or it should be divided by 1000.0 maybe, but you’re doing it as the example says. Maybe try the example and see if that also has the same stutter?

Thank you for your reply, @cactusanctuary
I used Float and dividing by 1000.0, but the problem remained. So I decided to test the enter frame example. I did two changes to the example code: 1. I used my own image; 2. I added the trace(deltaTime); line.
Main.hx - https://gist.github.com/anonymous/bd058d6ee1962564f9b76e37c11e6b97
Project (FlashDevelop) - https://mega.nz/#!j8ZyHajJ!WOpeeRDOKtJkyb9Cn0iheBcFXylUT5ospWQ-2OT2p6Y
I did 3 runs on each platform and created a chart to visualize the delta time after each frame - https://docs.google.com/spreadsheets/d/1f5HZqh1UkSYzS46aDwq-aYHkFEeugnS2-oel5zDP0dw/edit?usp=sharing
This time the situation was the opposite - the image was moving more or less smoothly on Neko, but it was horrible on Flash.
I think that the reason why the sprites are not moving smoothly is because there are these spikes - http://i.imgur.com/1k3KodW.png
I ran the app at 30 fps on both platforms. Flash player version is 21.
Is there any way to fix this? Am I doing something wrong?

Yes those spikes would indicate where the movement is irregular. I don’t know what the problem is though :frowning: I don’t think it would be a memory allocation issue (only 2 ints created) and the calculations shouldn’t be too heavy.

Personally I don’t use delta time in my game I’m making in OpenFL, I do rely on the FPS being consistent.

Maybe @singmajesty has an idea about this?

I have updated my code by adding a max delta time (1 / FPS) - https://gist.github.com/anonymous/481756e265bbbcf434ef9a8770d93050
The animation looks better now, but it’s still not smooth (the spikes are still thre and they are consistent).
I also made new charts which show delta time per frame - https://docs.google.com/spreadsheets/d/1WqyPxwNuVO_osR8XO95JZNacejAitTBPsl3C2oddvMw/edit?usp=sharing

That max delta time sounds like a hack to smooth out whatever the real issue is.

I made a new project with your first code example, and it runs smoothly on Neko and Flash! deltaTime was always in the range 31-36 ms, with no spikes. To me that indicates the problem might be your computers… Mine is also a few years old and is OK.

Maybe it’s some issue with your versions of Haxe/Lime/OpenFL? If you run command haxelib list, what do you get?

Thank you for taking a look at my code. You are being very helpful. I think that reinstalling Windows should solve the problem, since I installed it quite awile (like 2 years ago).

actuate: [1.8.5] 1.8.6 advanced-layout: [0.5.0] akifox-asynchttp: 0.4.1 [0.4.2] flixel-addons: [2.0.0] flixel-demos: [2.1.0] flixel-templates: [2.0.1] flixel-tools: [1.1.2] flixel-ui: [2.0.0] flixel: 3.3.11 [4.0.1] format: [3.1.2] goodies: [0.2.1] hamcrest: [2.0.0] haxeui: [1.7.16] hscript: [2.0.4] hxcpp: [3.2.102] hxssl: [3.0.0-alpha] layout: 1.2.0 [1.2.1] lime-tools: [1.5.7] lime: 2.4.1 2.6.2 2.7.0 [2.8.2] 2.9.0 mconsole: [1.6.0] mcover: [2.1.1] mint: git [dev:C:\HaxeToolkit/mint/git] mlib: [2.0.2] monax: [1.5.1] msignal: [1.2.2] munit: 2.1.0 [2.1.2] nape: [2.0.19] openfl-bitfive: [3.0.0] openfl: 3.0.6 3.3.1 3.3.3 3.3.7 3.4.0 [3.5.3] 3.6.0 promhx: 1.0.19 [1.0.21] random: [1.4.1] svg: [1.0.8] swf: [1.8.7] tweenx: [0.0.5] utest: [1.3.10] yagp: [1.1.4]

This happens to me with a game in 60fps.
A random fps gagging happens very often (from second to second) on the cpp target but not for html5 so I supposed it’s a timing issue.
This is not a computer perform issue (I have an i7, GTX 960 and still see fps gagging with the simple lime samples).

Anyway what I have done is extends the lime._backend.native.NativeWindow, override things related to the main loop then implemented an update event with a custom timer.
You just have to “backend = new CustomBackend(this)” on the constructor of the Main class to setup it.
The timer is something like this: https://github.com/ncannasse/heaps/blob/master/hxd/Timer.hx

In my haxelib list, my lime and openfl are more recent:

  • lime [2.9.1]
  • openfl [3.6.1]

I think updating these two might help. Try these commands one after another:

  1. haxelib update lime
  2. haxelib update openfl
  3. openfl rebuild hxcpp windows
  4. openfl rebuild windows

Then try testing your game again. There might be other dependencies of libraries. I didn’t suggest haxelib upgrade (which updates all your Haxe libraries) because you might want to keep some at their current version.

I hope this helps. Sometimes it is indeed tricky to get stuff working with all the different versions :confounded:

I got what is the issue.
The render event is executed in a different frequency than the update event.
The positions are updated at one point but the render at other which cause all those problems with visual flicker.
I just move the main render method of my engine to the update event of Lime and now everything is very smooth.

2 Likes