Domanda

I'm making a basic game to amuse my friends. I haven't called it anything yet, but it's basically a square chasing another square. The red square is AI, and the white square is player-controlled. I've got the collision all figured out on that, and it works perfectly.

I decided to make a 2 player version of this game. So I just copied the files over to another folder and transformed it into a 2 player game. Everything works fine, except the collision. I have the exact same collision as the singleplayer version, but for some reason, the collision doesn't always work. It's glitchy, and sometimes it only counts if they touched the player's right side, or sometimes it's the left side. There's no determining what happens.

Anyway, here's my death checking code. I call it in the main.lua, in love.update().

death.check=function()
    for _,enemy in ipairs(enemy) do
        for _,player in ipairs(player) do
            if ((enemy.x+enemy.w>player.x and enemy.x+enemy.w<(player.x+player.w)) and
            (enemy.y+enemy.h>player.y and enemy.y+enemy.h<(player.y+player.h))) then
                death.state=true
            end
        end
    end
end
È stato utile?

Soluzione

I found the answer, like this:

for _,o in ipairs(enemy) do
    for _,i in ipairs(player) do
        if (o.x+o.w>i.x and o.x<i.x+i.w and
        o.y+o.h>i.y and o.y<i.y+i.h)  then
            death.state=true
        end
    end
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top