Log In  


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
1


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 the end of an if block with another block, complete with its own end keyword. without an if block immediately preceding them, else blocks are syntax errors.
if thing then
  -- stuff
else
  -- other stuff
end
  • else if needs to be combined into the elseif keyword to prevent some counterintuitive syntax (secretly it's making an if block inside the else block, thus it needs two end 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

Thank you.



[Please log in to post a comment]