HTML5-game for android with jerky/jumpy movements.

Hi

I’m doing a html5 game and my problem is that the sprite-movements aren’t very smooth.
The game sometimes stops for half a second and then continues, the problem is noticeable when i preview it in Firefox and far less than usable when i run the apk on my Android device (I got a cheap phone but it should be able to handle this).

The problem seems to be more often occurring to when there are a lot of sprite/camera movements.

Things I’ve done so far:

  1. Optimized the game loop so that it will be effective/short.
  2. Removed a background picture so that the background is just a solid color instead.
  3. Added limits to camera movements.
    These things have improved things quite a lot but not to an acceptable level.

Questions:

  1. Is there any good way to monitor execution times for code-sections in html5-games like “Display performances” for native games?

  2. Can i make sprite-handling less resource-consuming? Will it make a major difference if i use lower picture resolutions, have less frames in the animations, resize the pictures before loading them instead of using scaling inside GDevelop?

  3. Are there any known pitfalls in GDevelop that newbies usually have problems with, resource-consuming functions that can be replaced with others?

  4. I’m currently using a “for each-loop” to move all the movable sprites according to object-vars with their angle/speed, is it possible to somehow run this in a separate thread? Like having two game-loops in the same scene, one for all sprite-movements and one for all other functions?

  5. In the scene properties, there is an option called “Objects internal sort” that is always set to “Fast sort(faster but risk of flickering)” regardless of if i try to set it “Stable sort” instead. Could this be related to my problem?

It will be a lot easier to fix if you could upload your .gdg file or some screenshots of your code (if it’s short).

  1. Put a timer reset at the start of a function and a a timer pause at the end of it. Keep track of how many times the function is called. After some number of calls to the function (say 100), output this timer value to a text object. You can then see what functions are taking up the most time.

  2. Yes this can make a huge difference, especially if you’re loading tons of objects. I was just messing around with a game using around 6500 instances of an object and taking that image file size down from ~950 bytes to 750 bytes made a very noticeable performance boost!

  3. “resource-consuming functions that can be replaced with others?” Almost definitely. Hard to give advice here without seeing the functions. Obviously, if you’re using known common algorithms, you can read up on Google, Wiki, gamedev.net (to name a few places) on how to optimize that algorithm. Also, sometimes the choice of algorithm is inappropriate. I think it’s a good idea to read up on algorithms a lot when you’re making games!

4/5 - Sorry I have no good answers.

4- Are you sure the “for-each” loop is needed? If not, try to put your events outside it. When you have an event like this:

Condition: Variable "ID" of MyObject is != 0 Action: Add a force to MyObject of length MyObject.Variable(Speed) and angle MyObject.Variable(Angle)
GD will iterate internally over all objects (with Variable ID != 0), you don’t need the “for-each” loop everywhere :wink:

To be honest, I don’t know exactly when the “for-each” loop is needed, but I think (intuitively) they work when you have to find a condition between all the objects in the group: for example find the nearest object A to object B, when there are many objects A and B :neutral_face:

Hi again

I tried to size down some graphics without any effects, removed the foreach-loop, etc.
Haven’t had time to analyze performance with timers yet but I’ve ruled out most parts of the loop by disabling stuff to see what happens but whatever i do doesn’t seem to have any effect.

If someone would like to have a look at the gdg, i would really appreciate it:
renfors.org/gdg/Myproject6.tar.gz

Checking collisions is expensive. Don’t check for collisions every single frame. Put a timer on it (I find 0.05s seems to work well for most games that don’t involve high speeds).

Your enemy movement function is also large. You could consider putting a timer on that also (maybe 0.04s or something like that). You will need to adjust the values to keep the enemy movement the same.

I tried these changes on your game and it runs smoothly on my laptop whereas the original preview showed the pausing and jitters that you described in your first post.

Thanks Mats, it works ok now.