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
--update and draw
--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
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
if btn(⬅️) then
if btn(➡️) then
--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
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.
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.
[Please log in to post a comment]