Problems with drag/drop

I’d like to see your code with the markedX, Y not working. My gut is a scope issue. [import]uid: 19626 topic_id: 33201 reply_id: 131934[/import]

So if I have two pieces stacked on top of each other, they both get the touch event. The one below will return false since it isn’t focused? [import]uid: 160288 topic_id: 33201 reply_id: 132037[/import]

Its definitely in the docs, yes I can:

Touch and Tap event listeners return true if they want to show that they handled the touch or tap. If they return false the event gets passed to the next display object in the hierarchy which is listening for those events, until one returns true. Then all the associated events are passed directly to the listening object which returned true.

It has occurred to me that there are some improvements to be made to my function, but keep an eye on the post and I’ll get to it shortly. [import]uid: 8271 topic_id: 33201 reply_id: 132038[/import]

Ok, that makes sense now.

What specifically is an issue with the function? [import]uid: 160288 topic_id: 33201 reply_id: 132039[/import]

I think I’m not giving the began/moved/ended functions the chance to return true|false… Updated now.

I’ve fixed the return values - if your functions don’t return anything it will return a default.
Also, if you have attached the touch listener to Runtime it will assume the target to be the stage. [import]uid: 8271 topic_id: 33201 reply_id: 132056[/import]

@Mate, thanks a lot. That completely solved my problem and I didn’t think to lock the move event to focus as well. It runs a ton better now and two of the oddness I was experiencing some of the time.

@Rob This is something like what I was doing:

local function movePiece(event)  
   
 local markedX  
 local markedY  
  
 if event.phase == "began" then  
 -- bring piece to front   
 event.target:toFront()  
   
 --lock focus  
 display.getCurrentStage():setFocus(event.target)  
 event.target.hasFocus = true;  
  
 markedX = event.target.x -- store x location of object  
 markedY = event.target.y -- store y location of object  
  
 elseif event.phase == "moved" then  
 local x = (event.x - event.xStart) + markedX  
 local y = (event.y - event.yStart) + markedY  
  
 event.target.x, event.target.y = x, y -- move object based on calculations above  
   
 elseif event.phase=="ended" then  
 event.target.hasFocus = false  
 display.getCurrentStage():setFocus(nil)  
 end  

I also tried putting the local markedX/Y at the beginning of the begin phase. I did not know at the time you can have a move event without a began so I assumed x/y would always be initialized. I didn’t think of the scenario Mate mentioned about dragging the screen and sliding into a piece. I am not quite sure how this works though, but forcing the move event to also have focus before my code executes, makes everything work perfectly. [import]uid: 160288 topic_id: 33201 reply_id: 131962[/import]

Thanks Horacebury, I am going to have a look shortly. [import]uid: 160288 topic_id: 33201 reply_id: 131963[/import]

Horacebury,

I looked through your code and I had a few questions.

Is touchBegan, touchMoved, touchEnded your own functions that you attached to the object?

Do you have a simple example of this?

What is the purpose of returning true/false?

I love the idea of this, where you don’t have to modify it, and just do the callbacks, I am just not sure how to attach the callbacks. I assume you create a these functions, then do something like object.touchBegan = touchBegan to point to the function?
[import]uid: 160288 topic_id: 33201 reply_id: 132023[/import]

Return true or fale indicates to Corona that the event should fall through (false) to the display object below, or not (true.)

Yes, those functions need to be defined on the object you are attaching the event listener to. They would be attached either like this:

[lua]dragTab.touchEnded = function(e)[/lua]

or like this:

[lua]function dragTab:touchEnded(e)[/lua]
[import]uid: 8271 topic_id: 33201 reply_id: 132032[/import]

Very nice how you can use this as a snippet without having to modify it.

I still don’t quite understand the true/false, and I couldn’t see it in the documentation.

Can you explain the scenario where the final return false would occur.
In my code I am not returning true or false, what is happening differently from you returning true?
[import]uid: 160288 topic_id: 33201 reply_id: 132034[/import]

If there’s another listener underneath the initial listener, then it will also receive the touch event, unless you return true from the initial listener.

Not returning anything is equivalent to false - it lets the event pass through. [import]uid: 36578 topic_id: 33201 reply_id: 132036[/import]

So if I have two pieces stacked on top of each other, they both get the touch event. The one below will return false since it isn’t focused? [import]uid: 160288 topic_id: 33201 reply_id: 132037[/import]

Its definitely in the docs, yes I can:

Touch and Tap event listeners return true if they want to show that they handled the touch or tap. If they return false the event gets passed to the next display object in the hierarchy which is listening for those events, until one returns true. Then all the associated events are passed directly to the listening object which returned true.

It has occurred to me that there are some improvements to be made to my function, but keep an eye on the post and I’ll get to it shortly. [import]uid: 8271 topic_id: 33201 reply_id: 132038[/import]

Ok, that makes sense now.

What specifically is an issue with the function? [import]uid: 160288 topic_id: 33201 reply_id: 132039[/import]

I think I’m not giving the began/moved/ended functions the chance to return true|false… Updated now.

I’ve fixed the return values - if your functions don’t return anything it will return a default.
Also, if you have attached the touch listener to Runtime it will assume the target to be the stage. [import]uid: 8271 topic_id: 33201 reply_id: 132056[/import]