please help: flickering sprites and hickup/tug in background scroll

Show off your games, demos and other (playable) creations.
Post Reply
nikgid
Prole
Posts: 2
Joined: Mon Jul 01, 2019 10:50 am

please help: flickering sprites and hickup/tug in background scroll

Post by nikgid » Mon Jul 01, 2019 12:21 pm

Hi folks,

please be gentle... :-) I am a newbie to this forum.

I am trying to "copy" the easter-egg dino-game of the chromebrowser in love2d.
I am using the "push"-library by user Ulydev to make the game freely scalable (https://love2d.org/forums/viewtopic.php?t=80738).

Something that I find annoying in my game at the moment is, that the "enemy"-sprites, especially the cactuses are flickering heavily. And I am not sure what to do about that. Does it have to do with the in-game scaling of the sprites?

Also the horizontal scroll does have a small tug/hickup from time to time. What could I do to resolve that?

I am gratefull for any help. A love-file of the game is attached.

Thanks and cheers
Niklas
Dino_Game.love
(26.22 KiB) Downloaded 43 times

User avatar
pgimeno
Party member
Posts: 1912
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

Re: please help: flickering sprites and hickup/tug in background scroll

Post by pgimeno » Mon Jul 01, 2019 6:01 pm

Hello, welcome to the forums.

Actually, I notice that the dino also flickers when moving left and right. The flicker comes from the nearest filter, the non-integer scaling factor and the drawing at non-integer coordinates.

The occasional jerking of some enemies seems to be due to skipping the update of the next enemy whenever you delete an enemy from the allEnemies table. Imagine your allEnemies table contains 2 elements: allEnemies[1] is about to be deleted, and allEnemies[2] is still in the table. Now you loop through allEnemies. i is set to 1 and enemy is set to allEnemies[1]. The code finds that allEnemies[1] needs to be deleted, therefore it goes ahead and deletes it. That deletion makes allEnemies[2] become allEnemies[1]. On the next iteration, i is set to 2. Now allEnemies[2] does no longer exist and the loop ends. As a result, the enemy after the one deleted isn't updated.

An easy solution is to loop backwards, i.e. instead of this:

Code: Select all

for i, enemy in ipairs(allEnemies) do
    ...
use this:

Code: Select all

for i = #allEnemies, 1, -1 do
    local enemy = allEnemies[i]
    ....
In your case, you don't care about the checking/deletion order, so you can ignore the rest of the post. I'm adding it just because if the need arises (which happens rarely but I've run into a few cases), it's good to know what to do.

If the checking order is important, the procedure would require a more involved solution: to use your own loop variable, and to increment it only if you are not deleting an element. Something along these lines:

Code: Select all

local i = 1
local numEnemies = #allEnemies  -- cache the item count, to avoid calculating it every time
while i <= numEnemies do
    local enemy = allEnemies[i]
    if hasToBeDeleted(enemy) then
        table.remove(allEnemies, i)
        numEnemies = numEnemies - 1  -- update the item count
        -- don't increment i here
    else
        -- increment i here
        i = i + 1
    end
end

User avatar
DwayneDev
Prole
Posts: 24
Joined: Sat Jun 15, 2019 9:33 pm
Contact:

Re: please help: flickering sprites and hickup/tug in background scroll

Post by DwayneDev » Mon Jul 01, 2019 6:03 pm

Played the game and noticed the flickering your talking about on the cactus. I think you can solve this issue but making the cactus stalks wider than 1 pixel. Make larger / wider sprites and that should get rid of this flickering.

also, use float for all the sprite's vectors. if you move sprites by float you can increment the vector by an amount that is less than an integer which should help make smoother movement.

another factor is the framerate, the higher the framerate the more the sprites get rendered to screen the smoother the movement should look.
Visit my blog or social medias to play or follow my current game projects:
My blog: https://blog.dwaynedev.dev/
My Twitter: https://twitter.com/Animoodl
My itch.io: https://dwaynedev.itch.io/

User avatar
zorg
Party member
Posts: 2735
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: please help: flickering sprites and hickup/tug in background scroll

Post by zorg » Tue Jul 02, 2019 5:17 am

As pgimeno said above, you could solve those issues by math.floor-ing the coordinates for drawing things, that might reduce flicker, although it may look "less smooth" in the process; zooming with whole numbers, and not using nearest-neighbor filtering.

Also, as long as the framerate is higher or equal to the screen's refresh rate, it won't look any more smoother regardless of how high it is.

"By the way, this subforum is for showing your games and creations. The questions normally go in Support and Development."
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

nikgid
Prole
Posts: 2
Joined: Mon Jul 01, 2019 10:50 am

Re: please help: flickering sprites and hickup/tug in background scroll

Post by nikgid » Tue Jul 02, 2019 8:34 pm

Dear pgimeno, DwayneDev and zorg,

thanks a lot for your helpful answers. Thanks especially to pgimeno for the very elaborate reply!
I'm already trying to implement your suggestions. I will play around with the possible changes you pointed out to me and will come back with more questions :-)

Thanks also for telling me about the right place for posting future questions, zorg. Sorry for getting this wrong. If some admin reads this and is willing to move this thread to the right place ... please do! Until that happens: Should I rather continue/reply here in this thread or start another thread in the "Support and Development" forum?

Thanks and cheers
Niklas

User avatar
pgimeno
Party member
Posts: 1912
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

Re: please help: flickering sprites and hickup/tug in background scroll

Post by pgimeno » Tue Jul 02, 2019 9:39 pm

I didn't mention it because, after all, there was a game included in the OP :nyu:

For new questions I'd suggest opening a new thread. If you have more trouble with what you asked in the op (which is likely for what I've tried myself), it's better to continue in this thread.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest