Hi,
I’m just starting to use Corona and I’m stuck trying to use removeSelf( ) with a timer.
I’m trying to simulate the blade effect in Fruit Ninja. I create a display object called “rollerpoint” and I want to remove it after 400 milliseconds, but I don’t get it to work. I got pen.lua:32: attempt to index upvalue ‘rollerpoint’ (a nil value).
I haven’t found a way to fix this. Can anyone help me out please. I attach my code.
Cheers!
[lua]local pen = {}
local rollerpoint
pen.rollerpoint = rollerpoint
local color = {1,1,1}
pen.color = color
local lastX = 0
pen.lastX = lastX
local lastY = 0
pen.lastY = lastY
local distBetween = function ( x1, y1, x2, y2 ) – distance between 2 points
local xFactor = x2 - x1
local yFactor = y2 - y1
local dist = math.sqrt( (xFactor*xFactor) + (yFactor*yFactor) )
return dist
end
pen.distBetween = distBetween
local destroy = function( event )
print( “listener called” )
rollerpoint:removeSelf( )
rollerpoint = nil
end
pen.destroy = destroy
local draw = function(target)
local drawing = function(event)
if ( event.phase == “began”) then
–code executed when the player touches the background
print( “bg touched = “…tostring(event.x)…”,”…tostring(event.y) ) --‘event.target’ is the touched object
lastX = event.x
lastY = event.y
elseif (event.phase == “moved”) then
rollerpoint = display.newImageRect( “images/bgs/pen.png”, 40, 7 )
rollerpoint.fill = {color}
rollerpoint:rotate( math.deg(math.atan2(event.y - lastY, event.x - lastX )) ) --it gets the angle between 2 points.
rollerpoint.width = distBetween(lastX, lastY , event.x, event.y)
–transition.to( rollerpoint, {time = 0, xScale = rollerpoint.width * .32} )
transition.to( rollerpoint, {time = 400, height = .1} )
rollerpoint.x = (event.x + lastX) / 2
rollerpoint.y = (event.y + lastY) / 2
lastX = event.x
lastY = event.y
timer.performWithDelay( 400, destroy )
end
end
target:addEventListener( “touch”, drawing ) --add a “touch” listener to playBtn
end
pen.draw = draw
return pen[/lua]