Store and keep track of colliders x,y - windfield

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.
Post Reply
Willi
Prole
Posts: 3
Joined: Thu Jan 27, 2022 3:16 pm

Store and keep track of colliders x,y - windfield

Post 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!
Attachments
main.lua
(4.6 KiB) Downloaded 86 times
Willi
Prole
Posts: 3
Joined: Thu Jan 27, 2022 3:16 pm

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

Post 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!
Willi
Prole
Posts: 3
Joined: Thu Jan 27, 2022 3:16 pm

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

Post 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.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests