Who can give me a example code for drag object like tetris , please ?
Search the forums, there are many examples.
And the sample code that came with Corona. There is a sample in there that specifically covers dragging things.
local bg = display.newImage("bg.png") bg.x=160 bg.y=240 local rect local function moveObject(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 ) t.isFocus = true elseif t.isFocus then if "moved" == phase then local gridx = math.floor(event.x/35) -- local gridy = math.floor(event.y/35) local gridxpos = gridx \* 35 -- local gridypos = gridy \* 35 if gridx \> 0 and gridx \< 8 then --and gridy \> -1 and gridy \< 9 then t.x = gridxpos -- t.y = gridypos end elseif "ended" == phase then display.getCurrentStage():setFocus( nil ) event.target.isFocus = false end end return true end local line1,line2 local x = 35 for i=1,10 do line1 = display.newLine(0,x,320,x) line2 = display.newLine(x,0,x,480) line1.alpha = .2 line2.alpha = .2 x=x+35 end local function callNewBlock() rect = display.newRect(35,0,35,35) rect:setReferencePoint(display.TopLeftReferencePoint) rect:addEventListener("touch",moveObject) transition.to(rect , {time = 11500 , y=400}) end callNewBlock() timer.performWithDelay(11500, callNewBlock,0)
How can I drag the object by touching the background not touching the rect to drag , thanks .
Search the forums, there are many examples.
And the sample code that came with Corona. There is a sample in there that specifically covers dragging things.
local bg = display.newImage("bg.png") bg.x=160 bg.y=240 local rect local function moveObject(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 ) t.isFocus = true elseif t.isFocus then if "moved" == phase then local gridx = math.floor(event.x/35) -- local gridy = math.floor(event.y/35) local gridxpos = gridx \* 35 -- local gridypos = gridy \* 35 if gridx \> 0 and gridx \< 8 then --and gridy \> -1 and gridy \< 9 then t.x = gridxpos -- t.y = gridypos end elseif "ended" == phase then display.getCurrentStage():setFocus( nil ) event.target.isFocus = false end end return true end local line1,line2 local x = 35 for i=1,10 do line1 = display.newLine(0,x,320,x) line2 = display.newLine(x,0,x,480) line1.alpha = .2 line2.alpha = .2 x=x+35 end local function callNewBlock() rect = display.newRect(35,0,35,35) rect:setReferencePoint(display.TopLeftReferencePoint) rect:addEventListener("touch",moveObject) transition.to(rect , {time = 11500 , y=400}) end callNewBlock() timer.performWithDelay(11500, callNewBlock,0)
How can I drag the object by touching the background not touching the rect to drag , thanks .