Remove display-object and place a new one on random position

Hey Coronans out there! :slight_smile:

I ve got the following problem:

I place a picture on the screen and add an EventListener to it. If the user touch the pic the pic should disappear and after a delay it should appear again at a random position. So far, so good.

local removePic = function( event ) local t = event.target; local phase = event.phase; if "began" == phase then t:removeSelf(); picTimer(); end return true end local function setPic() pic = display.newImage( "erdm.png", true ); pic.y = math.random(400, 700); pic.x = math.random(50, 1000); pic.width = 50; pic.height = 100; pic:addEventListener( "touch", removePic ); end local function picTimer() timer.performWithDelay( 1000, setPic, 1); end picTimer();

I know that the removePic-function “dont know” the picTimer()-function so an error occurs. 

But whats the best way to construct something like this? :wacko:

Thanks in advance!

The easiest way I can think of is this:

Somewhere at the start of the lua file (BEFORE your removePic function), write this:

local picTimer

Then change the function definition to this:

picTimer = function()     timer.performWithDelay( 1000, setPic, 1); end

Done!

This is known as a forward declaration:

http://www.coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/

That is exactly what I am looking for. Thanks a lot!!!

The easiest way I can think of is this:

Somewhere at the start of the lua file (BEFORE your removePic function), write this:

local picTimer

Then change the function definition to this:

picTimer = function()     timer.performWithDelay( 1000, setPic, 1); end

Done!

This is known as a forward declaration:

http://www.coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/

That is exactly what I am looking for. Thanks a lot!!!