A Fuse Powered Company

Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

I Want To Drag And Drop Objects I Placed On Screen
Started by Darnelle Mar 28 2013 02:05 PM

8 replies to this topic
android corona api drag drop move send
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Darnelle

[GLOBAL: userInfoPane.html]
Darnelle
  • Starter
  • Pip
  • 7 posts
  • Newbie

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

 



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Corona Staff
  • 10,364 posts
  • Jedi

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



[TOPIC: post.html]
#3

Darnelle

[GLOBAL: userInfoPane.html]
Darnelle
  • Starter
  • Pip
  • 7 posts
  • Newbie

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 



[TOPIC: post.html]
#4

kodewulf

[GLOBAL: userInfoPane.html]
kodewulf
  • Pro
  • PipPip
  • 28 posts
  • Member

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.



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Corona Staff
  • 10,364 posts
  • Jedi

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



[TOPIC: post.html]
#6

bava

[GLOBAL: userInfoPane.html]
bava
  • Starter
  • Pip
  • 6 posts
  • Newbie

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.,


[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Corona Staff
  • 10,364 posts
  • Jedi

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



[TOPIC: post.html]
#8

bava

[GLOBAL: userInfoPane.html]
bava
  • Starter
  • Pip
  • 6 posts
  • Newbie

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

 

 

 

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



[TOPIC: post.html]
#9

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Corona Staff
  • 10,364 posts
  • Jedi

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




[topic_controls]
[/topic_controls]



Also tagged with one or more of these keywords: android, corona, api, drag, drop, move, send