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.

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 :)

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):

1
2
3
4
5
6
7
8
9
10
11
12
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)

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 :)

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. :D

Very soon a new game in AppStore/Android :D

Thanks!

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 :)

Peach :)

views:1766 update:2012/2/9 11:37:26
corona forums © 2003-2011