Guidance on spawning multiple circles and removing a circle on tap.

So right now I have one circle on the screen and when I tap the circle I pretty much transition the circles alpha to zero then set the circle to nil to remove it. What I would like to do is spawn multiple circles and use the same function to remove each circle I tap. Is it possible to have a event listener that listens to multiple circles rather than just one? Not sure how to approach this with corona.

yep you sure can,

It’s late and I hope I am not talking crazy.

I use this code to call generic functions to create images and navigation buttons that can attach to the same event listener.

the CreateStaticImage function could easily be modified to add an event listener ( just like the navigation createNavButton )

if you are trying to destroy the object after the click it just setting it to nil might not totally work I think you have to use remove or removeSelf ( I cant recall ). If you do that in a touch event you are going to need to wrap the remove or remove self in an PerformWithDelay method call passing in a 1 for a 1 millisecond delay. ( that’s because the delay gives the current touch event time to complete its work before you remove the object - same thing with collisions ( fyi ) ).

Let me know if you have any issues or questions with the example below

Larry

 local topLogo = createStaticImage("top", "images/topsmall.png", 428, 206, centerX/2-100,appGlobals.screenTop+141); sceneGroup:insert( topLogo ); sceneGroup.topCatText = topLogo; local getstarted = createNavButton( sceneGroup, "getstarted", "menu", "images/getstarted.png", 628, 702, centerX,appGlobals.screenBottom-351); sceneGroup:insert( getstarted); sceneGroup.oBtn = getstarted;

these are common functions used to handle creating the images and navigation buttons and acting on the touch event

-------------------------------------------------------------------------------- -- This is the Global Navigation Event Listener Touch Handler -------------------------------------------------------------------------------- function onNavigationTouch( self, event ) if event.phase == "began" then print( "Touch event began on: " .. self.action ) local action = self.action; local options = {effect="flipFadeOutIn", time=500}; print("eventListener onNavigationTouch Removed") if action == "pause" then composer.showOverlay(action, {isModal=true, effect="fromTop"} ) elseif action == "verify" then self:removeEventListener( "touch", onNavigationTouch ); composer.removeScene("menu"); composer.gotoScene( "verify", options ); elseif action == "newgame" then self:removeEventListener( "touch", onNavigationTouch ); composer.removeScene("gameloop"); composer.gotoScene( "gamerecycle", options ); --composer.showOverlay("gameover", {isModal=true, effect="fromTop"} ) elseif action == "resume" then composer.hideOverlay("slideUp", 500); elseif action == "removeads" then if(isAdDisplayed == true) then isAdDisplayed = false; else isAdDisplayed = true; end -- Dispatch an event named "requestingMessageBox" to be received by C#. local ShowHideWindowsAdsEvent = { name = "ShowHideWindowsAds", showad = isAdDisplayed, } --local result = Runtime:dispatchEvent(ShowHideWindowsAdsEvent) print(result); elseif action == "messagebox" then -- Dispatch an event named "requestingMessageBox" to be received by C#. local requestingMessageBoxEvent = { name = "requestingMessageBoxEvent", message = "Phone Native Message box - From LUA", } --local result = Runtime:dispatchEvent(requestingMessageBoxEvent); print(result); else self:removeEventListener( "touch", onNavigationTouch ); composer.gotoScene( action, options ) end end return true end -------------------------------------------------------------------------------- -- Global Navigation Button Creator -------------------------------------------------------------------------------- function createNavButton(sceneGroup, sButtonName, sAction, sImage, oWidth, oHeight, x,y,anchorX,anchorY) local oBtn = display.newImageRect( sImage, oWidth, oHeight ); oBtn.x = x; oBtn.y = y; oBtn.alpha = 1; oBtn.oldAlpha = 1 oBtn.oriX = oBtn.x; oBtn.oriY = oBtn.y; oBtn.oriXs = oBtn.xScale; oBtn.oriYs = oBtn.yScale; oBtn.name = sButtonName; oBtn.action = sAction; oBtn.isVisible = true; oBtn.touch = onNavigationTouch; -- Global Navigation Event Listener if (anchorX ~= nil) then oBtn.anchorX = anchorX; end if (anchorY ~= nil) then oBtn.anchorY = anchorY; end oBtn:addEventListener( "touch", oBtn ); -- sceneGroup:insert( oBtn); -- sceneGroup.oBtn = oBtn; return oBtn; end function createStaticImage(sName, sImage, oWidth, oHeight, x,y,anchorX,anchorY) local oBtn = display.newImageRect( sImage, oWidth, oHeight ); oBtn.x = x; oBtn.y = y; oBtn.alpha = 1; oBtn.oldAlpha = 1 oBtn.oriX = oBtn.x; oBtn.oriY = oBtn.y; oBtn.oriXs = oBtn.xScale; oBtn.oriYs = oBtn.yScale; oBtn.name = sName; oBtn.isVisible = true; if (anchorX ~= nil) then oBtn.anchorX = anchorX; end if (anchorY ~= nil) then oBtn.anchorY = anchorY; end return oBtn; end

yep you sure can,

It’s late and I hope I am not talking crazy.

I use this code to call generic functions to create images and navigation buttons that can attach to the same event listener.

the CreateStaticImage function could easily be modified to add an event listener ( just like the navigation createNavButton )

if you are trying to destroy the object after the click it just setting it to nil might not totally work I think you have to use remove or removeSelf ( I cant recall ). If you do that in a touch event you are going to need to wrap the remove or remove self in an PerformWithDelay method call passing in a 1 for a 1 millisecond delay. ( that’s because the delay gives the current touch event time to complete its work before you remove the object - same thing with collisions ( fyi ) ).

Let me know if you have any issues or questions with the example below

Larry

 local topLogo = createStaticImage("top", "images/topsmall.png", 428, 206, centerX/2-100,appGlobals.screenTop+141); sceneGroup:insert( topLogo ); sceneGroup.topCatText = topLogo; local getstarted = createNavButton( sceneGroup, "getstarted", "menu", "images/getstarted.png", 628, 702, centerX,appGlobals.screenBottom-351); sceneGroup:insert( getstarted); sceneGroup.oBtn = getstarted;

these are common functions used to handle creating the images and navigation buttons and acting on the touch event

-------------------------------------------------------------------------------- -- This is the Global Navigation Event Listener Touch Handler -------------------------------------------------------------------------------- function onNavigationTouch( self, event ) if event.phase == "began" then print( "Touch event began on: " .. self.action ) local action = self.action; local options = {effect="flipFadeOutIn", time=500}; print("eventListener onNavigationTouch Removed") if action == "pause" then composer.showOverlay(action, {isModal=true, effect="fromTop"} ) elseif action == "verify" then self:removeEventListener( "touch", onNavigationTouch ); composer.removeScene("menu"); composer.gotoScene( "verify", options ); elseif action == "newgame" then self:removeEventListener( "touch", onNavigationTouch ); composer.removeScene("gameloop"); composer.gotoScene( "gamerecycle", options ); --composer.showOverlay("gameover", {isModal=true, effect="fromTop"} ) elseif action == "resume" then composer.hideOverlay("slideUp", 500); elseif action == "removeads" then if(isAdDisplayed == true) then isAdDisplayed = false; else isAdDisplayed = true; end -- Dispatch an event named "requestingMessageBox" to be received by C#. local ShowHideWindowsAdsEvent = { name = "ShowHideWindowsAds", showad = isAdDisplayed, } --local result = Runtime:dispatchEvent(ShowHideWindowsAdsEvent) print(result); elseif action == "messagebox" then -- Dispatch an event named "requestingMessageBox" to be received by C#. local requestingMessageBoxEvent = { name = "requestingMessageBoxEvent", message = "Phone Native Message box - From LUA", } --local result = Runtime:dispatchEvent(requestingMessageBoxEvent); print(result); else self:removeEventListener( "touch", onNavigationTouch ); composer.gotoScene( action, options ) end end return true end -------------------------------------------------------------------------------- -- Global Navigation Button Creator -------------------------------------------------------------------------------- function createNavButton(sceneGroup, sButtonName, sAction, sImage, oWidth, oHeight, x,y,anchorX,anchorY) local oBtn = display.newImageRect( sImage, oWidth, oHeight ); oBtn.x = x; oBtn.y = y; oBtn.alpha = 1; oBtn.oldAlpha = 1 oBtn.oriX = oBtn.x; oBtn.oriY = oBtn.y; oBtn.oriXs = oBtn.xScale; oBtn.oriYs = oBtn.yScale; oBtn.name = sButtonName; oBtn.action = sAction; oBtn.isVisible = true; oBtn.touch = onNavigationTouch; -- Global Navigation Event Listener if (anchorX ~= nil) then oBtn.anchorX = anchorX; end if (anchorY ~= nil) then oBtn.anchorY = anchorY; end oBtn:addEventListener( "touch", oBtn ); -- sceneGroup:insert( oBtn); -- sceneGroup.oBtn = oBtn; return oBtn; end function createStaticImage(sName, sImage, oWidth, oHeight, x,y,anchorX,anchorY) local oBtn = display.newImageRect( sImage, oWidth, oHeight ); oBtn.x = x; oBtn.y = y; oBtn.alpha = 1; oBtn.oldAlpha = 1 oBtn.oriX = oBtn.x; oBtn.oriY = oBtn.y; oBtn.oriXs = oBtn.xScale; oBtn.oriYs = oBtn.yScale; oBtn.name = sName; oBtn.isVisible = true; if (anchorX ~= nil) then oBtn.anchorX = anchorX; end if (anchorY ~= nil) then oBtn.anchorY = anchorY; end return oBtn; end