SOLVED - Sticky Dragging

Heylo,
I’ve got a level and the player drags it around to look around.
I dont want the player to look outside of the levels boundaries.

I used this piece of code.

local function drag(e)  
if (e.phase == "moved") then  
 if (game.x \< 1 and game.x \> -481) then  
 game.x = e.x - touchposx  
 end  
 if (game.y \< 1 and game.y \> -321) then   
 game.y = e.y - touchposy  
 end  
end  

And then if withing the boundary the player can drag around.

For the most part it works well, but when the player reaches a boundary it sticks to that boundary and can no longer be dragged by that axis.
This is because, for example, game.x has touched 0 and now that part of the code doesn’t work. And since you can’t drag on that axis, you can’t get it out of 0.

I attempted this in the same function above the previous code:

 if (game.x \> 0) then  
 game.x = 0;  
 return true  
 elseif (game.x \< -480) then  
 game.x = -480;  
 return true  
 elseif (game.y \> 0) then  
 game.y = 0;  
 return true  
 elseif (game.y \< -320) then  
 game.y = -320;  
 return true  
 end  

However this causes a snapback motion as it snapped back to 0, then the player moves his finger slightly changing it beyond the boundary then it snapping back again.

Is there a way to prevent the player from dragging the background past the boundary without it getting stuck or snapback?

Thanks Matt. [import]uid: 91798 topic_id: 16071 reply_id: 316071[/import]

Edit: my comment below only applies if you are using physics. If not, I have no clue why you are having the problem…

You might want to read up on: http://blog.anscamobile.com/2011/08/solutions-to-common-physics-challenges/

There’s a section entitled Objects are sticking to walls.

I also had a problem with object sticking to a wall – once it’s stuck, it won’t budge. If I remember correctly, I think it was because I didn’t plot the physics body of the wall properly (i.e., if you are using polygon to define the physics body, it must be defined in clockwise order).

If none of the above applies to your case, hopefully, someone else will chime in to help.

Good luck. [import]uid: 67217 topic_id: 16071 reply_id: 59753[/import]

Thanks for the support.

But no there is not physics involved in the object that is being dragged or the boundaries.

Thanks anyway, and good luck to YOU sir. [import]uid: 91798 topic_id: 16071 reply_id: 59756[/import]

I hope someone else will help you out, Matt. I’d also like to know why this sticky dragging is happening too (for my future reference). [import]uid: 67217 topic_id: 16071 reply_id: 59763[/import]

Then you’re in luck, Naomi.
I was just having a bath and the solution came to my water wrinkled self.

 if (game.x \> 0) then  
 game.x = 0;  
 return true  
 elseif (game.x \< -480) then  
 game.x = -480;  
 return true  
 elseif (game.y \> 0) then  
 game.y = 0;  
 return true  
 elseif (game.y \< -320) then  
 game.y = -320;  
 return true  
 end  

If i make this code it’s own function and add an Event Listener to it “enterFrame” so it runs every frame.
It’ll check the boundaries every frame, whether or not the player is dragging around the level. (in “moved” phase)

So the intial ‘sticky’ problem was when it entered, 0 for example, it was told to not allow dragging, therefore you couldn’t drag the other way.

By allowing dragging until 1, it would get stuck at 1 instead (confusing but 1 is more to the left than 0)
but the above code stops it at 0, but still allowing dragging.
(See post #1 code for clarification)

I should code in the bath tub in future ^-^. [import]uid: 91798 topic_id: 16071 reply_id: 59771[/import]

Cool. Glad to hear you’ve found the solution and hearing about it. You know, I’ve heard sometime ago about how our brain can come up with great solution to a problem when our mind is in idle mode. It’s good to take our mind off things sometimes. [import]uid: 67217 topic_id: 16071 reply_id: 59774[/import]

I normally come up with simple ideas that require very complex code (complex for me, maybe not so much for a pro). Usually it’s when I am running, or at the beach, or at the park or hanging out with our new baby boy(6 weeks old tommorow).

The things currently on my mind

A* (the most pain in my ass algorithm)

dijkstra algorithm
^^ these 2 i’m looking at freestyle AI path finding…yea it’s a pain.

level design

etc.

I’ll have to try the bathtub and write code in my head :slight_smile:

ng

[import]uid: 61600 topic_id: 16071 reply_id: 59775[/import]

The weirdest time when I got a solution to a problem was in my dream. I not only wrote the code but also debugged and found the problem all in my dream.

No wonder they say “Sleep on it” and it did work for me

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16071 reply_id: 59807[/import]

Hahahaha, that is so insane and funniest thing ever, Jayant! [import]uid: 67217 topic_id: 16071 reply_id: 59821[/import]

Yes, that is… this was in 2000 when I was trying to resolve an issue with Custom Outlook forms implementation (long before Sharpoint existed, at that time MS was toying with Dashboard, a plug-in for Outlook) while implementing custom forms that had to work with both English and Arabic. The data was not getting saved to a database… and the only person that could test the Arabic part for me (as I cannot read/write or type it) was scarce as a resource.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16071 reply_id: 59822[/import]

Jayant,

Honestly, your case is awesome. I really do believe you about that. :] [import]uid: 89165 topic_id: 16071 reply_id: 59836[/import]