Log In  
Follow
Toby-Sanders
Help De-Bug
by
[ :: Read More :: ]

The code is showing a "attempt to preform arithmetic on global 'X1' ( a nil value)" line 30 tab 2. I have tried everything and nothing fixes it. Any suggestions is much appreciated. Please not that the code is not done yet.

pico-8 cartridge // http://www.pico-8.com
version 16
lua
--variables
function _init()
player={
sp=1,
x=59,
y=59,
w=8,
h=8,
flp=false,
dx=0,
dy=0,
max_dx=2,
max_dy=3,
acc=0.5,
boost=4,
anim=0,
running=false,
jumping=false,
falling=false,
sliding=false,
landed=false,
}

gravity=0.3
friction=0.85
end

-->8
--update and draw
function _update()
player_update()
--player_animate()
end

function _draw()

cls()
map(0,0)
spr(player.sp,player.x,player.y,1,1,player.flp)
end
-->8
--collisions
function _collide_map(obj,aim,flag)
--obj
--aim left right up down

local x=obj.x local y=obj.y
local w=obj.w local h=obj.h

local x1=0  
local y=0
local x2=0 

local y2=0

if aim== "left" then
    x1=x-1 y1=y
    x2=x   y2=y+h-2
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
end

--pixels to tiles                ** <----- This is the error**
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

-->8
--player function
function _player_update()
--physics
player.dy+=gravity
player.dx*=friction
end
--controls
if btn(⬅️) then
player.dx-=player.acc
player.running=true
player.flp=true
end
if btn(➡️) then
player.dx+=player.acc
player.running=true
player.flp=false
end

--slide
if player.running
and not btn(⬅️)
and not btn(➡️)
and not player.falling
and not player.jumping then
player.running=false
player.sliding=true
end

--jump
if btnp(❎) 
and player.landed then
    player.dy-=player.boost
    player.landed=false
    end

    --check colldie
    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", 1) then
    plater.d=0
    end
end

    --check collide l and r
    if player.dx<0  then
 collide_map(player,"left",1)       player.dx=0
      elseif player.dx>0 then
    if collide_map(player,"right",1) then
    player.dx=0
    end
    end

    --stop sliding
    if player.sliding then
    if abs(plater.dx)<.2
    or player.running then
    plager.dx=0
    player.sliding=false

end

    player.x+=player.dx
    player.y+=player.dy
    end
P#97248 2021-09-13 00:20 ( Edited 2021-09-13 00:26)