Drag to draw line

local cir = display.newCircle(170,200,40) cir:setFillColor(0.4,0.5,0.6) local function cueShot( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) t.isFocus = true event.target.x = t.x event.target.y = t.y myLine = nil elseif t.isFocus then if "moved" == phase then if ( myLine ) then myLine.parent:remove( myLine ) -- erase previous line, if any end myLine = display.newLine( t.x,t.y, event.x,event.y ) myLine:setStrokeColor( 1, 1, 1,0.5) myLine.strokeWidth = 30 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false if ( myLine ) then myLine.parent:remove( myLine ) end end end return true -- Stop further propagation of touch event end cir:addEventListener("touch",cueShot)

How to draw a line from the circle by dragging the screen ? 

Hi @martinlim520,

What is the issue you’re having? As I quickly look at your code, you have the correct idea: draw a line, and on any movement of the touch, erase any previous line and draw a new one in its place. That makes it appear as if the user is constantly dragging a “cue shot” line away from the ball.

Take care,

Brent

I want to draw a line start from the circle and getting longer or getting shorter follow the way I drag . I have change the 

cir:addEventListener("touch",cueShot)

 to 

Runtime:addEventListener("touch",cueShot)

 but the end of the line is in where I touch , I want the end of the line start from the circle and follow where I dragging . What I should change in the cueShot function ?

 myLine = display.newLine( t.x,t.y, event.x,event.y )

local cir = display.newCircle(170,200,40) cir:setFillColor(0.4,0.5,0.6) local function cueShot( event ) local t = cir local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) t.isFocus = true cir.x = t.x cir.y = t.y myLine = nil myLine = display.newLine( t.x,t.y,t.x,t.y+20) myLine:setStrokeColor(1,1,1) myLine.strokeWidth = 30 elseif t.isFocus then if "moved" == phase then if ( myLine ) then myLine.parent:remove( myLine ) -- erase previous line, if any end myLine = display.newLine( t.x,t.y,event.x,event.y) myLine:setStrokeColor( 1, 1, 1,0.5) myLine.strokeWidth = 30 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false if ( myLine ) then myLine.parent:remove( myLine ) end end end return true -- Stop further propagation of touch event end Runtime:addEventListener("touch",cueShot)

I’m not sure about the answer to your question, but you can replace this:

if ( myLine ) then myLine.parent:remove( myLine ) -- erase previous line, if any end

with this:

display.remove( myLine ) myLine = nil

Hi @martinlim520,

What is the issue you’re having? As I quickly look at your code, you have the correct idea: draw a line, and on any movement of the touch, erase any previous line and draw a new one in its place. That makes it appear as if the user is constantly dragging a “cue shot” line away from the ball.

Take care,

Brent

I want to draw a line start from the circle and getting longer or getting shorter follow the way I drag . I have change the 

cir:addEventListener("touch",cueShot)

 to 

Runtime:addEventListener("touch",cueShot)

 but the end of the line is in where I touch , I want the end of the line start from the circle and follow where I dragging . What I should change in the cueShot function ?

 myLine = display.newLine( t.x,t.y, event.x,event.y )

local cir = display.newCircle(170,200,40) cir:setFillColor(0.4,0.5,0.6) local function cueShot( event ) local t = cir local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) t.isFocus = true cir.x = t.x cir.y = t.y myLine = nil myLine = display.newLine( t.x,t.y,t.x,t.y+20) myLine:setStrokeColor(1,1,1) myLine.strokeWidth = 30 elseif t.isFocus then if "moved" == phase then if ( myLine ) then myLine.parent:remove( myLine ) -- erase previous line, if any end myLine = display.newLine( t.x,t.y,event.x,event.y) myLine:setStrokeColor( 1, 1, 1,0.5) myLine.strokeWidth = 30 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false if ( myLine ) then myLine.parent:remove( myLine ) end end end return true -- Stop further propagation of touch event end Runtime:addEventListener("touch",cueShot)

I’m not sure about the answer to your question, but you can replace this:

if ( myLine ) then myLine.parent:remove( myLine ) -- erase previous line, if any end

with this:

display.remove( myLine ) myLine = nil