Log In  

I'm having issues with this code, everytime I run it i just get this


runtime error line 34 tab 3
if collide_map(player,"down",0) then
attempt to call global "collide_map" (a nil value)

here's my code:

--external functions--
function dns(sp)
    spr(sp.sp,sp.x,sp.y,sp.w/8,sp.h/8,sp.flp)
end

function player_update()
    --physics
    player.dy+=gravity
    player.dx*=friction

    --running
    if btn(⬅️) then
        player.dx-=player.acc
        player.running=true
        player.flp=true
    end
    if btn(➡️) then
        player.dx+=player.acc
        player.running=true
    end

    --jumping
    if btnp(🅾️) and player.landed then
        player.dy-=boost
        player.landed=false
    end

    --collision on y axis
    if player.dy>0 then
        player.falling=true
        player.landed=false
        player.jumping=false

        if collide_map(player,"down",0) then
            player.landed=true
            player.falling=false
            player.dy=0
            player.y-=(player.y+player.h)%8
        end
    elseif player.dy<0 then
        player.jumping=true

        if collide_map(player,"up",0) then
            player.dy=0
        end
    end

    --collision on x axis
    if player.dx<0 then
        if collide_map(player,"left",0) then
            player.dx=0
        end
    elseif player.dx>0 then
        if collide_map(player,"right",0) then
            player.dx=0
        end
    end

    player.x+=player.dx
    player.y+=player.dy
end
--function player_animate()
--end

here's just the section its calling out:


collide_map(player,"down",0) then
player.landed=true
player.falling=false
player.dy=0
player.y-=(player.y+player.h)%8

can anyone help me out with this

P#126769 2023-03-08 07:14

doing a cmd+f on your code block I don't see a
"function collide_map() ... end" or "collide_map = function() ... end"
If you do define this variable are you sure your doing it before this code is ran? if not debug it out a bit to see what's being ran if it's inlined?

P#126770 2023-03-08 07:20

apologies I missed my collision code

--collisions--
function map_collide(obj,aim,flag)
    --obj must be a table with (x,y,w,h)
    --will not work if aim is unassigned
    local x=obj.x   local y=obj.y
    local w=ob.w    local h=obj.h

    local x1    local y1
    local x2    local y2

    if aim=="left" then
        x1=x-1          y1=y
        x2=x                    y2=y+h-1
    elseif aim=="right" then
        x1=x+w          y1=y
        x2=x+w+1    y2=y+h-1
    elseif aim=="up" then
        x1=x+1          y1=y-1
        x2=x+w-1    y2=y
    elseif aim=="down" then
        x1=x                    y1=y+h
        x2=x+w          y2=y+h
    end

    --pixels to tiles
    x1/=8           y1/=8
    x2/=8           y2/=8

    if fget(mget(x1,y1),flag)   or fget(mget(x1,y2),flag)   or fget(mget(x2,y1),flag)   or fget(mget(x2,y2),flag) then
        return true
    else
        return false
    end
end
P#126773 2023-03-08 07:49

ya that's "map_collide" not "collide_map"
The error is saying "collide_map" is a nil value and it tried to call it using ()

P#126774 2023-03-08 07:59 ( Edited 2023-03-08 08:00)
1

oh shit... thank you, i just didn't see that

P#126782 2023-03-08 13:14

[Please log in to post a comment]