What's causing my object to snap?

I have a draggable object that, when it reaches the top of the screen, triggers the bg to scroll down one field, taking the object with it. However, once the transition is over, the object suddenly “snap’s” to wherever the user’s finger may be (assuming the user hasn’t yet lifted his finger off the screen). I don’t want this to happen, I want the user to have to retouch the object to get it moving again.

I tried taking control away from the user during the transition by temporarily removing the event listener for the object, but once the event listener is reinstated, the object is a-snappin’ again.

if player.y < 50 thenplayer.y = 50 if flag2~="on" then--I add the listener back after 1.0 sec (the duration of the transition) timer.performWithDelay(1000,function(event) player:addEventListener("touch",onTouch) end, 1)-- I remove the listener for now player:removeEventListener( "touch", onTouch )-- I add the transition transition.to(player, {time = 1000, delay = 0, y = player.y + 920}) transition.to(background, {time = 1000, delay = 0, y = background.y + 1024}) transition.to(background2, {time = 1000, delay = 0, y = background2.y + 1024}) flag2="on" end end[/code]Thank you,Steven [import]uid: 79394 topic_id: 13931 reply_id: 313931[/import]

it got something to do with the touch event of your object. please post that part of the code so that we can have a better understanding of what its really happening. [import]uid: 71210 topic_id: 13931 reply_id: 51234[/import]

As Renjith said, if you show us your touch function (for moving your player) we should be able to help you out :slight_smile: [import]uid: 52491 topic_id: 13931 reply_id: 51276[/import]

Awesome guys, I didn’t want to post too much for fear of making my post too long or confusing so I tried to just include what I thought was the bare minimum. That said, here’s the touch event part of my code (please excuse any odd syntax I may be using, as I’m just trying to word things in a way that makes sense to me):

-- text to display directionlocal directionTXT = display.newText("Direction: ",130,25,nil,25)directionTXT:setTextColor( 255,0,0 )local function onTouch( event ) local t = event.target 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 prevx = event.x prevy = event.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 local dx = prevx - event.x local dy = prevy - event.y local distance = dx * dx + dy * dy if distance>50 then local angle = math.atan2(dy,dx) * 57.2957795 if angle>=45*-1 and angle<=45 then string_dir="Left" if dragDirection ~= "left" then player:play('Player run side view left') dragDirection = "left" end elseif angle>45 and angle<135 then string_dir="Up" if dragDirection ~= "up" then player:play('Player run back view') dragDirection = "up" end elseif angle>135*-1 and angle<45*-1 then string_dir="Down" if dragDirection ~= "down" then player:play('Player run front view') dragDirection = "down" end elseif angle>=135 or angle<=135*-1 then string_dir="Right" if dragDirection ~= "right" then player:play('Player run side view right') dragDirection = "right" end end prevx=event.x prevy=event.y directionTXT.text ="Direction : "..string_dir moved = moved + 1 counttracker = 0 end elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false if string_dir == "Left" then player:play('Player static left side view') elseif string_dir == "Up" then player:play('Player static back view') elseif string_dir == "Down" then player:play('Player static front view') elseif string_dir == "Right" then player:play('Player static right side view') end dragDirection = "stopped" flag = "on" end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true endendlocal function movementTracker(event) if moved == 0 then if flag ~= "on" then if counttracker == 3 then print("not moving and last movement was towards "..string_dir) if string_dir == "Left" then player:play('Player static left side view') elseif string_dir == "Up" then player:play('Player static back view') elseif string_dir == "Down" then player:play('Player static front view') elseif string_dir == "Right" then player:play('Player static right side view') end dragDirection = "stopped" flag = "on" end end end counttracker = counttracker + 1 moved = 0end[/code]Thank you in advance for your advice. I still feel like a blind man making his way around, so I really need all the help I can get. If there's anything else (like the declaration of my variables) you need, please let me know. [import]uid: 79394 topic_id: 13931 reply_id: 51289[/import]

PS: Renjith, you’ll no doubt notice alot of your code sugg’s in there, many of which I may have butchered in my ineptitude (embarrassed face). If so, apologies in advance:) [import]uid: 79394 topic_id: 13931 reply_id: 51291[/import]

@edaabs am lookin gat your code… where are you putting that background scroll logic, the code in your first post of this thread ? [import]uid: 71210 topic_id: 13931 reply_id: 51489[/import]

ok got it…
you may put the following code after the code which removes the touch event listener

[lua]display.getCurrentStage():setFocus( nil )
player.isFocus = false[/lua]

otherwise the player object will be still be the object on focus and the newly added touch event will be on the player object itself.

your code will look like this
[lua]if player.y < 50 then
player.y = 50

if flag2~=“on” then
–I add the listener back after 1.0 sec (the duration of the transition)
timer.performWithDelay(1000,function(event) player:addEventListener(“touch”,onTouch) end, 1)
– I remove the listener for now
player:removeEventListener( “touch”, onTouch )
display.getCurrentStage():setFocus( nil )
player.isFocus = false
– I add the transition
transition.to(player, {time = 1000, delay = 0, y = player.y + 920})
transition.to(background, {time = 1000, delay = 0, y = background.y + 1024})
transition.to(background2, {time = 1000, delay = 0, y = background2.y + 1024})
flag2=“on”
end
end[/lua] [import]uid: 71210 topic_id: 13931 reply_id: 51490[/import]

Solved. I owe you one, Renjith, now I really gotta make this app the next Angry Birds:)

Cheers,
Steven
[import]uid: 79394 topic_id: 13931 reply_id: 51498[/import]

good to know it worked… best of luck for your app… :slight_smile: [import]uid: 71210 topic_id: 13931 reply_id: 51501[/import]