Hey guys,
i want to develop an solitare card game and im using the basic drag and drop code with collision detection.
Every card is an object and has a tap eventListener that calls “dragCard”.
That code works perfect when i only drag and drop single cards but i have to drag and drop multi cards and the same time.
I really dont know how i can to that and now i’m trying to make a check in the eventListener if only one card or multi cards should be dragged. If multi cards should be dragged, i make a new display group and insert all cards in that new display group that should be draggen and at the end i want to chance the target(event.target) to this new display group instead of only dragging the single card but when i try my code in the game, i cant move anything after i want to dragg multi cards
maybe you can help me with my code or maybe you have a better solution for me how i can solve the multi drag problem.
local target local function dragCard( event ) local phase = event.phase if ( event.phase == "began" ) then if event.target.parent.numChildren == event.target.pos then target = event.target else local newGrp = display.newGroup() newGrp.color = event.target.cColor newGrp.cNumber = event.target.cNumber local diff = event.target.parent.numChildren - event.target.pos local num = event.target.parent.numChildren for i = 0, diff do local cardObj = event.target.parent[event.target.parent.numChildren] local index = (num-i) newGrp:insert(index , cardObj) end target = newGrp end local parent = target.parent display.getCurrentStage():setFocus( target ) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif ( target.isFocus ) then if ( phase == "moved" ) then target.x = event.x - target.x0 target.y = event.y - target.y0 elseif ( phase == "ended" or phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) target.isFocus = false if ( hasCollided( event.target, hotSpots ) ) then -- Snap in place transition.to( event.target, {time=0, x=hotSpotX, y=hotSpotY} ) else -- Move back transition.to( event.target, {time=0, x=event.target.xOrig, y=event.target.yOrig} ) end end end return true end
and this part is my own code, the rest of the code is for single object dragging:
if event.target.parent.numChildren == event.target.pos then target = event.target else local newGrp = display.newGroup() newGrp.color = event.target.cColor newGrp.cNumber = event.target.cNumber local diff = event.target.parent.numChildren - event.target.pos local num = event.target.parent.numChildren for i = 0, diff do local cardObj = event.target.parent[event.target.parent.numChildren] local index = (num-i) newGrp:insert(index , cardObj) end target = newGrp end
I hope you understand my problem
Thank you
Br,
a.jaks