Page 1 of 1

Store and keep track of colliders x,y - windfield

Posted: Thu Jan 27, 2022 3:31 pm
by Willi
Hello guys,
I started learning lua and love2d this month. In my little game you can controll a character on the left hand side of the screen and move him up and down. Arrows spawn randomly on the right side and fly towards the player.

My problem is i dont know how to create independent colliders and keep track of the x and y coordinates. The script creates one collider and if a second one would spawn it crashes because i think it uses the same colider for the next arrow.

I tried almost 6 hours to get the result that i have now and dont know hot to solve this problem. Ive tried but not succeded.
I searched on forums, read the windfield github manuel, tried a lot, but nothing worked.

File is attached. Thank you!

Re: Store and keep track of colliders x,y - windfield

Posted: Thu Jan 27, 2022 5:42 pm
by Willi
I have read that you should store the colliders in a table and delete them after a set amount of time. I have tried that but could not work it out... I keep trying!

Re: Store and keep track of colliders x,y - windfield

Posted: Thu Jan 27, 2022 6:24 pm
by Willi
Found a solution!

for _,a in pairs(arrow_flug) do
a.x = a.x - 5
if a.collider:enter('Player') then
player.leben = player.leben - 1
table.remove(arrow_flug, _)
a.collider:destroy()
elseif a.x < 10 then
table.remove(arrow_flug, _)
a.collider:destroy()
player.punkte = player.punkte + 1
end
end

No problems so far.

Re: Store and keep track of colliders x,y - windfield

Posted: Fri Jan 28, 2022 8:04 am
by darkfrei
Willi wrote: Thu Jan 27, 2022 6:24 pm Found a solution!

Code: Select all

for _,a in pairs(arrow_flug) do
        a.x = a.x - 5
        if a.collider:enter('Player') then
            player.leben = player.leben - 1
            table.remove(arrow_flug, _)
            a.collider:destroy()
        elseif a.x < 10 then
            table.remove(arrow_flug, _)
            a.collider:destroy()
            player.punkte = player.punkte + 1
        end
    end
No problems so far.
Why you don't use the dt? So the speed is:
vx = vx + ax*dt
And the position is:
x = x + vx*dt

Grüße