random position

Hi Friends,

I am developing a game in which i have 20 images and 20 positions for that. now i wanted to assign random positions to all my images. so every time game is played it shows the images at different positions.

Hoping for any reply soon.

Regards
Varun [import]uid: 130269 topic_id: 26329 reply_id: 326329[/import]

What’s your question? [import]uid: 8271 topic_id: 26329 reply_id: 106753[/import]

hi

my question is that how can i do the thing mention above?

Regards
Varun [import]uid: 130269 topic_id: 26329 reply_id: 106760[/import]

Load the images and use the math api to generate random values.

https://developer.anscamobile.com/reference/index/displaynewimage

https://developer.anscamobile.com/content/math-0 [import]uid: 8271 topic_id: 26329 reply_id: 106764[/import]

Hi

Thanks for your advice but i dont wont same image loading twice so how shall i code that i will get all the different images at different position. [import]uid: 130269 topic_id: 26329 reply_id: 106782[/import]

Calling:

[lua]local a = dsiplay.newImage(“image.png”)
local b = dsiplay.newImage(“image.png”)
local c = dsiplay.newImage(“image.png”)[/lua]

Only loads the “image.png” image once - Corona handles this for you.

If you don’t want to /display/ the same image more than once - well, that’s up to the logic you code in your program. I would put the names in a list and loop once through the list, positioning each image at a random location, while checking to see if the location is not already taken.

How you handle the logic in your program is up to you. [import]uid: 8271 topic_id: 26329 reply_id: 106788[/import]

Hi

Thanks for the advice i will try it for sure.

Regards
Varun [import]uid: 130269 topic_id: 26329 reply_id: 106789[/import]

I’m still not quite sure what you mean by 20 random positions. Do you have 20 designated locations, like say a grid of 4x5 blocks and you want your 20 images to randomly fill those spaces or do you want to have 20 images show up and random X, Y locations and the X, Y isn’t fixed?

The later is hyper easy:

local images = {}  
for i = 1, 20 do  
 images[i] = display.newImageRect(string.format("image%02d.png",i), 64, 64)  
 -- assumes your images are 64x64 pixels in size and they are named  
 -- with a sequence of numbers. If not, you would have either setup up  
 -- another array that had your image names or load them one at a time.  
 images[i].x = math.random(320)  
 images[i].y = math.random(480) -- assuming vertical orientation  
end  

If you have 20 spots that you want to put a different image in, then I would setup a table with the locations pre-defined:

local spots = {}  
spots[1] = {}  
spots[1].x = 32  
spots[1].y = 47  
spots[2] = {}  
spots[2].x = 49  
spots[2].y = 104  
...  

Then you could load your images in like above:

for i = 1, 20 do  
 images[i] = display.newImageRect(string.format("image%02d.png",i), 64, 64)  
end  

Then use a table shuffle function like:

local function shuffle(t)  
 local rand = math.random   
 assert(t, "table.shuffle() expected a table, got nil")  
 local iterations = #t  
 local j  
  
 for i = iterations, 2, -1 do  
 j = rand(i)  
 t[i], t[j] = t[j], t[i]  
 end  
end  
  
shuffle(images)  
for i = 1, 20 do  
 images[i].x = spots[i].x  
 images[i].y = spots[i].y  
end  

[import]uid: 19626 topic_id: 26329 reply_id: 106792[/import]

Moved to a more appropriate forum [import]uid: 84637 topic_id: 26329 reply_id: 106830[/import]

HI Rob,

For me its like 20 fixed spot and i wanted to shuffle images with in this 20 spots. i Think the example you gave will really work cool.

Will let you know after i do the coding.

Regards
Varun [import]uid: 130269 topic_id: 26329 reply_id: 106967[/import]