Problem with drawing moving sprites.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Moonkis
Prole
Posts: 14
Joined: Thu Jan 09, 2014 4:37 pm

Problem with drawing moving sprites.

Post by Moonkis »

I'm doing an endless runner kind of thing, but with Wizards and Goblins and shoot em' up elements. I'm quite new to Lua and trying to get the hang of it. The goblins comes swarming from the right to the left in 400px per second. I'v noticed that they shift back a little whenever they move. it looks like they take 3 steps forward and one back each frame, which is really weird.

This intensifies whenever there are higher entity counts on screen (32~ entities). Have anyone else had this problem? I'm not sure how to solve it. I'm uploading the .love files so you guys can see what I mean.

EDIT:

Move with WASD and shoot with spacebar!


EDIT 2:

This seems only to happen when shooting bullets, I adjusted the spawner to spawn in the interval of 0.01 which spawns enough goblins to always update and render 136 objects.

EDIT 3:

If I remove the check inside bullet that sets the alive status to false ( causing it to not get removed from a table ) then the problem disapears. I think it is how I remove objects from the game ... is it a bad idea to call table.remove?
Attachments
Dungeon Pew Pew.love
(8.45 KiB) Downloaded 221 times
Last edited by Moonkis on Fri Feb 21, 2014 11:53 pm, edited 5 times in total.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Problem with drawing moving sprites.

Post by Doctory »

Incorrect game packaging
It should be a .zip, not .rar.
Moonkis
Prole
Posts: 14
Joined: Thu Jan 09, 2014 4:37 pm

Re: Problem with drawing moving sprites.

Post by Moonkis »

Doctory wrote:Incorrect game packaging
It should be a .zip, not .rar.
Fixed it! Try download it again :)
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Problem with drawing moving sprites.

Post by Doctory »

Moonkis wrote:
Doctory wrote:Incorrect game packaging
It should be a .zip, not .rar.
Fixed it! Try download it again :)
Take out all the files from Dungeon Pew Pew folder and put it in the zip.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Problem with drawing moving sprites.

Post by davisdude »

I haven't looked at it yet, but I'd be willing to put money on the fact that it's your drawings. How they're made to quads or whatever. That's probably it.

*(On a side note, if that's not the problem, don't expect any money) :P
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Moonkis
Prole
Posts: 14
Joined: Thu Jan 09, 2014 4:37 pm

Re: Problem with drawing moving sprites.

Post by Moonkis »

I think so too, hopefully someone could explain what I'm doing wrong!

I updated the .love file! HOPEFULLY this is correct! :P

Thanks in advance!
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Problem with drawing moving sprites.

Post by Doctory »

Yes, it works now. I see no problem what so ever here, it may be your computer.
Moonkis
Prole
Posts: 14
Joined: Thu Jan 09, 2014 4:37 pm

Re: Problem with drawing moving sprites.

Post by Moonkis »

Doctory wrote:Yes, it works now. I see no problem what so ever here, it may be your computer.
Have you tried spamming bullets (Holding spacebar)? Its then the problem arises. I "kind" of fixed this, by basically move the table.remove(...) into it's own loop which runs after the update-loop.

Is table.remove a bad idea to call WHEN iterating over the loop itself?

I'v basically isolated the problem to this piece of code (in EntityManager.update(dt))

Code: Select all

	for i, e in pairs(EntityManager.objects) do
		if e.update then
			e:update(dt)
                        if e:isAlive() == false then
                           table.remove(EntityManager.objects, i)
                        end
		end
	end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Problem with drawing moving sprites.

Post by davisdude »

It depends on how you're iterating. If you have a loop that starts at the end and iterates until the beginning adding -1 each time, it's harmless. Now if you have a normal for-loop, you're really messing it up. Here's what I mean:

Code: Select all

Fruits = {
     { Color = { 255, 0, 0, 255 }, Taste = 'Great!', Size = 64 }, -- Apple
     { Color = { 255, 255, 0, 255 }, Taste = 'Awful', Size = 32 }, -- Lemon
     { Color = { 255, 0, 255, 255 }, Taste = 'Great!', Size = 2 } }, -- Grape
}

function FruitLoop( Table ) -- Yes, intentional pun here. :)
     for a = 1, #Table do
          local Fruit = Table[a]
          if Fruit.Taste = 'Great!' then
               table.remove( Table, a )
          end
     end
end
What happens here is the original Fruits[2] (lemon) is not even touched in the first iteration. Here's how the table would look during this:

Code: Select all

a = 1
#Fruits = 3
Fruits[1] has a taste of 'Great!' so it's removed. Now the table is
Fruits = {
     { Color = { 255, 255, 0, 255 }, Taste = 'Awful', Size = 32 }, -- Lemon, 1st index
     { Color = { 255, 0, 255, 255 }, Taste = 'Great!', Size = 2 } }, -- Grape, 2nd index
}

a = 2 (Accessing Grape)
#Fruits = 2
Notices that Grape tastes great too, and removes it.
To fix this problem, iterate like this:

Code: Select all

function FruitLoop( Table ) -- Yes, intentional pun here. :)
     for a = #Table, 1, -1 do -- Starts accessing at 3. Goes until 1, removing 1 from the index each time.
          local Fruit = Table[a]
          if Fruit.Taste = 'Great!' then
               table.remove( Table, a )
          end
     end
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Moonkis
Prole
Posts: 14
Joined: Thu Jan 09, 2014 4:37 pm

Re: Problem with drawing moving sprites.

Post by Moonkis »

davisdude wrote:It depends on how you're iterating. If you have a loop that starts at the end and iterates until the beginning adding -1 each time, it's harmless. Now if you have a normal for-loop, you're really messing it up. Here's what I mean:

Code: Select all

Fruits = {
     { Color = { 255, 0, 0, 255 }, Taste = 'Great!', Size = 64 }, -- Apple
     { Color = { 255, 255, 0, 255 }, Taste = 'Awful', Size = 32 }, -- Lemon
     { Color = { 255, 0, 255, 255 }, Taste = 'Great!', Size = 2 } }, -- Grape
}

function FruitLoop( Table ) -- Yes, intentional pun here. :)
     for a = 1, #Table do
          local Fruit = Table[a]
          if Fruit.Taste = 'Great!' then
               table.remove( Table, a )
          end
     end
end
What happens here is the original Fruits[2] (lemon) is not even touched in the first iteration. Here's how the table would look during this:

Code: Select all

a = 1
#Fruits = 3
Fruits[1] has a taste of 'Great!' so it's removed. Now the table is
Fruits = {
     { Color = { 255, 255, 0, 255 }, Taste = 'Awful', Size = 32 }, -- Lemon, 1st index
     { Color = { 255, 0, 255, 255 }, Taste = 'Great!', Size = 2 } }, -- Grape, 2nd index
}

a = 2 (Accessing Grape)
#Fruits = 2
Notices that Grape tastes great too, and removes it.
To fix this problem, iterate like this:

Code: Select all

function FruitLoop( Table ) -- Yes, intentional pun here. :)
     for a = #Table, 1, -1 do -- Starts accessing at 3. Goes until 1, removing 1 from the index each time.
          local Fruit = Table[a]
          if Fruit.Taste = 'Great!' then
               table.remove( Table, a )
          end
     end
end
Why isn't Fruit[2] touched here? Shouldn't it? Also I'm using a generic loop (think what it's called)

Code: Select all

for i, v in pairs(table) do
  -- code
end
EDIT:

Oh, is it because that a = 1 then we remove an item, the table is resized, which means lemon is now at index 1, and we move onto index 2, thus "skipping" lemon?
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests