Performance issues

Hello all! I’m facing some performance issues with Phaser, I was hoping someone could help me out. First of all, I’m really noob in Phaser and Game Dev, but I know my ways with javascript as I am a frontend developer.

I’m trying to make a platformer game using Arcade physics and maps from Tiled. It started good but then I started patching some bugs here and there and after many “quick fixes” I think I’m doing a really bad job with performance.

This is an image of a map I’m trying to use in my game, everything loads fine, but when I load the enemies, the framerate drops to half. And in the hero update function I have some custom checks for like holding the jump button to make him jump higher and things like that, but when the game is running super slow and the update function is not called that often, the hero ends up jumping super high, plus some other weird things.

So if I have 10 enemies in screen, they all have their update function, which on every run will look for all tiles in the map, so they can know if they are about to go off a platform. I did this so I can make them go the other way around and just stay in the platform, going from one side to the other. I think this might be one of the biggest problems in my performance, but also, there are so many insane complex games that are played in my phone with 120 fps and I can’t believe that this is giving my browser problems. idk.

I also have custom collisions in my tiled (squares and circles) that loads when the stage is loaded.

Is there a way for when the frame drop I can keep the game speed the same, somehow? Or maybe any other kind of performance tip. Should I wrap some of my update logic into a scene.time.delayedCall or maybe the native RAF or settimeout?

You can play the game here: (choose the first stage) https://blopa.github.io/my-code-sux/
Source code: https://github.com/blopa/my-code-sux

Thanks

My initial thoughts is that your update loop might be pretty heavy. Instead of your enemies knowing about every tile on the map, can they just know which tiles they are allowed to walk on?

1 Like

Hi, i think you spotted the problem yourself.
You only need to know if the next tile under enemy collides or not. Look at:
https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.DynamicTilemapLayer.html#getTileAtWorldXY

2 Likes

Nice, that did the trick. Thanks.

Thanks for your suggestion, this was indeed the problem. What are some ways/tricks that I can use to make my update function lighter? Other then making the code efficient. Would stuff like skipping frames or using RAF or setTimeout help a bit? Thanks

I’m already impressed how efficient you use your graphic assets. It should be a very efficient game in the end.

1 Like

Don’t use requestAnimationFrame or setTimeout.

You could use delayedCall to run enemy updates on a larger interval, e.g. 50ms or 100ms. But if you switch to getTileAt… that won’t be necessary.

1 Like

I want test some enemies’s instance with:
scene.cameras.main.worldView.contains(this.x, this.y)
to skip logic when not visible on screen, but i’m not sure if it’s efficient.
@samme, what do you think?

It should be pretty efficient, just beware that worldView is correct only after at least one camera render.

Ok, thanks