I'm getting an error.
SYNTAX ERROR LINE 19
PLAYER.SP=2
UNCLOSED FUNCTION
I can't figure it out and here's my code.
function _init() player={ sp=1, x=59, y=59, w=1, h=1, flp=false, speed=2, walking=false } end function _update() move() if walking==true then player.sp=2 end else then player.sp=1 end end function _draw() cls() spr(player.sp,player.x,player.y,player.w,player.h,player.flp) end function move() if btn(0) then player.x-=player.speed player.walking=true end else if btn(1) then player.x+=player.speed player.walking=true end else if btn(2) then player.y-=player.speed player.walking=true end else if btn(3) then player.y+=player.speed player.walking=true end else player.walking=false end end |
2
oof, the error message isn't very good at surfacing the syntax papercut here... this is a problem revolving around the if-else statement syntax, which can be a bit convoluted.
else
blocks replace theend
of anif
block with another block, complete with its ownend
keyword. without anif
block immediately preceding them,else
blocks are syntax errors.
if thing then -- stuff else -- other stuff end |
else if
needs to be combined into theelseif
keyword to prevent some counterintuitive syntax (secretly it's making an if block inside the else block, thus it needs twoend
keywords in a row to terminate it)
if thing then -- do something else if other_thing then -- do something else end end -- two 'end's!! -- does the same thing as if thing then -- do something elseif other_thing then -- do something else end |
here's a version of your _update
and move
functions with the fixed syntax:
function _update() move() if walking==true then player.sp=2 else player.sp=1 end end |
function move() if btn(0) then player.x-=player.speed player.walking=true elseif btn(1) then player.x+=player.speed player.walking=true elseif btn(2) then player.y-=player.speed player.walking=true elseif btn(3) then player.y+=player.speed player.walking=true else player.walking=false end end |
[Please log in to post a comment]