Log In  


Like the title says, I have rudimentary collision detection on the top and sides of my sprite but not on the bottom.
I'm very new to game dev, but I have some experience in python. I can't see if I have a typo, or I've made a mistake in following the tutorial I watched.

--player--
function create_player()
player={
state="normal",
sprite=1,
health=4,
x=64,
y=82,
h=8,
w=8,
gravity=0.30,
friction=0.15,
inertia=0,
thrust=0.80
}
end

function collide(o)

local x1=o.x/8
local y1=o.y/8
local x2=(o.x+7)/8
local y2=(o.y+7)/8

local a=fget(mget(x1,y1),0)
local b=fget(mget(x1,y2),0)
local c=fget(mget(x2,y2),0)
local d=fget(mget(x2,y1),0)

if a or b or c or d then
return true
else
return false
end
end

function move_player(o)
o.y+=o.gravity --applies player gravity

local lx=o.x --last x pos
local ly=o.y --last y pos

if (btn(❎)) o.y-=o.thrust --player move
if (btn(⬅️)) o.x-=0.5
if (btn(➡️)) o.x+=0.5

--if the player collides, moves back

if collide(o) then
o.x=lx
o.y=ly
end
end

function ani_player(o)
if btn(⬅️) then --player animation
o.sprite=2
elseif btn(➡️) then
o.sprite=3
else
o.sprite=1
end
end

function draw_sprite(o)
spr(o.sprite,o.x,o.y)
end



I'm not sure where your _update() function is but I assume something is calling move_player().. anyway from this snippet it looks as though you are updating o.y with gravity before setting the last y pos, so when you try to move it back you're not undoing the gravity move. Set the last y pos before doing anything that modifies the y pos that frame.


I apologize for the sloppiness of the code. I'm still new to posting here and unsure about how the formatting works, so I copied and pasted from the relevant tab in pico. Thank you. As usual, the fix was apparent when you pointed it out.



[Please log in to post a comment]