Newbie stuck with timer to remove display object

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]

It’s saying that the error is on line 32, so before you even try to remove it. That would imply that line 31 is not successful:

rollerpoint = display.newImageRect( "images/bgs/pen.png", 40, 7 )

My guess would be that there is a typo in the file name “images/bgs/pen.png”. Corona is case sensitive, so double check that the path name exactly matches the real directory + file names.

e.g. If your folder is called “BGS” then it will not work as it is upper case and the path name in your code is lower case)

Thanks Alan.

I already solved it. It turned out to be an scope problem. I leave my corrected code here in case anybody needs to use the Fruit Ninja blade effect in one game.

[lua]local UI = require(“UI”)

local pen = {}

local rollerpoint

pen.rollerpoint = rollerpoint

–local color = { math.random(10)/10 , math.random(10)/10 , math.random(10)/10 }

–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 ( object )

local function remove( … )
object:removeSelf( )
object = nil
end
timer.performWithDelay( 300, remove )
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:setFillColor( UI.settings.penColor[1],UI.settings.penColor[2],UI.settings.penColor[3] )
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) * .5
rollerpoint.y = (event.y + lastY) * .5
lastX = event.x
lastY = event.y
destroy(rollerpoint)
end

end
target:addEventListener( “touch”, drawing )
end

pen.draw = draw

return pen
[/lua]

It’s saying that the error is on line 32, so before you even try to remove it. That would imply that line 31 is not successful:

rollerpoint = display.newImageRect( "images/bgs/pen.png", 40, 7 )

My guess would be that there is a typo in the file name “images/bgs/pen.png”. Corona is case sensitive, so double check that the path name exactly matches the real directory + file names.

e.g. If your folder is called “BGS” then it will not work as it is upper case and the path name in your code is lower case)

Thanks Alan.

I already solved it. It turned out to be an scope problem. I leave my corrected code here in case anybody needs to use the Fruit Ninja blade effect in one game.

[lua]local UI = require(“UI”)

local pen = {}

local rollerpoint

pen.rollerpoint = rollerpoint

–local color = { math.random(10)/10 , math.random(10)/10 , math.random(10)/10 }

–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 ( object )

local function remove( … )
object:removeSelf( )
object = nil
end
timer.performWithDelay( 300, remove )
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:setFillColor( UI.settings.penColor[1],UI.settings.penColor[2],UI.settings.penColor[3] )
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) * .5
rollerpoint.y = (event.y + lastY) * .5
lastX = event.x
lastY = event.y
destroy(rollerpoint)
end

end
target:addEventListener( “touch”, drawing )
end

pen.draw = draw

return pen
[/lua]