Hi All,
I've got a noob question I'm afraid and sorry this is such a long post! I've tried searching the forums and wider internet but haven't been able to find an answer my tiny brain can understand! I'm pretty new to coding and very new to Pico 8, loving it so far and just trying to clone a few basic games as a learning exercise.
Currently working on a Frogger-like, but I'm really struggling to make the 'log riding' mechanic work. I'm using AABB collisions and have a hit box defined for the frog and the water area. The plan is then to make touching the water kill the frog unless it's also colliding with a log hit box.
I'm creating the frog as an object within a table during init, then using a build_level function in init to add the logs to another table as below.
function build_level()
add(logs,{sp=24,x=16,y=24,w=3,h=1,dx=1,spd=0.5,ride=false})
add(logs,{sp=24,x=64,y=24,w=3,h=1,dx=1,spd=0.5,ride=false})
add(logs,{sp=24,x=112,y=24,w=3,h=1,dx=1,spd=0.5,ride=false}) --there's more in the full cart, but you get the idea
end
I'm then trying to iterate through this to draw, move and interact with them. I have log_draw and log_move functions called in game update/draw which are working fine, but the problem comes when I try to deal with collisions, which again is within a collision function called in game update.
I'm defining hit boxes for the frog and water area first, then iterating through the table of logs to define their hit boxes and check for collisions as below.
for l in all (logs) do
log_hb=hit_box(l.x,l.y+1,(l.w8)-1,(l.h8)-3) --defines bounds of log hit box
frog_log_hit = hit_detect(frog_hb,log_hb) --checks for collisions between frog and log HBs
if frog_log_hit then
if frog.x < 112 then -- sets frog velocity equal to log velocity without going out of bounds
frog.x+=l.cx
end
l.ride=true -- sets boolean on the log object
frog.safe=l.ride -- sets boolean on the frog object
else
l.ride=false
frog.safe=l.ride
end
end
I know the collisions are being detected as the frog velocity does change whenever it's colliding with any log, I also have a debug log (press 'd' to view it if running the cart) which tells me that the l.ride value is being set whenever a collision happens. The problem is when I try to transmit the collision detection outside of the loop. frog.safe gets set correctly if I'm on the first log in the table, but not for any other. I've tried using l.ride to set a global variable (g_safe) outside the frog object too, but the issue is the same.
I'm guessing this is because although l.ride is true for the specific log where the collision is happening, all the others are returning false. I just don't know how to stop that happening!
Thanks for taking the time to read such a long post, I tried to describe the bug as well as I can! Any advice would be very much appreciated.



My guess is that once you collide with any log, you need to stop checking the rest and exit your for loop using a break statement. Once the frog is on one log, you know it's not on the others so no need to check them. I think you need to do that after setting l.ride and frog.safe to true:
l.ride=true -- sets boolean on the log object frog.safe=l.ride -- sets boolean on the frog object break -- add this |
[Please log in to post a comment]