I have this unique problem that I have the co-ordinate of different image to be placed but don’t know how to use it to fix this at random to different fixed location.I had asked this query before but did not get any answer but hoping to get this time .
I thought of using math.random but did not solve my problem.Any Help please… [import]uid: 82446 topic_id: 15925 reply_id: 315925[/import]
I’m not exactly sure what you are asking. It’s going to be hard to advise with so little information.
It sounds to me like you have a set of images, and you have the x, y coordinates to draw the, but you want to draw them in a random order.
If this is the case, you need a “shuffle” routine. You have a table of images, and their X and Y’s and then you randomize the array, then loop through the table now that the order has been randomized and you get the effect you’re kind of describing.
[import]uid: 19626 topic_id: 15925 reply_id: 58879[/import]
Heheh,
Indeed: your description is not that easy to understand. Anyway, a wild guess of your problem and solution:
[lua]local table = {}
table[1].name = “image1.png”
table[1].Xpos = 32
table[1].Ypos = 100
table[2].name = “image2.png”
table[2].Xpos = 88
table[2].Ypos = 50
table[3].name = “image3.png”
table[3].Xpos = 100
table[3].Ypos = 250
myImage = “a global variable here”
local function randomImage()
myImage = nil – this line is just to make sure your previous image is deleted
local randomNumber = math.random(#table)
myImage = display.newImage(table[randomNumber].name, table[randomNumber].Xpos, table[randomNumber].Ypos)
end – randomImage
timer.performWithDelay(1000, randomImage)[/lua]
Untested so I might be off here, but at least something like this should work. [import]uid: 70134 topic_id: 15925 reply_id: 58901[/import]
yes ,this is what I meant somewhat.If possible Could you provide me something so that I could understand better… [import]uid: 82446 topic_id: 15925 reply_id: 59054[/import]
@thomas6: I am using that but nothing is happening also showing no error. [import]uid: 82446 topic_id: 15925 reply_id: 59055[/import]
???
do you want place images on random place or fixed places and take one of this places by random?
Maybe if you can declare better what you want you will understand what you have to write down as the program part - algorithmic idea [import]uid: 70114 topic_id: 15925 reply_id: 59077[/import]
Can i use Xs and Ys in the following manner
local randX = math.random( 200,400,600 )
local randY = math.random( 100,200,300,400,500 )
So that all the images take the following co-ordinates only.Is there any other way to do this.Hope Now you have understood my problem. [import]uid: 82446 topic_id: 15925 reply_id: 59065[/import]
I want my images to be placed on the specific location but at random…
Here it is how i want it to go:
5 Image
5 Pre-Defined location
Now I want all the 5 images to go to these 5 Pre Defined location but at random.Now,how should I proceed. [import]uid: 82446 topic_id: 15925 reply_id: 59078[/import]
You need to learn how to shuffle things. There are several forum posts with several different shuffle methods and probably some in the code exchange.
Here’s what I would do:
local imageList = { "image1.png", "image2.png", "image3.png", "image4.png", "image5.png"}
local locations = { {x=100, y=100}, {x=200, y=100}, {x=300, y=100}, {x=150, y=150}, {x=250, y=150}}
That sets up two tables one with the 5 images to display, the other table holds tables of x,y coordinates to draw the image. Now we need to shuffle the images.
local function shuffleTable(table)
local tmpArray = {}
local shuffledArray = {}
local i
for i = 1, #table do
tmpArray[i] = table[i]
end
math.randomseed(os.time()) -- reseed the random number generator
local random = math.random
for i = 1, entries do
local idx = random(#tmpArray)
shuffledArray[i] = tmpArray[idx]
table.remove(tmpArray,idx)
end
return shuffledArray
end
local randomizedImages = shuffleTable(imageList)
Now at this point you have a new table (leaving the original alone) called randomizedImages where they are shuffled.
Then to draw the images:
imageObjects = {} -- we need to store the created display objects
local i
for i = 1, #randomizedImages do
imageObjects[i] = display.newImageRect(randomizedImages[i],100,150) -- assuming the images are 100px x 150px in size)
imageObjects[i].x = locations[i].x
imageObjects[i].y = locations[i].y
end
This is untested, but should work in principle.
[import]uid: 19626 topic_id: 15925 reply_id: 59087[/import]
local shuffle = function()
local coordinates ={{x=655, y=114}, {x=787, y=114}, {x=924, y=114},{x=655, y=250},{x=787, y=250}}
local i = 1
local randX = coordinates[i].x
local randY = coordinates[i].y
local function next()
i = i + 1
if i \<= max then --max here is 5
local randX = coordinates[i].x
local randY = coordinates[i].y
transition.to( Object[i], { time=150, x=randX, y=randY, onComplete=next } )
else
isReady = true
Runtime:addEventListener( "touch", startDrag )
end
end
transition.to( Object[i], { time=150, x=randX, y=randY, onComplete=next } )
end
I got something and came up to this. This is what I have done till now.This is making the images to move to the defined location in sequence manner.But I want this to happen in random manner.Like the First image should go to 3rd location, Second image should go to 5th location and like wise…Can anyone help me out? [import]uid: 82446 topic_id: 15925 reply_id: 59301[/import]
Try this:
[code]
local yPositions = {0,100,200,300,400} – 5 positions in regular order
for i = 1, #yPositions*2 do
local position1 = math.random(#yPositions) – gives a random number from 1 to the total number of elements in the list, 5 in this case
local position2 = math.random(#yPositions) – does the same for a second number
yPositions[position1], yPositions[position2] = yPositions[position2], yPositions[position1] – switches the two values
end
– the list yPositions will now be something like {300,0,500,200,400}
image1.y = yPositions[1]
image2.y = yPositions[2]
image3.y = yPositions[3]
image4.y = yPositions[4]
image5.y = yPositions[5]
[/code] [import]uid: 70134 topic_id: 15925 reply_id: 59310[/import]
@Thomas6 : I really could not get through what you have written.Could you explain me or give me some other alternative. [import]uid: 82446 topic_id: 15925 reply_id: 59319[/import]
My Object [i] is a single image where I have divided it into 5 pieces and then I am using it in my Function " shuffle".Hope now I have got it. [import]uid: 82446 topic_id: 15925 reply_id: 59322[/import]
Sheesh! I don’t mean to sound rude, but I can’t explain it even simpler than that - especially when the info you give is pretty superficial (e.g. HOW is your single image divided into 5 pieces? How can we give you exactly what you want - which is what you seem to be asking - if you don’t supply us with 100 percent complete and detailed info? [import]uid: 70134 topic_id: 15925 reply_id: 59344[/import]