Hi,
I am trying to make an eraser for a line, i came up with an idea but it is not quite working for me. I tried “tapping” a static body with a dynamic body using joints. And making the static body draggable. The problem is that when the block hits the drawn line( drag on the screen to create a line), the function (line:removeSelf() ) is not working quite right. Here is the code that I did this on:
[code]
local physics = require (“physics”)
physics.start(true)
–physics.setDrawMode “hybrid”
local i = 1
local tempLine
local ractgangle_hit = {}
local prevX , prevY
local function runTouch(event)
if(event.phase==“began”) then
if(tempLine==nil) then
tempLine=display.newLine(event.x, event.y, event.x, event.y)
– Random Colors for line
r = math.random (0, 255)
g = math.random ( 0, 255)
b = math.random (0, 255 )
tempLine:setColor(r,g, b)
prevX = event.x
prevY = event.y
end
elseif(event.phase==“moved”) then
tempLine:append(event.x,event.y-2)
tempLine.width=tempLine.width+0.9
ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
ractgangle_hit[i]:setColor(r,g, b)
ractgangle_hit[i].width = 5
– Creates physic joints for line (Turn on draw mode to see the effects)
local Width = ractgangle_hit[i].width * 0.6
local Height = ractgangle_hit[i].height * 0.2
– Physic body for the line shape
local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}
physics.addBody(ractgangle_hit[i], “static”, { bounce = -1, density=0.3, friction=0.7, shape = lineShape})
prevX = event.x
prevY = event.y
i = i + 1
elseif(event.phase==“ended”) then
tempLine.parent.remove(tempLine)
tempLine=nil
end
end
Runtime:addEventListener(“touch”, runTouch)
object01 = display.newRect(100,100,50,50)
physics.addBody( object01, “static”, {friction =0.1, isSensor = true, bounce = 0.1})
object01 :setFillColor(255,0,0)
object02 = display.newRect(99,99,70,70)
physics.addBody( object02, {friction =0.1, isSensor = true, bounce = 0.1})
object02 :setFillColor(255,0,0)
myJoint = physics.newJoint( “pivot”, object02, object01, 100, 100 )
myJoint = physics.newJoint( “pivot”, object02, object01, 100, 150 )
myJoint = physics.newJoint( “pivot”, object02, object01, 150, 150 )
myJoint = physics.newJoint( “pivot”, object02, object01, 150, 100 )
eventoffset = {x=0,y=0}
local function wassup( self, event )
tempLine:removeSelf()
ractgangle_hit[i]:removeSelf()
end
object02.collision = wassup
object02:addEventListener( “collision”, object02)
---------------- drag function
local function dragging (event)
local stage = display.getCurrentStage()
local target = event.target
if event.phase == “began” then – on touch
stage:setFocus(target, event.id)
eventoffset.x = event.x - target.x
eventoffset.y = event.y - target.y
group1:insert(target)
end
if event.phase == “moved” then – on move
target.x = event.x - eventoffset.x
target.y = event.y - eventoffset.y
end
if event.phase == “ended” or event.phase == “cancelled” then – on release
stage:setFocus(nil)
end
end
object01:addEventListener(“touch”, dragging)
[import]uid: 23689 topic_id: 12434 reply_id: 312434[/import]