drag function

http://moh97.us/flow/

How to make a drag function like this game ?

 local function onTouch(e) if e.phase == "began" then elseif e.phase == "moved" then if e.target.on == false then e.target:setFillColor(0,0,1) e.target.on = true else e.target:setFillColor(1,1,1) e.target.on = false end end end local box = {} for i = 1 ,5 do box[i] = {} for j = 1, 8 do box[i][j] = display.newCircle(0,0,25) box[i][j].x = i\*55 box[i][j].y = j\*55 box[i][j]:addEventListener("touch",onTouch) end end

That game really isn’t “dragging” anything around.  It just senses when the mouse cursor is over a grid and then animates the drawing of the line to fill that spot.

There are a couple of different ways to implement this.  One is to have a touch handler on each rectangle and when you see a moved phase on that rectangle you can then draw the appropriate graphic.  You will probably need to keep track of what grid square was touched last so that you know which image to draw representing the movement.

The other way is to simply make the whole screen a touch target and collect the current X, Y of the tough event and then check to see if it’s within the bounds of a square and go from there.

Rob

rect = {} local function touch(e) local id = e.target.id local id2 = e.target.id2 if e.phase == "began" then elseif e.phase == "moved" then if e.y \<= rect[id][id2].contentBounds.yMin --or e.y \>= rect[id][id2].contentBounds.yMax then rect[id][id2]:setFillColor(1,1,1) rect[id][id2].alpha = 0.5 end end end local function touch2(e) local id = e.target.id local id2 = e.target.id2 if e.phase== "began" then elseif e.phase == "moved" then rect[id][id2]:setFillColor(1,0,0) rect[id][id2].alpha = 1 rect[id][id2]:addEventListener("touch",touch) end end for i = 1 ,1 do rect[i] ={} for j = 1 , 5 do rect[i][j] = display.newRect( 100, 100, 50,50 ) rect[i][j].x = i \*55 +150 rect[i][j].y = j\*55 rect[i][j].id = i rect[i][j].id2 = j rect[i][j]:setFillColor( 1, 1, 1 ) rect[i][j].alpha = 0.5 rect[i][j]:addEventListener("touch",touch2) end end

When I touch and move the grid will change to red color , then how to change the grid color to white when I move to the opposite direction ?

That game really isn’t “dragging” anything around.  It just senses when the mouse cursor is over a grid and then animates the drawing of the line to fill that spot.

There are a couple of different ways to implement this.  One is to have a touch handler on each rectangle and when you see a moved phase on that rectangle you can then draw the appropriate graphic.  You will probably need to keep track of what grid square was touched last so that you know which image to draw representing the movement.

The other way is to simply make the whole screen a touch target and collect the current X, Y of the tough event and then check to see if it’s within the bounds of a square and go from there.

Rob

rect = {} local function touch(e) local id = e.target.id local id2 = e.target.id2 if e.phase == "began" then elseif e.phase == "moved" then if e.y \<= rect[id][id2].contentBounds.yMin --or e.y \>= rect[id][id2].contentBounds.yMax then rect[id][id2]:setFillColor(1,1,1) rect[id][id2].alpha = 0.5 end end end local function touch2(e) local id = e.target.id local id2 = e.target.id2 if e.phase== "began" then elseif e.phase == "moved" then rect[id][id2]:setFillColor(1,0,0) rect[id][id2].alpha = 1 rect[id][id2]:addEventListener("touch",touch) end end for i = 1 ,1 do rect[i] ={} for j = 1 , 5 do rect[i][j] = display.newRect( 100, 100, 50,50 ) rect[i][j].x = i \*55 +150 rect[i][j].y = j\*55 rect[i][j].id = i rect[i][j].id2 = j rect[i][j]:setFillColor( 1, 1, 1 ) rect[i][j].alpha = 0.5 rect[i][j]:addEventListener("touch",touch2) end end

When I touch and move the grid will change to red color , then how to change the grid color to white when I move to the opposite direction ?