[Resolved] Dragging objects too fast

In a scenario without physics, i wrote a touch listener for an object to dragg it. And it moves ok.

The problem began when i move too fast my finger (or mouse in the emulator). It doesn’t move until the finger goes back (without un-tap) and it starts moving again (until i move the finger too fast).
It’s like it’s lost the listener or something. And it happends with all codes out there.

Should i write the listener for Runtime instead of the object to avoid this? How did you do that?

Thanks. [import]uid: 116842 topic_id: 30534 reply_id: 330534[/import]

Don’t use a runtime listener. You will want to instead use setFocus. I’ve used it for the exact same issue you are experiencing.

http://docs.coronalabs.com/api/type/StageObject/setFocus.html [import]uid: 147305 topic_id: 30534 reply_id: 122338[/import]

Damn, i deleted thatn because i didn’t know what it was doing. Now that i know, i’m using it, but…

With the same result, if i move fast my finger (or mouse) it still “loses” the listener and stop moving.

Can’t you post a small overview of your drag function? Mine is simple and not working with or without the setFocus.

I’m using other codes, like from here: http://www.coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/ from dm’s comment, but it’s too close like mine.

Thanks. [import]uid: 116842 topic_id: 30534 reply_id: 122359[/import]

It’s not my code but the DragMe sample included with Corona’s Sample Apps works. Below is the code, but you can just open it up on your system to see it first hand.

[lua]local function onTouch( event )
local t = event.target

– Print info about the event. For actual production code, you should
– not call this function because it wastes CPU resources.
printTouch(event)

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end[/lua] [import]uid: 147305 topic_id: 30534 reply_id: 122361[/import]

Shit. (sorry)

I was using that example. It is exactly as i have (added more things, but the importat is the same). And it doesn’t work for me. It is using physics in the example and it works great, could be that the problem?
Do you remember when you used setFocus if it was with or without physics?
In the example of breakout the paddle moves with a runtime listener and it works perfectly. I don’t want to use runtime listener for this because i imagine it is bad.

PS:
I also copied that code exactly, and still doesn’t work.

I deleted the: display.getCurrentStage():setFocus( t, event.id ) from the dragg example and stops working smoothly like what i’m getting. So that’s the answer for physics i suppose.

Thanks for your job budershank.
[import]uid: 116842 topic_id: 30534 reply_id: 122401[/import]

I did not have physics body attached to any object i was dragging. As a test you can try adding:

[lua]if event.phase ==“began” then
event.target:removeBody()
end[/lua]

If it works fine with the body removed then yes, it is an issue with physics. [import]uid: 147305 topic_id: 30534 reply_id: 122415[/import]

I didn’t understand you. Where should i write that? in the dragg example or in my code?

I wrote event.target:removeBody() in my code and it stills work.
I wrote event.target:removeBody() in dragg example code and it doesn’t work. removeBody function is nil.

If it works fine with the body removed then yes, it is an issue with physics.

How will be fine without the object??
and do you mean issue with physics or feature?? haha

PS: after researching for removeBody i got this:
physics.removeBody( object )

Do you mean that? i’ll try it too. [import]uid: 116842 topic_id: 30534 reply_id: 122423[/import]

Ok, i tried physics.removeBody( object ) in the dragg example.

And it works as bad as my code, i mean, with the same behaviour, “losing” the control and doesn’t moving, until the mouse goes back…

So what’s the problem? is physics better handling touch listeners or something?

Thanks.

It call setlinervelocity method, but it throw an error, because there isn’t any physic object, i think that’s not a problem.

PS: I’m using the old one corona, is NOT the last build (Build 2012.894). [import]uid: 116842 topic_id: 30534 reply_id: 122425[/import]

The physics.removeBody(event.target) and the event.target:removeBody() should do the same thing. I was actually confused when i posted this morning(I hadn’t had coffee yet!). So even testing that wasn’t really helpful.

At this point I can’t be of much help unless you post some sample code of the issue that I can easily plug in. I would post my code but my touch event listener is incredibly massive. The setFocus thing works fine for me and it should for you as well. [import]uid: 147305 topic_id: 30534 reply_id: 122437[/import]

I’m deleting all innecesary code:
There are some lines commented because i was testing them.

[lua] local function onTouch( event )

t = event.target
local estabien = false

local phase = event.phase

if “began” == phase then
event.target:removeBody()
display.getCurrentStage():setFocus( t )
–I also tried:
–display.getCurrentStage():setFocus( t, event.id )
t.isFocus = true

– Store initial position
posiciones.x = t.x
posiciones.y = t.y
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
–t.x = event.x - t.x0
–t.y = event.y - t.y0
else
if “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
–this is something “mine”, i think you can forget it
if ( ( event.x < ( posiciones.x - 30 ) ) or ( event.x > ( posiciones.x + 30 ) ) ) or ( ( event.y < ( posiciones.y - 30 ) ) or ( event.y > ( posiciones.y + 30 ) ) ) then
print(“ESTA BIEN”)
estabien = true
transition.to ( t, { time=450, x=-80, y=t.y } )
else
print(“ES POCO”)
restar_puntos(1)
estabien = false
–t.x = posiciones.x
–t.y = posiciones.y
end
print(“suelto”)
end
end

end

return true
end[/lua]

And this is if any of you want to try it. (faster)

[lua]local myObject = display.newRect( 0, 0, 100, 100 )
myObject:setFillColor( 255 )
myObject:addEventListener( “touch”, onTouch )[/lua]

Thanks. [import]uid: 116842 topic_id: 30534 reply_id: 122454[/import]

This seems to work for me. Here are the changes I made to the code you provided:

  1. Added the posiciones table deceleration, i assume this is somewhere in the code you didn’t post.
  2. Uncommented the code in the moved section since that is the code that actually moves the box.
  3. removed the removeBody() thing since that was from my stupid suggestion earlier.

[lua]posiciones = {x=0, y=0}

function onTouch( event )

t = event.target
local estabien = false

local phase = event.phase

if “began” == phase then

display.getCurrentStage():setFocus( t )
–I also tried:
–display.getCurrentStage():setFocus( t, event.id )
t.isFocus = true

– Store initial position
posiciones.x = t.x
posiciones.y = t.y
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
else
if “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
–this is something “mine”, i think you can forget it
if ( ( event.x < ( posiciones.x - 30 ) ) or ( event.x > ( posiciones.x + 30 ) ) ) or ( ( event.y < ( posiciones.y - 30 ) ) or ( event.y

> ( posiciones.y + 30 ) ) ) then
print(“ESTA BIEN”)
estabien = true
transition.to ( t, { time=450, x=-80, y=t.y } )
else
print(“ES POCO”)
restar_puntos(1)
estabien = false
–t.x = posiciones.x
–t.y = posiciones.y
end
print(“suelto”)
end
end

end

return true
end
local myObject = display.newRect( 0, 0, 100, 100 )
myObject:setFillColor( 255 )
myObject:addEventListener( “touch”, onTouch ) [/lua] [import]uid: 147305 topic_id: 30534 reply_id: 122461[/import]

Thanks for the time to see and re-write my code.

When do you move your mouse fast, doesn’t it stop? wow, great. I’ll try that code

  1. Added the posiciones table deceleration, i assume this is somewhere in the code you didn’t post.
    –that posciones where working because it goes into the if or else. That was just for something “mine”.
  2. Uncommented the code in the moved section since that is the code that actually moves the box.
    –But it was moving with that commented lines, this is crazy :frowning:
  3. removed the removeBody() thing since that was from my stupid suggestion earlier.
    –I can forget it :wink:

Really thanks for all you did budershank. I’ll try and will come back to thanks you one more time hehe. [import]uid: 116842 topic_id: 30534 reply_id: 122464[/import]

Ok, i’m just a retard.

onTouch was at first for testing purpose. The real listener was other… 2 days changing lines and was the same result. I’m so newbie, NOOB.

Your code works nice how you said. THANKS. [import]uid: 116842 topic_id: 30534 reply_id: 122467[/import]

@budershank - always enjoy your posts but you clearly went above and beyond in this thread; good on you :slight_smile:

@correogracioso - don’t beat yourself up so much - we’re all newbies sometime.

Peach :slight_smile: [import]uid: 52491 topic_id: 30534 reply_id: 122565[/import]

But that was a… argh. If i don’t beat myself, who is going to do it?

<< @budershank - always enjoy your posts but you clearly went above and beyond in this thread; good on you :slight_smile: >>

Only if he drank ( enough? ) coffee :slight_smile: [import]uid: 116842 topic_id: 30534 reply_id: 122602[/import]

I’m sure if you ask nicely you could coax someone else into beating you - but this isn’t that kind of forum :wink:

Speaking of coffee, time to go get mine… marking as resolved! Hope to see more of you around the forum.

Peach :slight_smile: [import]uid: 52491 topic_id: 30534 reply_id: 122735[/import]

Don’t use a runtime listener. You will want to instead use setFocus. I’ve used it for the exact same issue you are experiencing.

http://docs.coronalabs.com/api/type/StageObject/setFocus.html [import]uid: 147305 topic_id: 30534 reply_id: 122338[/import]

Damn, i deleted thatn because i didn’t know what it was doing. Now that i know, i’m using it, but…

With the same result, if i move fast my finger (or mouse) it still “loses” the listener and stop moving.

Can’t you post a small overview of your drag function? Mine is simple and not working with or without the setFocus.

I’m using other codes, like from here: http://www.coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/ from dm’s comment, but it’s too close like mine.

Thanks. [import]uid: 116842 topic_id: 30534 reply_id: 122359[/import]

It’s not my code but the DragMe sample included with Corona’s Sample Apps works. Below is the code, but you can just open it up on your system to see it first hand.

[lua]local function onTouch( event )
local t = event.target

– Print info about the event. For actual production code, you should
– not call this function because it wastes CPU resources.
printTouch(event)

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end[/lua] [import]uid: 147305 topic_id: 30534 reply_id: 122361[/import]

Shit. (sorry)

I was using that example. It is exactly as i have (added more things, but the importat is the same). And it doesn’t work for me. It is using physics in the example and it works great, could be that the problem?
Do you remember when you used setFocus if it was with or without physics?
In the example of breakout the paddle moves with a runtime listener and it works perfectly. I don’t want to use runtime listener for this because i imagine it is bad.

PS:
I also copied that code exactly, and still doesn’t work.

I deleted the: display.getCurrentStage():setFocus( t, event.id ) from the dragg example and stops working smoothly like what i’m getting. So that’s the answer for physics i suppose.

Thanks for your job budershank.
[import]uid: 116842 topic_id: 30534 reply_id: 122401[/import]