how i get the x, y, width, height of something draw

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
vitail
Prole
Posts: 26
Joined: Thu Apr 09, 2015 8:37 pm

how i get the x, y, width, height of something draw

Post by vitail »

if i draw in the main.lua a rectangle, like this

Code: Select all

player = love.graphics.rectangle('fill', x, y, 32, 32)
i can get something like the x, y position or width and height with a function or something in love?
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: how i get the x, y, width, height of something draw

Post by airstruck »

love.graphics.rectangle doesn't return anything, so assigning the result to a variable is not useful. That function draws a rectangle, it doesn't create a rectangle object. Store x/y/w/h elsewhere and pass the values to the rectangle function instead.
szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

Re: how i get the x, y, width, height of something draw

Post by szensk »

no. Love doesn't store that information. You should store the x,y, width, and height yourself.

Code: Select all

local rects = {}

function love.load()
  table.insert(rects, {x=100,y=100,w=200,h=50})
  for i=1,10 do -- let's add 10 random rectangles
    table.insert(rects, {x=math.random(0,600),y=math.random(0,480),w=32,h=32})
  end
end

function love.draw()
  for index, rect in ipairs(rects) do 
    love.graphics.rectangle('fill',rect.x,rect.y,rect.w,rect.h) 
  end
end
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests