A Coding Advice

I exscuse my self in advance for my bad english.

However , I
have a little problem: I m coding my first game and I don’t know how to
switch the position of an image in 2 precise coorditanate. I explain
better this is my code:

function gonext(event) if event.phase == "began" then transition.to(img, {time = 10 , x = math.random(13 , 239)}) end end img:addEventListener("touch" , gonext)

In this way the Img after a ‘’ touch ‘’ switch his position between the number 13 and 239 ( so 122 , 34 , 68 ,200 … etc) But my target is to make move the img or in the position x = 13 or x = 239 (not all the values between these 2 numbers)

I hope I ve been clear xD.

Thanks in advance

Are you saying that you want the img to ONLY move to either x = 13 or x = 239?  

If so then math.random will not help you, as you will get all values in between (as you have already found out).  

Try this instead:

local validPositions = {13, 239} function gonext(event) if event.phase == "began" then transition.to(img, {time = 10 , x = validPositions[math.random(1, 2)]}) end end img:addEventListener("touch" , gonext)

There is a table at the top with the only valid positions contained in it. The transition now picks a random value from that table. The math.random() call will only give you either 1 or 2, by inserting the function call into the table index it means it will choose either index 1 or index 2 from the validPositions table.

I tried to make as you said but nothing… the System doesn’t recognize the ValidPosition local and the img doesn’t switch… It remains in the same position

Are you saying that you want the img to ONLY move to either x = 13 or x = 239?  

If so then math.random will not help you, as you will get all values in between (as you have already found out).  

Try this instead:

local validPositions = {13, 239} function gonext(event) if event.phase == "began" then transition.to(img, {time = 10 , x = validPositions[math.random(1, 2)]}) end end img:addEventListener("touch" , gonext)

There is a table at the top with the only valid positions contained in it. The transition now picks a random value from that table. The math.random() call will only give you either 1 or 2, by inserting the function call into the table index it means it will choose either index 1 or index 2 from the validPositions table.

I tried to make as you said but nothing… the System doesn’t recognize the ValidPosition local and the img doesn’t switch… It remains in the same position