How do you specify a new "grab point" for a draggable object?

I've created a wall in the middle of the screen that my player (a draggable object) cannot pass. It's coded as such:

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
local wall = display.newRect(0,512,768,10)
local player = display.newCircle(20,20,20)
--Player movement function
local function onTouch( event )
        local player = event.target     
        local phase = event.phase
        if "began" == phase then
                -- Make target the top-most object
                local parent = player.parent
                parent:insert( player )
                display.getCurrentStage():setFocus( player )
                player.isFocus = true
                -- Store initial position
                player.x0 = event.x - player.x
                player.y0 = event.y - player.y
        elseif player.isFocus then
                if "moved" == phase then
                        player.x = event.x - player.x0
                        player.y = event.y - player.y0                                             
                end
        end   
        return true             
end
player:addEventListener( "touch", onTouch )
 
--limit player move boundary
local function limitPlayer (event)
if player.y<532 then
player.y=542
end
end
Runtime:addEventListener("enterFrame", limitPlayer )

There's an example of exactly what you want to accomplish in the Air Hockey demo app.

-David

Hi Steven,
hope this is what you are after

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
local wall = display.newRect(0,512,768,10)
local player = display.newCircle(20,542,20)
 
 
--Player movement function
local function onTouch( event )
        local player = event.target     
        local phase = event.phase
        if "began" == phase then
                -- Make target the top-most object
                local parent = player.parent
                parent:insert( player )
                display.getCurrentStage():setFocus( player )
                player.isFocus = true
                -- Store initial position
                player.x0 = event.x - player.x 
                player.y0 = event.y - player.y
 
        elseif player.isFocus then
                if "moved" == phase then
                        player.x = event.x - player.x0
                        if event.y - player.y0     < 532 then
                            player.y=542
                            player.y0 = event.y - player.y
                        else  
                            player.y = event.y - player.y0   
                        end                
                end
        end   
        return true             
end
player:addEventListener( "touch", onTouch )

Hi Renjith,

You just did in two seconds what I could not in two days:) Yes, that's exactly what I was after, worked like a charm (and killed 2 birds with one stone by also solving the "snapping problem" I mentioned to you in my email)

As always, you have my utmost gratitude!

Cheers,
Steven

make it 3 birds... it paved way to a solution for an issue I was facing for long...

3 cheers! :)

views:1467 update:2011/10/22 9:46:13
corona forums © 2003-2011