how can I draw the same object in different locations
how can I draw the same object in different locations
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
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
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Re: how can I draw the same object in different locations
The same goes for drawing a rectangle, in case you're asking about shapes rather than images.
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:
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.
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
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
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Re: how can I draw the same object in different locations
Make the object in the object! (actually table in table)
If the object has own x and y, then just use it as
Otherwise just the position of that box.
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,
}
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
Re: how can I draw the same object in different locations
thx you helped me all
Who is online
Users browsing this forum: No registered users and 6 guests