Hello,
I am trying to some how have an undo button with the code i have below. So far all i have achieved has been to have a collision with a ball touching a line that you draw and the whole line remove it self (Go ahead and try it ). What i want is not to only remove all of the line, but to remove the previous line that was drawn. As in, if i draw 3 separate lines and the ball touches the first line the last line will disappear. If it touches two lines the last two lines will remove it self, and so on… I’ve tried adding a variable once the line function is ended (released) and deleting that variable, but no success. Does anyone know how this can be done? An example will be much appreciated. Thanks!
The code where a ball has collision with the line and the line removes it self:
*COPY PASTE FRIENDLY*
[code]
local physics = require (“physics”)
physics.start(true)
physics.setGravity(10, 0)
– Ball rolls on Line
local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(0, 255, 0)
ball.x = display.contentHeight/12
ball.y = display.contentWidth/4
physics.addBody(ball, {bounce=0.3, radius = 25, friction=0.5})
– Draw a line
local i = 1
local tempLine
local rectangle_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)
tempLine.alpha = 0
– 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
rectangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y)
rectangle_hit[i]:setColor(r,g, b)
rectangle_hit[i].width = 5
– Creates physic joints for line (Turn on draw mode to see the effects)
local Width = rectangle_hit[i].width * 0.6
local Height = rectangle_hit[i].height * 0.2
– Physic body for the line shape
local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height}
physics.addBody(rectangle_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)
local function wassup( event )
for i = 1, #rectangle_hit, 1 do
rectangle_hit[i]:removeSelf()
rectangle_hit[i] = nil
end
end
ball.collision = wassup
ball:addEventListener( “collision”, ball) [import]uid: 23689 topic_id: 13531 reply_id: 313531[/import]