Hi. I'm trying to learn about collisions using the DOM8VERSE section of zine 3 but I'm getting weird error messages whenever I try and use a foreach.
For example, when I try this:
function _update()
if btnp(5, 0) then
add(objects, bulletconstruct(player1.position.x,player1.position.y))
end
foreach(objects, function(obj)
obj.update(obj)
end
end
.. I get ')' expected ( to close '(' at line 0.0003891 near 'end'
I've tried adding and taking away brackets, as well as 'end' statements but I can't get it to work. Thought it was strange as I'm copy pasting zine 3 code.
Anyone know the solution?
You need a closing parenthesis after the "end" of the function used inside the foreach. And yes this is an error in the Picozine #3.
foreach(objects, function(obj) obj.update(obj) end) |
Explanation: foreach() is a built-in that takes an array as its first argument and a function as its second argument. In Lua, you can define a named function elsewhere then use the name of that function as the second argument, or you can do what this code does and define an anonymous function where the argument goes. The anonymous function is defined with "function(obj) ... end". Because this function is the second argument to foreach(), so there must be a closing parenthesis after the "end".
[Please log in to post a comment]