Hi, I'm very new to Pico-8 and coding in general. I've been following some tutorials on how to work in Pico-8, which go over basic elements using separate projects. In order to further my understanding of how the pieces fit together, I wanted to try to incorporate each tutorial into one project. I'm having some trouble though, because my code throws an error when I try to refer to my player class.
This is the code I have in Tab 0, the original code to set up movement and directional sprites for the player:
function _init() player={ x=63, y=63, fx=false, fy=false, sp=1, speed=1 } end function _update() --player movement-- if btn(➡️) then player.x+=player.speed player.sp=9 player.fx=false player.fy=false end if btn(⬅️) then player.x-=player.speed player.sp=9 player.fx=true player.fy=false end if btn(⬇️) then player.y+=player.speed player.sp=1 end if btn(⬆️) then player.y-=player.speed player.sp=5 end if mget(flr((player.x+4)/8),flr((player.y+4)/8))==33 then player.speed=1.2 else player.speed=1 end end function _draw() cls(3) map() spr(player.sp,player.x, player.y,1,1,player.fx, player.fy) print(mget(flr((player.x+4)/8),flr((player.y+4)/8))) end |
That all works fine, but when I add code from the animation tutorial into Tab 1, it throws
RUNTIME ERROR LINE 15 TAB 1
IF PLAYER.SP<LAST_FRAME THEN
ATTEMPT TO INDEX GLOBAL 'PLAYER'
(A NIL VALUE)
function _init() stimer=0 ani_speed=10 first_frame=1 last_frame=4 end function _update() --player animation-- if stimer < ani_speed then stimer+=1 else if player.sp < last_frame then player.sp+=1 else player.sp=first_frame end stimer=0 end end function _draw() end |
(There aren't spaces before and after my '<' signs in the code, I added those so it would display correctly here.)
For some reason, it doesn't recognize the class I defined. I tried putting the code into Tab 0 instead, to the same effect. What am I doing wrong here? I was under the impression that defining variables under a class allows you to change those variables by referencing as CLASS.VAR. Any advice would be appreciated.



You're resetting the _init, _update and _draw functions in the code you paste, so those in tab 0 won't have any effect.



Ahh, for some reason I thought the two tabs would merge functions. But, that makes no sense. Thank you so much!
[Please log in to post a comment]