Looking for the best way to move random objects and then identifying which one was moved (or something like that)

Hi, I’m new here and very new to programming in general. I bought Corona on the Halloween promotion and I’ve been doing a lot of reading. Right now I’m trying to learn as much as I can and test different things. So I encountered the following problem:

I have 3 identical objects positioned like so:

X1 X2 X3

I want 2 of the objects, chosen at random, to swap place. The swap can’t be instantaneous, then need to actually move and be seen moving. Now this part is no problem, the problem comes when I want to swap them a second time (and a third and so on), then my noob code starts to get out of hand. If it wasn’t random it wouldn’t be a problem either, bu having to figure out where each object is in order to see if it has to move left or right and the distance (X1 can go to X2 or X3) is kinda getting to me.

How would you guys do it? Conceptually speaking, what would be the best way to design something like this? It should be super easy I know, but like I said, Lua is my first programming language and I’ve been doing this for 2 days, so be patient with me. Thanks.
[import]uid: 10835 topic_id: 3318 reply_id: 303318[/import]

It just occurred to me that I could have explained myself better. Could find the edit button, so forgive the double post.

This is the start (forget about y coordinates for a second, they’re all at the same height):

X1 X2 X3

After the first cycle it could end up like this:

X2 X1 X3

Then like this:

X3 X2 X1

Now if X3 were picked to be swapped with X1 now, I’m having trouble keeping up. I need to tell X3 to move 2 units to the left and X1 2 units to the right. But since they are picked randomly any X could be in the original 1 position.

I don’t know, it’s probably stupidly easy and doesn’t have to balloon like my code. I also want it to stop moving after a set number of cycles.
[import]uid: 10835 topic_id: 3318 reply_id: 9910[/import]

It’s a little hard imagining exactly what you’re trying to do. You said it wouldn’t be a problem if it WASN’T random.

Let me know how you would do it if it wasn’t random, and I think then I’ll have a better understanding what you’re trying to accomplish, and will be able to help you out more.

So basically, make up some pre-determined “swaps” and show me how you would do that with X1, X2, and X3 and post it here. [import]uid: 7849 topic_id: 3318 reply_id: 9917[/import]

Well at start I know the x coordinates of all my Xs. Let’s say they are X1.x=10, X2.x=30 and X3.x=60

So if I want to swap X1 for X2 I do something like Runtime:addEventListener( “enterFrame”, move ) for a move() function. And in the function I have something like this:

if X1.x < 30 then
X1.x = X1.x + 1
end
Which will move X1 to X2’s position.

Now I move X2 to x=10

if X2.x > 10 then
X2.x = X2.x - 1
end

Easy stuff.

Now if I wanna move X1 to X3 I know that X1.x=30 so it’s just as easy as before. Probably terrible coding practices all around, but doable. I can get it to work.

If it’s random however after the first move I don’t know what X1.x will be. So I don’t know if I wanna swap it with say X3 if I have to move it to the right or left and for how long. I can’t use hard-coded values anymore. I have to dynamically check where they are and I’m looking to the best conceptual approach to do that, without my code blowing up from complexity (it should be pretty trivial stuff anyway). [import]uid: 10835 topic_id: 3318 reply_id: 9919[/import]

Since you have predefined “spots” for each of the X’s, couldn’t you just do a conditional “if” statement depending on what random swap occurred? You’d just write code out as you told me you would “IF” you knew what were to happen.

Is there any problems with doing it that way? [import]uid: 7849 topic_id: 3318 reply_id: 9921[/import]

Yes, a tiny one. I can’t figure out how to do it. Also it doesn’t seem very efficient or scalable (think 10 Xs instead of 3). But the main problem is that I don’t really know what to do with X1.x and the others after the first swap.

There could be an infinite (or at least very high, think shuffling) number of swaps so I don’t want to write a function for each swap. I want one that can handle any type of swap, since they are few (X1 to X2, X1 to X3 and X2 to X3) in their original positions, and will always be 3 possible swaps.

I tried something like a matrix to identify which X is in which position and then move it accordingly, but failed miserably. I don’t know, there has to be a simpler way, something I’m missing. It really seems trivial to my untrained eye, not something that should take hundreds of line of code to accomplish. [import]uid: 10835 topic_id: 3318 reply_id: 9926[/import]

What about making a function, of course below is very basic but maybe you could build off of the concept:

[blockcode]
local swapSpots = function( x1, x2 )
– The x1 and x2 arguments are the two objects to be swapped
originalX1 = x1.x
originalX2 = x2.x

x1.x = originalX2
x2.x = originalX1
end
[/blockcode]

And before calling that function, randomly pick the first X object, and then randomly pick the second X object (of course making sure it’s not the same as the first). [import]uid: 7849 topic_id: 3318 reply_id: 9930[/import]

Thanks for the suggestion. I had thought of something similar, but I encountered a problem updating x1.x in between the movement. Remember I can’t just swap the values, I need to move X to its new position. It has to be visible.

So let’s say I do what you say. Would I do originalX1 = originalX1 + 1 until I reach my new destination? But that would just increase originalX1 not x1.x, so it wouldn’t move X like I want.

Basically I don’t understand how to do the actual movement with your example, given my limited knowledge of lua. [import]uid: 10835 topic_id: 3318 reply_id: 9931[/import]

Put all the objects in a group

Get the number of children in the group, which will be 3 in this case

get 2 random numbers math.rand(1,3), check to make sure you dont generate same number twice

you get then access the graphic in the group[1].x , group[1].y , group[3].x etc…

swap those 2 by taking the x and y values and then use a transition to update those values

keep repeating [import]uid: 5354 topic_id: 3318 reply_id: 9941[/import]

Here is a little sample that shows continuously swapping of objects. It was written without
the ability to test, so if there is any problems, let me know.

[lua]–Create a group that holds all items
local grpItems = display.newGroup()

–The next call is important to a bug in math.random
local dummy = math.random(255)

–Predefine the swap function
local swapItems

–Next function will swap the items randomly
swapItems = function()
–Determine the first object
local index1 = math.random(1,grpItems.numChildren)

local index2 = index1
–Determine the second object
while index2 == index1 do
index2 = math.random(1,grpItems.numChildren)
end

transition.to(grpItems[index1],{ time=1000, x=grpItems[index2].x })
transition.to(grpItems[index2],{ time=1000, x=grpItems[index1].x, onComplete=swapItems })
end

–Create the items
for i=1,4 do
local rect = display.newRect( 55*i, 40, 40, 40 )
rect:setFillColor( 255, 55*i, 0 )
grpItems:insert(rect)
end

–And ACTION!
swapItems()[/lua]

[import]uid: 5712 topic_id: 3318 reply_id: 9946[/import]

Thanks for the responses! Unfortunately I can´t test at the moment either as I’m at work (yuck) and won’t be back home in quite a while.

For the moment it looks good, clean and simple. Thanks!

Could you elaborate a little on the math.random bug? I had used it before without that call and didn’t encounter any problem (that I know of!).

I like your way of determining objects much better than mine (I’ll just say I didn’t know math.random could have 2 arguments).

I’ve been looking it over for a few minutes (along with checking out the relevant API documentation) and I think I get it. It should work for what I need.

The only problem I have now it’s y axis movement. I didn’t bring it up before because I had a nice sine movement thing going on so they avoid each other in a circular motion (one moving on a semi circle above and the other below). I’m not really sure how to achieve that with transitions though. I’ve looked a little through the Easings, but I hasn’t yet clicked with me.

Edit: I was able to test it on my lunch break. I works as expected. However since it’s time based the swap takes just as long if it’s X1 and X2 as if it’s X1 and X4. This results in the movement speed to be really slow when they’re next to each other and fast when they’re far. It’s easy to fix though. Instead of time being a constant I’ll make it a variable dependant on the distance between indexes. That way I’ll get the constant movement speed I want.

Thanks for all the help. [import]uid: 10835 topic_id: 3318 reply_id: 9990[/import]

The first call to math.random will always result in the same number. If you also want to modify the y value during your transition, then I think it is better to create your own transition functionalities. [import]uid: 5712 topic_id: 3318 reply_id: 10019[/import]

Thanks. How would one go about doing that? I can’t find anything in the documentation about creating your own transition functionalities, do you have a link by any chance? [import]uid: 10835 topic_id: 3318 reply_id: 10028[/import]

In the contributed code section there are other transitions for use with transition.to function

Have a look how that was created [import]uid: 5354 topic_id: 3318 reply_id: 10029[/import]