Drag and drop

Im making a game that need some kind of interaction with elemnts. I need to combine then.

Im having some problems with drag and drop elements.

Im using a little diferent code found here (http://blog.anscamobile.com/2011/09/tutorial-how-to-drag-objects/) to drag my elements in the screen.

But it seems that the movement of drag and drop is not well done. The Mobile (Android / Milestone, Android / Galaxy S2 and iPhone 3G) does not seem to follow the movement of the finger on the screen.

A better way to do the drag and drop? I have some functions as collision and other things, but not seen to be related with movement.
[import]uid: 9133 topic_id: 21071 reply_id: 321071[/import]

You may try setting focus, that could help depending on circumstances.

When you say it isn’t following do you mean it gets dropped, or you’re modifying code to have it follow the touch but not actually dragging it?

Peach :slight_smile: [import]uid: 52491 topic_id: 21071 reply_id: 83282[/import]

Hi Peach,

My code is EXACTLY that code in Corona`s Blog. To be honest, I just made one modification: I put a Variable to be sure that only one element in screen will move (strange effects without this trick):

function element:touch( event )  
 if event.phase == "began" then  
 event.target:toFront()  
 self.markX = self.x -- store x location of object  
 self.markY = self.y -- store y location of object  
 self.onlyme = true  
 elseif event.phase == "moved" and self.onlyme == true then  
 local x = (event.x - event.xStart) + self.markX  
 if x \< 0 then x = 0 end;  
 if x \> (\_W-30) then x = (\_W-30) end;  
  
(continue the same)  

This “onlyme” variable is here just to be sure only one element on the screen will move in the same time.

My program do this: Imagine 2 squares, one yellow and another one blue. I move both on the screen.

If I do a “double tap” in a color, this color tapped copy itself, so, If I do this is a blue square, now I have 2 blue squares and one yellow visible.

Imagine now that I’m dragging and dropping a blue square (or yellow) on the other. The desired effect is that both squares disappear and a new square, green, appear in place.

My problem is that it seems to drag the movement is not occurring properly with this code. It seems that the element is “tied.” I do a collision between objects only in phase “ended” of the event …

elseif event.phase == "ended" then  
 self.onlyme = false  
 element\_collide()  
end  

… so I suppose that this code should not interfere (element_collide) the movement of the elements on the screen.

In time, the elements are stored in a table and drag event is added when built as follows:

tabela[#tabela]:addEventListener( "tap", buttontap)

where “tabela” is a table (this remains in portuguese, my first language).

Do not bother with the #. In my code, I assure you that the element is created in the last position of the table.

When I build the program, even in simulator, the element dont seen to follow the bouse (button pressed) or my finger, in the screens.

How setFocus() helps me and I did not understand? Of course, I miss something, I dont know what…

Thanks!

[import]uid: 9133 topic_id: 21071 reply_id: 83328[/import]

You do not need to use your “onlyme” variable if you set the focus - it makes things much easier. Look at;

CoronaSDK > SampleCode > Interface > DragMe

It is already on your computer and I think it will help you a LOT.

Let me know how you go with it and I can try to assist you from there.

Peach :slight_smile: [import]uid: 52491 topic_id: 21071 reply_id: 83351[/import]

Hi Peach,

I am honest in saying I do not understand the example you mentioned completely. However, I realize that simply using setfocus greatly changes the behavior of my program. I had the impression that my variable “OnlyMe” service that does the same “isfocus” in the example you mentioned.

Thanks for your help, I will study more settfocus, but the results are encouraging. :smiley:

Very soon a new game in AppStore/Android :smiley:

Thanks! [import]uid: 9133 topic_id: 21071 reply_id: 83541[/import]

No worries - I know at first it can be very confusing, it was for me as well but over time you will learn it and it will seem easy :slight_smile:

Peach :slight_smile: [import]uid: 52491 topic_id: 21071 reply_id: 83546[/import]