Iv'e been messing with love/lua for about 3 days now, and I've hit a snag while trying to make a simple side scrolling beat'em up. I get the error, main.lua: 43: '}' expected (to close'{' at line 2 'draw', and can't seem to figure out what the problem is.
Here is line 43, draw, I know it's probably some of the worst coding you have ever seen, so feel free to point out any other issues. I'm just trying to grasp this programming thing.
draw = function(self)
if self.facing == 1 then }
if self.y <= 480 then self.vspeed = self.vspeed + self.gravity else
love.graphics.draw(self.walking_right, self.x, self.y)
else if
love.graphics.draw(self.falling_right, self.x, self.y)
end
else if self.facing == -1 then
if self.y <= 480 then self.vspeed = self.vspeed + self.gravity else
love.graphics.draw(self.walking_left, self.x, self.y)
else if
love.graphics.draw(sellf, self.x, self.y)
end
else if love.graphics.draw(self.falling_left, self.x, self.y)
-- one case
if test then
-- do something
end
-- two cases
if test then
-- do something
else
-- do something else
end
-- many cases
if test1 then
-- do something
elseif test2 then
-- do something else
elseif test3 then
-- and so on
else
-- getting the idea?
end
(3) spelling
You also have a typo:
love.graphics.draw(sellf, self.x, self.y)
Look carefully at the first argument (sellf)
It looks like you might get some benefit from going over basic lua docs.
What dose, Error in update (arg 2), expected 'float' got nil mean?
function update(self,dt)
if love.keyboard.isDown(love.key_d) then
x = x + (speed * dt)
self.facing = 1
elseif love.keyboard.isDown(love.key_a) then
x = x - (speed * dt)
self.facing = 2
else
facing = 0
end
if love.keyboard.isDown(love.key_s) then
y = y + (speed * dt)
elseif love.keyboard.isDown(love.key_w) then
y = y - (speed * dt)
end
The error says: Error in update (arg 2), expected 'float' got nil - this means that "in the update function (that accepts two parameters) I was expecting a float value, but I got a nil value instead"
This happens because the 'self' parameter is an undefined (nil) variable. update() only needs one parameter (dt). Try removing self so it looks like this:
function test(a, b)
print("a is", a, "b is", b)
end
test(0.1) -- prints: a is 0.1 b is nil
update() is called with one argument, which we usually call dt. If you define more arguments, only the first will contain that value, the rest will be nil. And if a value is nil, you can't do arithmetics with it.