tables

so im learning about tables and its all good but i do have a question, please if your gonna answer dont refer me to a link, if i understood by reading i wouldnt be asking,

anyway i was reading this article

http://www.coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

i understand the post but when i get  to the bottom example, how would you display these objects on the screen without them overlapping, i made a table with images and displayed it on the screen but my images overlap, how would i make each individually appear on different parts of the screen

Example from article local myTable = { "blue", "red", "yellow", "green", "white", "purple" } for i=1,#myTable do print( myTable[i] ) end

everything is fine with the example above when you are printing, but what about when you are displaying them to the simulator they will overlap each other, how do you avoid this?

this is my example

local myTable = {}

myTable[1] = “yellowGreen.png”
myTable[2] = “pinkDot.png”
for i=1,#myTable do
display.newImage(myTable[i])
end
 

images here are overlapping, how do you fix

I’ll ask you - if you put two pieces of paper on each other then how you expect them not to overlap? There are two things you can do - bring one to back or front, move them on the screen in such way that they are not overlapping.

thats exactly what im asking, that is why i asked, HOW do you do that?

To adjust display objects location you need to change their x and y values.  So if you are iterating through a table via a loop then you’ll need change their location based on the iterator.  For example:

for i = 1, #myTable do local img = display.newImage(myTable[i]) img.x = i \* 10 img.y = 100 end

i tried what you told me and there is an error, says"attempt to index local ‘img’ (a nil value) liine 20

local myTable = {}


myTable[1] = “yellowGreen.png”

myTable[2] = “pinkDot.png”

myTable[3] = “orangeDot.png”

myTable[4] = “blueDot.png”



for i=1,#myTable do

img = display.newImage(myTable[i])

line 20… local img.x=i*10

img.y=100

end

Please try my code again.  I did not type “local img.x”

sorry that was mt typo, i copied pasted this time, this is what i have and it does not work

local myTable = {}


myTable[1] = “yellowGreen.png”

myTable[2] = “pinkDot.png”

myTable[3] = “orangeDot.png”

myTable[4] = “blueDot.png”



for i=1,#myTable do

local img = display.newImage(myTable[i])

img.x=i*10

img.y=100

end

Try this:

local myTable = {} myTable[1] = display.newImage("yellowGreen.png") myTable[2] = display.newImage("pinkDot.png") myTable[3] = display.newImage("orangeDot.png") myTable[4] = display.newImage("blueDot.png") for i=1,#myTable do myTable[i].x=i\*10 myTable[i].y=100 end

You the MAN, HAHA thanks.

it worked, just one smaller problem it only display 2 of my images

Make sure the image filenames are correct.  Also, you will have to adjust this line: “myTable[i].x = i * 10”  to however big your images are.  Just change the number 10 around until you find a number that works for your images.  Same with the y coordinate

its like your psychic, thanks again, my other two names were wrong, you should teach tutorials, im gonna add you as a friend

:slight_smile:

Nah not psychic, just like many others I’ve learned from making the same mistakes :slight_smile:

Hi JoelG411.  Please read this blog post:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Many of the issues above you might have been able to determine by looking at the error messages generated by Corona in your console log file, like those image names being incorrect.   It really helps the people on here trying to help you if we know if there are errors going on.

Moving display objects is a pretty common thing.    You can also go to our Tutorial page: Corona University

http://www.coronalabs.com/resources/tutorials/images-audio-video-animation/

And look at the Loading Images video and refer to other information here.

I had to laugh at this line:

please if your gonna answer dont refer me to a link, if i understood by reading i wouldnt be asking,

I’m wondering how you managed to digest the information that JonPM wrote on the forum? 

its like your psychic

Ah, that’s how  :) 

haha

I’ll ask you - if you put two pieces of paper on each other then how you expect them not to overlap? There are two things you can do - bring one to back or front, move them on the screen in such way that they are not overlapping.

thats exactly what im asking, that is why i asked, HOW do you do that?

To adjust display objects location you need to change their x and y values.  So if you are iterating through a table via a loop then you’ll need change their location based on the iterator.  For example:

for i = 1, #myTable do local img = display.newImage(myTable[i]) img.x = i \* 10 img.y = 100 end

i tried what you told me and there is an error, says"attempt to index local ‘img’ (a nil value) liine 20

local myTable = {}


myTable[1] = “yellowGreen.png”

myTable[2] = “pinkDot.png”

myTable[3] = “orangeDot.png”

myTable[4] = “blueDot.png”



for i=1,#myTable do

img = display.newImage(myTable[i])

line 20… local img.x=i*10

img.y=100

end

Please try my code again.  I did not type “local img.x”