Could be me been tired. But I don't get why my character is falling through the map.
I thought I had done some basic collision detection, after following some tutorials. However it doesn't seem to work and my character simply falls straight through the map.
--player variables p1= { --initial sprite start --point and velocities x=64, y=24, vx=0, vy=0, isgrounded=false, grav=0.2, } function _update() --left and right movement p1.dx=0 if btn(0) then --left p1.dx=-2 end if btn(1) then --right p1.dx+=2 end p1.x+=p1.dx --gravity p1.vy+=p1.grav p1.y+=p1.vy local v=mget((p1.x+4)/8,(p1.y+8)/8) p1.isgrounded=false if p1.vy>=0 then --look for a solid tile if fget(v,0) then --place p1 on top of tile p1.y = flr((p1.y)/8)*8 --halt velocity p1.dy = 0 --allow jumping again p1.isgrounded=true end end end function _draw() cls() map(0,0,0,0,128,128) spr(48,p1.x,p1.y) end |
Do you need to flr your mget arguments? I'm not sure what rounding would do to that. I would print v to check.
It is coming back as nil.
How can I get it to check for a filled tile on a map?
I have the gravity and movement sorted.
Remove the "local". Otherwise you're not going to see v outside of its local scope.
Ok so one step closer lol.
V remains as 0 until it gets about a tile close to a filled tiles. Then it shoots up to 38.
V will just be the number of the Sprite used for the map tile (so I presume you might be using Sprite 38 as a background tile?). Fget is then checking to see if that Sprite has a flag set. You need to set a flag, then look for that (I believe).
Set the first flag of your background sprites (you set this in the Sprite editor), then see if your code works.
So Sprite 38 has its 0 flag set? Yeah what blue said, though I think Sprite 38 would be the ground.
in "if fget(v,0) then" you should do "p1.vy=0"
otherwise the vertical velocity would finish to be so high that it would be higher than 1 sprite/frame, so it would normal it pass through the ground.
Which is exactly what I found lol.
The guy sits there for a couple of frames then decides to fall off the face of planet, so to speak.
Actually where do I put the p1.vy=0?
When I replace anything in there I get a syntax error.
Sorry. I am trying to learn and this is helpful.
Another safety could be to set a maximum fall value, to avoid issues in case you are falling from very high.
--gravity
p1.vy+=p1.grav
if (p1.vy>3) p1.vy=3 --new
That was the other thing I want to look into. But for the moment I want to try and get the biggest mechanic in the game done.
But thanks all for your help!
It has bee frustrating but rewarding lol.
Now for a whisky.
[Please log in to post a comment]