Page 1 of 1
how can I draw the same object in different locations
Posted: Fri May 20, 2022 10:56 am
by A-lox
Hello I am A-lox and I am new to love2d I need to draw a box in diferent location and there are no totorial how to do that so I am stuck I would rilly like it if someone helped me with this
Re: how can I draw the same object in different locations
Posted: Fri May 20, 2022 11:44 am
by ReFreezed
Just call the drawing function several times with different coordinates.
Code: Select all
local image = love.graphics.newImage("myImage.png")
function love.draw()
-- Draw the same image twice in different places.
love.graphics.draw(image, 0, 0)
love.graphics.draw(image, 50, 90)
end
Re: how can I draw the same object in different locations
Posted: Fri May 20, 2022 5:27 pm
by milon
The same goes for drawing a rectangle, in case you're asking about shapes rather than images.
Code: Select all
function love.draw()
-- Draw the same rectangle twice in different places.
love.graphics.rectangle("line", 10, 20, 80, 80)
love.graphics.rectangle("line", 100, 120, 80, 80)
end
Although in actual practice you wouldn't want to hardcode your coordinates like that. You'd want to use variables instead to be able to update the size, etc. Something like this:
Code: Select all
local box = {}
box.x1 = 10
box.y1 = 20
box.x2 = 100
box.y2 = 120
box.w = 80
box.h = 80
function love.draw()
-- Draw the same rectangle twice in different places.
love.graphics.rectangle("line", box.x1, box.y1, box.w, box.h)
love.graphics.rectangle("line", box.x2, box.y2, box.w, box.h)
end
That way you're drawing 2 boxes with the same color, width, and height. The only thing that changes is the location you draw them at, and that can be changed just by updating the coordinte variables.
Re: how can I draw the same object in different locations
Posted: Mon May 23, 2022 9:46 am
by darkfrei
Make the object in the object! (actually table in table)
Code: Select all
object = {
x = 0, y = 0, w = 20, h = 40,
image = love.graphics.newImage("myImage.png"),
}
box1 = {
x = 80,
y = 30,
object = object,
}
box2 = {
x = 200,
y = 300,
object = object,
}
If the object has own x and y, then just use it as
Code: Select all
for i, box in ipairs ({box1, box2}) do
local x = box.x + box.object.x
local y = box.y + box.object.y
love.graphics.draw(box.object.image, x, y)
end
Otherwise just the position of that box.
Re: how can I draw the same object in different locations
Posted: Mon Jun 20, 2022 6:13 pm
by A-lox
thx you helped me all