Hi All
Im trying to come up with the most efficient way to do this but can’t seem to get to an end point, tried a while statement but that only compares one value to another.
I have these coordinates on a screen set in a table array:
cordx = {
[1] = 160,
[2] = 260,
[3] = 60
}
cordy = {
[1] = 100,
[2] = 250,
[3] = 400
}
x = cordx[math.random(1,3)]
y = cordy[math.random(1,3)]
But I want to assign x and y to 9 objects on the screen. However, I would like all of them to be placed in a unique set.
e.g.
object 1 = 160 (x), 100 (y)
object 2 = 160 (x), 250 (y)
object 3 = 60 (x), 100 (y)
But no clashes or overlaps of more than 1 object on the same coordinate x & y.
Any help would be greatly appreciated
I think this will help.
Basically make a table with all possible coordinates.
[lua]local cordx = {160, 260, 60}
local cordy = {100, 250, 400}
local allCords = {}
for x = 1, #cordx do
for y = 1, #cordy do
allCords[#allCords+1] = {cordx[x], cordy[y]}
end
end[/lua]
Probably you want it to be random? If so google how to shuffle a Lua table. Then loop through the table and take your coordinates:
[lua]for i = 1, #allCords do
print(“x”, allCords[i][1], “y”, allCords[i][2])
end[/lua]
This is brilliant, thanks a lot. I can shuffle the table but how do I call the pair of values in my object?
num1 = display.newText( 1, 60, 100, native.systemFont, 50 )
num2 = display.newText( 2, 160, 100, native.systemFont, 50 )
num3 = display.newText( 3, 260, 100, native.systemFont, 50 )
Im trying to get each of these to take a pair from the table it says Im getting a Nil value?
So in allCords you have both X and Y.
allCords[1] holds first position and allCords[1][1] holds X and allCords[1][2] holds Y for first position.
In your example:
num1 = display.newText( 1, allCords[1][1], allCords[1][2], native.systemFont, 50 )
num2 = display.newText( 2, allCords[2][1], allCords[2][2], native.systemFont, 50 )
num3 = display.newText( 3, allCords[3][1], allCords[3][2], native.systemFont, 50 )
The above method is a lot of repetitive code which eventually becomes a mess. Think about something like this instead.
local numbers = {}
for i = 1, #allCords do
numbers[i] = display.newText( i, allCords[i][1], allCords[i][2], native.systemFont, 50 )
end
That worked, you’ve been most helpful, yes I know my code is quite messy at the moment, the cleanup will come once done hopefully. Needless to say, I am also quite new to all this so my logical brain is lacking !
Glad to help. I think most important thing to learn sooner rather than later is about tables. Lua will be putty in your hands after that!
I think this will help.
Basically make a table with all possible coordinates.
[lua]local cordx = {160, 260, 60}
local cordy = {100, 250, 400}
local allCords = {}
for x = 1, #cordx do
for y = 1, #cordy do
allCords[#allCords+1] = {cordx[x], cordy[y]}
end
end[/lua]
Probably you want it to be random? If so google how to shuffle a Lua table. Then loop through the table and take your coordinates:
[lua]for i = 1, #allCords do
print(“x”, allCords[i][1], “y”, allCords[i][2])
end[/lua]
This is brilliant, thanks a lot. I can shuffle the table but how do I call the pair of values in my object?
num1 = display.newText( 1, 60, 100, native.systemFont, 50 )
num2 = display.newText( 2, 160, 100, native.systemFont, 50 )
num3 = display.newText( 3, 260, 100, native.systemFont, 50 )
Im trying to get each of these to take a pair from the table it says Im getting a Nil value?
So in allCords you have both X and Y.
allCords[1] holds first position and allCords[1][1] holds X and allCords[1][2] holds Y for first position.
In your example:
num1 = display.newText( 1, allCords[1][1], allCords[1][2], native.systemFont, 50 )
num2 = display.newText( 2, allCords[2][1], allCords[2][2], native.systemFont, 50 )
num3 = display.newText( 3, allCords[3][1], allCords[3][2], native.systemFont, 50 )
The above method is a lot of repetitive code which eventually becomes a mess. Think about something like this instead.
local numbers = {}
for i = 1, #allCords do
numbers[i] = display.newText( i, allCords[i][1], allCords[i][2], native.systemFont, 50 )
end
That worked, you’ve been most helpful, yes I know my code is quite messy at the moment, the cleanup will come once done hopefully. Needless to say, I am also quite new to all this so my logical brain is lacking !
Glad to help. I think most important thing to learn sooner rather than later is about tables. Lua will be putty in your hands after that!