Page 1 of 3

Problem with drawing moving sprites.

Posted: Fri Feb 21, 2014 4:22 pm
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?

Re: Problem with drawing moving sprites.

Posted: Fri Feb 21, 2014 4:41 pm
by Doctory
Incorrect game packaging
It should be a .zip, not .rar.

Re: Problem with drawing moving sprites.

Posted: Fri Feb 21, 2014 4:44 pm
by Moonkis
Doctory wrote:Incorrect game packaging
It should be a .zip, not .rar.
Fixed it! Try download it again :)

Re: Problem with drawing moving sprites.

Posted: Fri Feb 21, 2014 5:08 pm
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.

Re: Problem with drawing moving sprites.

Posted: Fri Feb 21, 2014 9:40 pm
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

Re: Problem with drawing moving sprites.

Posted: Fri Feb 21, 2014 11:32 pm
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!

Re: Problem with drawing moving sprites.

Posted: Sat Feb 22, 2014 2:11 pm
by Doctory
Yes, it works now. I see no problem what so ever here, it may be your computer.

Re: Problem with drawing moving sprites.

Posted: Sat Feb 22, 2014 3:31 pm
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

Re: Problem with drawing moving sprites.

Posted: Sat Feb 22, 2014 3:45 pm
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

Re: Problem with drawing moving sprites.

Posted: Sat Feb 22, 2014 3:54 pm
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?