how can I draw the same object in different locations

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

how can I draw the same object in different locations

Post 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 :)
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: how can I draw the same object in different locations

Post 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
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: how can I draw the same object in different locations

Post 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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: how can I draw the same object in different locations

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: how can I draw the same object in different locations

Post by A-lox »

thx you helped me all :)
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests