Newbie help with ipairs
Posted: Sat Apr 29, 2023 8:35 am
Hi,
I'm very new to Love2d and am struggling with ipairs, hopefully someone can help!
I'm trying to iterate through a table of cards to check whether or not a card has the value "isClicked", set to true. Then if so, I want the x and y of the card to be the same as the mouse cursor.
Essentially, if I've clicked on the card, I want it to follow the mouse around.
I can do this without using a table using:
However when trying to do this in an ipairs iteration, like this:
I get the following error:
Error
Syntax error: main.lua:74: 'end' expected (to close 'function' at line 33) near '<eof>'
Traceback
[love "callbacks.lua"]:228: in function 'handler'
[C]: at 0x7ff952b231d0
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
Am I not supposed to be doing "if ... then do" inside an ipairs iteration?
I'm very new to Love2d and am struggling with ipairs, hopefully someone can help!
I'm trying to iterate through a table of cards to check whether or not a card has the value "isClicked", set to true. Then if so, I want the x and y of the card to be the same as the mouse cursor.
Essentially, if I've clicked on the card, I want it to follow the mouse around.
I can do this without using a table using:
Code: Select all
function love.update(dt)
mouseX, mouseY = love.mouse.getPosition()
if card.isClicked == true then
card.x = mouseX - card.width / 2
card.y = mouseY - card.height / 2
end
end
Code: Select all
function love.update(dt)
mouseX, mouseY = love.mouse.getPosition()
for i,v in ipairs(cards) do
if v.isClicked == true then do
v.x = mouseX - v.width / 2
v.y = mouseY - v.height / 2
end
end
end
Error
Syntax error: main.lua:74: 'end' expected (to close 'function' at line 33) near '<eof>'
Traceback
[love "callbacks.lua"]:228: in function 'handler'
[C]: at 0x7ff952b231d0
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
Am I not supposed to be doing "if ... then do" inside an ipairs iteration?