I’ve created a wall in the middle of the screen that my player (a draggable object) cannot pass. It’s coded as such:
local wall = display.newRect(0,512,768,10)local player = display.newCircle(20,20,20)--Player movement functionlocal 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 endplayer:addEventListener( "touch", onTouch )--limit player move boundarylocal function limitPlayer (event)if player.y<532 thenplayer.y=542endendRuntime:addEventListener("enterFrame", limitPlayer )[/code]Right now, if you move the player up past the wall, the player will stop at the wall while your cursor continues through it. If you then want to start moving the player downwards again, you have to move your cursor down past the wall again before this will take effect. This is ok, but it's not what I want. What I want is for the downward movement of the cursor to take effect on the player immediately, regardless of whether or not the cursor is above the wall line. Does this make sense? If not, try dragging the player up to the wall, then continue moving the cursor up til it's at the top of the screen. Release, click again, and now you can move the player down immediately (albeit with a new "grab point"). This is the effect I'm after, although I don't want the person playing to have to retouch the screen, I want this to be automatic. Can anyone tell me how to achieve this? Any help would be much appreciated, as I've been puzzling over this for days (yes, I'm a beginner).Much thanks,Steven [import]uid: 79394 topic_id: 16581 reply_id: 316581[/import]
There’s an example of exactly what you want to accomplish in the Air Hockey demo app.
-David [import]uid: 96411 topic_id: 16581 reply_id: 61938[/import]
Hi Steven,
hope this is what you are after
[lua]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 )[/lua] [import]uid: 71210 topic_id: 16581 reply_id: 61986[/import]
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 [import]uid: 79394 topic_id: 16581 reply_id: 62027[/import]
make it 3 birds… it paved way to a solution for an issue I was facing for long… [import]uid: 71210 topic_id: 16581 reply_id: 62073[/import]
3 cheers!
[import]uid: 79394 topic_id: 16581 reply_id: 62075[/import]