Hi! I've been working on a new platforming game, and I'm struggling with lag.
I'll give some info about the game:
It's a 2d platformer, with 3 types of objects: platforms, which are straight lines or rectangles, but can be rotated; saws, which can move between 2 points on a sine function and always rotate; and checkpoints, which pulsate in length.
Of these objects, the platforms are the least laggy, but the other 2 are more laggy.
I'm quite certain the problem of lag comes from the many objects loaded in the level.
In the game, it's only 1 level, and since I used the code from another platformer I made, where it's divided up into multiple levels, it's always rendering all of those objects.
For the game I copied from, it was okay, because it didn't load objects that weren't in the level, but with all of the objects being in 1 level, and always being loaded, it's quite laggy.
I have an idea for optimization, but if any of you have any ideas that might work better, let me know.
My idea is to not load objects that aren't on screen. So, anything that's not on screen doesn't do anything except wait until it is on screen. But then the problem is, I can't figure out how to easily check if something is on screen.
Basically, I need a way to check if something is on screen. But, I need it to be simple and efficient. If it's too complex, it likely wouldn't be any better than if everything was just always loaded. Can someone help?
One way is to just check if the left side of the hitbox is greater than the left of the stage, right is less than the right of the stage, top is less than the top of the stage, or the bottom is greater than the bottom. Of course you should check if both a horizontal position and a vertical position is on screen.
The clones do sometimes overlap, mostly from saws either being placed on platforms, or moving saws moving into platforms. So, would all of the clones have to be slightly transparent? Or would all of the clones have to change the transparency of all the other clones after every check? If it's the first option, that could work great. If it's the second one, that would probably be orders of magnitude laggier.
For reference, there are nearly 200 clones, being platforms, saws, and checkpoints, that would all need to use this simultaneously. If a clone had to change the transparency of everything except itself when it needed to check, it most likely would result in large amounts of lag, which is exactly what I'm trying to fix, not create.
No, no, just change the transparency as you create a sprite. (Clones should inherit from their parent, I think, so it's just the toplevel sprites you have to worry about.)
I tried it, it doesn't work. It only checks the center for the transparency. I need to check if any part of the clone is on the screen, not just the center.
If the platforms move in a circle, you can find the square around the platform just by taking the radius, and adding or subtracting it from the x or y position (subtract from y to get bottom, add to y to get the top, subtract from x to get left, add to x to get right).
Hm... I tried implementing it, and even only being in the platform code, it drastically lowers the framerate, to the point where I got the "page unresponsive" message before I could stop the program. Do you have any idea why that could be?