I Want To Drag And Drop Objects I Placed On Screen

i got an object popping on screen and i want that object to be drag-able, 

like if its at the bottom of the screen i want to move it to the top

Please see the “DragMe” sample app in your SampleCode folder (on a Mac /Applications/CoronaSDK/SampleCode/Interface/DragMe)

i looked at it and its mighty confusing and not really what i need!
there will be multiple objects poping on screen and i want them to move it off screen each time>

the DragMe sample shows three rec and how you can move it around 

the DragMe sample shows three rec and how you can move it around 

From what I gather from the DragMe sample, it’s a very good start. You’ll have to tweak the “touch” event a bit, but I gather you should be able to use that code as a base.

Just add the touch handler to each object that you create that needs it.  Just because the sample app uses three, it doesn’t mean you’re limited to it.   The drag event handler is very hard to understand at the beginning, but be patient and give it a try.  It has all the right code for dragging things around.d

Please see the “DragMe” sample app in your SampleCode folder (on a Mac /Applications/CoronaSDK/SampleCode/Interface/DragMe)

i looked at it and its mighty confusing and not really what i need!
there will be multiple objects poping on screen and i want them to move it off screen each time>

the DragMe sample shows three rec and how you can move it around 

the DragMe sample shows three rec and how you can move it around 

From what I gather from the DragMe sample, it’s a very good start. You’ll have to tweak the “touch” event a bit, but I gather you should be able to use that code as a base.

Just add the touch handler to each object that you create that needs it.  Just because the sample app uses three, it doesn’t mean you’re limited to it.   The drag event handler is very hard to understand at the beginning, but be patient and give it a try.  It has all the right code for dragging things around.d

Hai All., (@Rob Miracle)

I am really very new to corona sdk,

please any one help!

i am in hurry for th solution ,.any body  kindly help me quickly

The question is :

First I stored 10 images in array(i.e. table) from 1-10 as key values and I create a random number using math.random function between 0-9., and i need to access the image that is stored in array by the value created by random function, and need to assign the touch and move(drag and drop) function for the particular image file alone., other images also needs touch(i.e drag alone)

Ex:  if the random fn creates no as “5” i need to drag and drop the image 5.png that is stored in array index as 5 .,other images except 5.png should not able to drop., (i.e., they are allowed to move in screen but not to drop able in screen)

Thanks a lot.,

That’s a lot of questions at once.

First, you don’t want to use Key-Value pair type arrays, you want to use indexed arrays.  There is a big difference between:

myTable[5]

and

myTable[“5”]

You want to use numbers, not strings to index the table.  Next, Lua array’s are not 0 index based like most other languages.  The first element is 1, not 0.  You probably want math.random to return a number between 1 and 10, not 0 and 9.

As for the other issue of limiting the drag, when you create each object assign the same touch handler to them.  That way in the touch handler you can refer to the object touched as event.target. When you create the object assign it a name, which can be the index number or you can call it id:

myObjects[1] = display.newImage(“1.png”)

myObjects[1].id = 1

The reason for this is that the touch handler will have a reference to the object, but not its position in the table, by giving it an id equal to the index, you know which one you are dealing with.  Now you have an attribute:  event.target.id that lets you know which one you’re working with. 

Next, when you know that only 5 can be dropped, set some variable:   canOnlyDrop = 5 and in your touch handle where you are dealing with the drop code:

     if event.target.id == canOnlyDrop then

            –  allow the drop to happen

     else

            – move the object back to where it was

     end

or something like that.

Rob

Thanks a lot Rob miracle. :)  :)   :slight_smile:

on more question is : how to assign one touch fn to all objects when creating it , from myObjects[1]…to myObjects[10]., 

i tried to assign myObjects[1] …myObjects[10] a touch event ., using for loop but it wont worked .,.,

so i write it for each n every object

suggest me how to write code in effective manner.,

Thanks once again

local function myTouchHandler( event )      -- event.target will be the thing that was touched      -- put your event handling code here      return true end   for i = 1, 10 do       myObjects[i] = display.newImageRect(...)       myObjects[i]:addEventListener("touch", myTouchHandler)       ... end

* … means put in whatever is appropriate for you, not three dots.

So if you touch myObjects[4] then inside the event handler:  event.target will be myObjects[4].

Rob

Hai All., (@Rob Miracle)

I am really very new to corona sdk,

please any one help!

i am in hurry for th solution ,.any body  kindly help me quickly

The question is :

First I stored 10 images in array(i.e. table) from 1-10 as key values and I create a random number using math.random function between 0-9., and i need to access the image that is stored in array by the value created by random function, and need to assign the touch and move(drag and drop) function for the particular image file alone., other images also needs touch(i.e drag alone)

Ex:  if the random fn creates no as “5” i need to drag and drop the image 5.png that is stored in array index as 5 .,other images except 5.png should not able to drop., (i.e., they are allowed to move in screen but not to drop able in screen)

Thanks a lot.,

That’s a lot of questions at once.

First, you don’t want to use Key-Value pair type arrays, you want to use indexed arrays.  There is a big difference between:

myTable[5]

and

myTable[“5”]

You want to use numbers, not strings to index the table.  Next, Lua array’s are not 0 index based like most other languages.  The first element is 1, not 0.  You probably want math.random to return a number between 1 and 10, not 0 and 9.

As for the other issue of limiting the drag, when you create each object assign the same touch handler to them.  That way in the touch handler you can refer to the object touched as event.target. When you create the object assign it a name, which can be the index number or you can call it id:

myObjects[1] = display.newImage(“1.png”)

myObjects[1].id = 1

The reason for this is that the touch handler will have a reference to the object, but not its position in the table, by giving it an id equal to the index, you know which one you are dealing with.  Now you have an attribute:  event.target.id that lets you know which one you’re working with. 

Next, when you know that only 5 can be dropped, set some variable:   canOnlyDrop = 5 and in your touch handle where you are dealing with the drop code:

     if event.target.id == canOnlyDrop then

            –  allow the drop to happen

     else

            – move the object back to where it was

     end

or something like that.

Rob

Thanks a lot Rob miracle. :)  :)   :slight_smile:

on more question is : how to assign one touch fn to all objects when creating it , from myObjects[1]…to myObjects[10]., 

i tried to assign myObjects[1] …myObjects[10] a touch event ., using for loop but it wont worked .,.,

so i write it for each n every object

suggest me how to write code in effective manner.,

Thanks once again

local function myTouchHandler( event )      -- event.target will be the thing that was touched      -- put your event handling code here      return true end   for i = 1, 10 do       myObjects[i] = display.newImageRect(...)       myObjects[i]:addEventListener("touch", myTouchHandler)       ... end

* … means put in whatever is appropriate for you, not three dots.

So if you touch myObjects[4] then inside the event handler:  event.target will be myObjects[4].

Rob