Dragging Bounds

In my code below, I want to make it so that the rectangle that is being dragged can only be dragged vertically in a straight line. How would I do that? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
system.activate( "multitouch" )
 
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, 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").
                        t.x = event.x - t.x0
                        t.y = event.y - t.y0
                elseif "ended" == phase or "cancelled" == phase then
                        display.getCurrentStage():setFocus( t, 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
 
local rectangle = display.newRect(100,100,50,50) 
rectangle:addEventListener("touch",onTouch)
 

If thats all you want then all you have to do is remove the part that tells the rectangle to move on the x-axis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
system.activate( "multitouch" )
 
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, 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.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.y = event.y - t.y0
                elseif "ended" == phase or "cancelled" == phase then
                        display.getCurrentStage():setFocus( t, 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
 
local rectangle = display.newRect(100,100,50,50) 
rectangle:addEventListener("touch",onTouch)
views:1663 update:2011/9/28 21:38:26
corona forums © 2003-2011