Position?

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
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Position?

Post by Findo777 »

How can I change the position of a text/image?


ex:

--defined image
--made image called image


so...

Say I had this defined image, could I change it's position?
like...

image.Position = (200,100)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Position?

Post by micha »

An image itself has not position. You assign a position in the moment you draw it.

Code: Select all

love.graphics.draw(Image,x,y)
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: Position?

Post by Wojak »

An image by it self don't have a position property, to solve it you need to create a custom object that has all the property You need. The best way is using tables:

Code: Select all

local allobjects = {} -- an empty table for all objects
image1= love.graphics.newImage('someimage1.png')
image2= love.graphics.newImage('someimage2.png')
allobjects[1] = { --image1 with coordinates 10,0
     x=10,
     y=0,
     image=image1,
}
allobjects[2] = { --image1 with coordinates 10,50
     x=10,
     y=50,
     image=image1,
}
allobjects[3] = { --image2 with coordinates 10,100
     x=10,
     y=100,
     image=image2,
}
allobjects[4] = { --image2 with coordinates 10,150
     x=10,
     y=150,
     image=image2,
}
function love.draw()
	for i,object in ipairs(allobjects) do --this is for with iterator, it will execute an operation for every object in  allobjects table
		love.graphics.draw(object.image,object.x,object.y) 
	end
end
to change the position you can do:

Code: Select all

allobjects[2].x,allobjects[2].y = 200,100
I hope this helps, but I would recommend reading about lua tables if You haven't already
Findo777
Prole
Posts: 36
Joined: Tue Feb 25, 2014 4:09 am

Re: Position?

Post by Findo777 »

Yes, I have learned of arrays, but for the ipairs part, I have noticed pairs works the same as ipairs.
Is there any difference?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Position?

Post by Robin »

Findo777 wrote:Is there any difference?
There are two:
  • pairs goes over every key in the table. ipairs only loops over the array part (basically, 1 to #t).
  • ipairs starts with the value at 1 and goes up 1 every time. pairs loops over the keys and values in an undefined order.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 58 guests