[Resolved] Dragging and Tapping

I am only a week into programming with Corona and am still trying to get to grips with some of the features.

I am using the Drag Me example from the Corona samples for my card game.
how can i add a “Tap” event listener to the cards as well as being able to drag them.

For instance I want the user to be able to manually shuffle the cards using dragging and when they want to select one just tap and the card will disappear or transition off the screen.

my very noob understanding of listeners is tap is different to touch events allowing a quick press of an object and if it remains at the same x/y you can trigger an event.
I tried adding a tap listener but it seems to take no effect.

I hope you understand what I’m after. drag the card then tap to select

Any advice would be much appreciated
[import]uid: 142733 topic_id: 25474 reply_id: 325474[/import]

Hey there, try running this code on its own - it will print in the terminal when tapped and can also be dragged around.

[lua]–Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

–Show a card
local card = display.newRect( 50, 50, 80, 120 )

–Dragging
local function drag(event)
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
end
card:addEventListener(“touch”, drag)

–Tapping
local function tapCard()
print “Card tapped!”
end
card:addEventListener(“tap”, tapCard)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 25474 reply_id: 103048[/import]

Thanks Peach,
this works but when I apply the function to the tapped card it selects all the cards beneath it then transitions all of them off the screen. I can’t work out what the tap event phases are to be able to focus on just that card I am tapping on and then apply the transition to only that card.
I’ve read that return true does this but don’t know how to implement this if there is no touch phase.

[import]uid: 142733 topic_id: 25474 reply_id: 103088[/import]

Try this;

[lua]–Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

–Show a card
local card = display.newRect( 50, 50, 80, 120 )

–Dragging
local function drag(event)
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
end
card:addEventListener(“touch”, drag)

–Tapping
local function tapCard()
print “Card tapped!”
return true
end
card:addEventListener(“tap”, tapCard)

–Second card to show return true is working
local card2 = display.newRect( 80, 50, 80, 120 )
card2:setFillColor(255, 0, 0)
card2:toBack()

local function touchCard2()
print “If this shows when you touch card1 there is a problem”
end
card2:addEventListener(“tap”, touchCard2)[/lua]
As you can see I added return true and I added a second card to show how it works, which is red and half behind the white card.

Peach :slight_smile: [import]uid: 52491 topic_id: 25474 reply_id: 103238[/import]

Great! That worked. Many thanks! [import]uid: 142733 topic_id: 25474 reply_id: 103271[/import]

No worries, marking as resolved :slight_smile: [import]uid: 52491 topic_id: 25474 reply_id: 103467[/import]