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
I think maybe you have an extra "end" in there. Where you have this:
x1=x y1=y+h x2=x+w y2=y+h end end |
The second "end" is closing your _collide_map function where you've defined x1 as local, so the line after the second "end" is looking for a global called x1 which doesn't exist. Try removing that second "end" and see if it fixes the error.
yes, thank you that worked. Although now there is another error saying "attempt to index global variable 'player; (a nil value). I am very new to this so thank you for any help.
You just misspelled "player" in a couple of spots - you have "abs(plater.dx)" and "plager.dx=0" near the end of your code.
It is still saying the same thing. And by the way, thanks for all of the help
It looks like you also end your _player_update() function after doing the gravity & friction - all of the button press code comes after the "end" for that function, it probably all needs to live inside that function.
It also looks like you're calling your collide_map() function without the "if" in your l and r collision check area. You probably didn't mean to do that. Probably the best thing you can do is just slowly read through your code & check for little things like that, make sure all of your if/elseif/ends nest up correctly, maybe make all the indentation of the code consistent so you can see things in blocks - it'll make it easier to spot when something doesn't match up. As it is now, the formatting here makes it hard to catch everything. If you do a little code cleanup and post your code here using the code formatting notation (you can use the Preview button before posting to get formatting help), it'll make it easier for people to help you. But I think if you take a slow look through what you've written and check for the things I mentioned, you'll be able to get this working!
Oh, one other thing I just spotted: you've named your functions _player_update() and _collide_map() but when you call the functions, you're calling player_update() and collide_map() without the leading underscore, so you'll have to either change those calls or change the function names for those to work.
Wow, thank you for all of this help. I will go and make the changes now
[Please log in to post a comment]