0
\$\begingroup\$

Obviously I could just copy and paste a love.image.draw() function multiple times for how many times I want the item to be drawn, but lets say I wanted to draw an image wherever the mouse is clicked at. How would I go about making it so it draw said item every time I clicked. If I only clicked twice it would draw two objects, and if I clicked it 500 times it would draw 500 items, without having to have 500 different draw functions?

\$\endgroup\$
-1
\$\begingroup\$

So you probably want to keep a list of positions somewhere, and each time the mouse is clicked, add a position to the list.

Then, when you're doing your rendering pass, you'll just iterate over the positions in the list, and draw an image there. This approach can be then later expanded to store other data too inside a table, like a sprite index or something similar.

Here's some pseudoish Lua code, I'm not familiar with love2d but I'm sure you can figure out the correct replacements for my imaginary framework calls.

positions = { }

function update()
    if mouse.wasJustClicked() then
        table.insert(positions, mouse.getPosition())
    end
end

function draw()
    for key, position in pairs(positions) do 
        love.image.draw(position)
    end
end
\$\endgroup\$
  • \$\begingroup\$ sounds like good code, though i am not sure if that would work in love2d. in love2d the draw function draws the image that frame exactly. so if you had 1 item it would draw fine, but i think after you got enough items the images would start to "flash" ill try it out though \$\endgroup\$ – dyl4n130 Aug 29 '17 at 0:56
  • \$\begingroup\$ hey I would just like to say I gladly ate my words. I was using another solution someone else gave me that was super complicated (it was really solid code, but too advanced for me) and was having a lot of trouble with it, your way worked perfectly fine and actually makes sense to me, so thanks a ton for that \$\endgroup\$ – dyl4n130 Aug 29 '17 at 4:33
  • \$\begingroup\$ Happy I could help out! \$\endgroup\$ – Tyyppi_77 Aug 29 '17 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.