Set barrier that you cannot drag object past?

Hello everyone,

I was wondering if it is possible to create a barrier that will not let you drag a particular object past. If you attempted to drag past this barrier the object would just stay behind it. Perhaps a way to limit the x or the y coordinate to a particular range? Has anyone successfully done anything like this? Any insight is much appreciated. [import]uid: 31262 topic_id: 9050 reply_id: 309050[/import]

I figured it out. Just in case anyone else is looking for the same thing, it is a lot simpler than I expected. It’s something like this:

if "moved" == phase then  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
 if t.y \> 200 then  
 t.y = 200  
 end  
end  

This is just a snippet of the “drag platforms” project I was playing with and not the full code. [import]uid: 31262 topic_id: 9050 reply_id: 32998[/import]

@aaaron

I’ve been trying to do something similiar.

After I scale up my image I use the max content bounds to see if it go at or beyond it, then try to set the coordinate.

doesn’t seem to work.

any suggestions?

thx’s
Frank [import]uid: 31039 topic_id: 9050 reply_id: 35439[/import]

Do you have some code you can post? [import]uid: 31262 topic_id: 9050 reply_id: 35456[/import]

@aaaron
Sure.

The code that happen before this changes the original image x,y reference, then I scale up by 2. Orig content is 320 x 480; thus the hard coded 640;
local function onTouch( event )
if scale == 0 then
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, event.id )

– 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”).
if (myTab.contentBounds.xMax) >= 640 then

t.x0 = 320

else
t.x = event.x - t.x0
t.y = event.y - t.y0
end

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( t, nil )

t.isFocus = false
end
end

return true
end
end
myTab:addEventListener( “touch”, onTouch )

[import]uid: 31039 topic_id: 9050 reply_id: 35461[/import]

Excuse my ignorance as I’m still new at this but I’m not sure where scaling would be used. What is the maximum you are allowing your object to be dragged to? Is it set to 640 (or 320) here? Because that would be the edge of the screen which you can’t drag past anyways unless I am not understanding this correctly. [import]uid: 31262 topic_id: 9050 reply_id: 35467[/import]