Create a button that can add multiples of the same object?

I want to make a button that can spawn 3 eggplants one after another, ie click the button once, one gets spawn then click again and another is spawned. How can I do this and limit it to 3 eggplants maximum on screen at a time.

Could you show an example using my code? 

 local eggplant = display.newImageRect( scene.perRunGroup, "shapes/monster.png", 74, 74, display.contentCenterX, display.contentCenterY, 30) eggplant.alpha = 0 eggplant.name = eggplant eggplant.collisionType = "eggplant" -------------------------Add eggplant button local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if event.phase == "ended" and eggplant.alpha ~= 1 then eggplant.alpha = 1 physics.addBody(eggplant, "dynamic", {radius = 20, friction = .4, density = 1, bounce = 0}) eggplant.x = 100 eggplant.y = 0 eggplant.gravityScale = 1 eggplant.rotation = 0 end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local addeggplant = widget.newButton( { width = 104, height = 128, defaultFile = "shapes/pipebutton.png", overFile = "shapes/pipebutton.png", label = "", onEvent = handleButtonEvent } ) -- Center the button addeggplant.x = 100 addeggplant.y = 5 -- Change the button's label text addeggplant:setLabel( "" ) sceneGroup:insert(addeggplant)

I know I have to use some sort of a counter, but have no idea how to make one

The way I would do it would be to first of all create a local variable to act as the counter.  It would be best to do this at the top of your file where you declare your other locals.  The code for creating your eggplant should be inside the button handler function, at which point you check to see if your counter is less than 3 and if it is create the new eggplant, not forgetting to increase the counter.

You should also remember that whenever you remove an eggplant you should decrease the counter (if you should need to create more). 

local numberOfEggPlants = 0 -- Forward declare the counter -------------------------Add eggplant button local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if event.phase == "ended" and numberOfEggPlants \< 3 then eggplant = display.newImageRect( scene.perRunGroup, "shapes/monster.png", 74, 74, display.contentCenterX, display.contentCenterY, 30) eggplant.name = eggplant eggplant.collisionType = "eggplant" physics.addBody(eggplant, "dynamic", {radius = 20, friction = .4, density = 1, bounce = 0}) eggplant.x = 100 eggplant.y = 0 eggplant.gravityScale = 1 eggplant.rotation = 0 numberOfEggPlants = numberOfEggPlants + 1 -- Increase the counter by 1 end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local addeggplant = widget.newButton( { width = 104, height = 128, defaultFile = "shapes/pipebutton.png", overFile = "shapes/pipebutton.png", label = "", onEvent = handleButtonEvent } ) -- Center the button addeggplant.x = 100 addeggplant.y = 5 -- Change the button's label text addeggplant:setLabel( "" ) sceneGroup:insert(addeggplant)&nbsp;

Hope that helps in some way.

Cheers, sorry for the late reply, this works perfectly, just that when I put 

“numberOfEggPlants = numberOfEggPlants - 1”

In my remove button it only removes one eggplant. If I had 3 eggplants on screen, the button will remove the first one but then if clicked again the second one won’t be removed, do you know why? I don’t want them to all be removed at once, just one at a time.

Here is my remove button code if you need it

 ------------------------------------------------Remove eggplant button local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if event.phase == "ended" and eggplant.alpha ~= 0 then eggplant.alpha = 0 physics.removeBody(eggplant) speed.alpha = 0.65 speed.isSensor = true physics.addBody(speed, "dynamic", {radius = 15, friction = .4, density = 0, bounce = 0}) speed.gravityScale = 0 speed.rotation = 0 speed.linearDamping = 900000000000000000000 speed.angularDamping = 9000000000000000000 speed.isSensor = true speed.isFixedRotation = true numberOfEggPlants = numberOfEggPlants - 1 ---- Decrease the counter by 1 end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local removeeggplant = widget.newButton( { width = 64, height = 70, defaultFile = "images/restartdefault.png", overFile = "images/restartover.png", label = "", onEvent = handleButtonEvent } ) -- Center the button removeeggplant.x = 1025 removeeggplant.y = 40 -- Change the button's label text removeeggplant:setLabel( "" ) sceneGroup:insert(removeeggplant)

If you want three in a succession, I would take all the code and put it into it’s own Spawn function. Then in your button handler, call timer.performWithDelay() to run with 3 iterations, and use the spawn function as the function for the timer to run.

Rob

The way I would do it would be to first of all create a local variable to act as the counter.  It would be best to do this at the top of your file where you declare your other locals.  The code for creating your eggplant should be inside the button handler function, at which point you check to see if your counter is less than 3 and if it is create the new eggplant, not forgetting to increase the counter.

You should also remember that whenever you remove an eggplant you should decrease the counter (if you should need to create more). 

local numberOfEggPlants = 0 -- Forward declare the counter -------------------------Add eggplant button local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if event.phase == "ended" and numberOfEggPlants \< 3 then eggplant = display.newImageRect( scene.perRunGroup, "shapes/monster.png", 74, 74, display.contentCenterX, display.contentCenterY, 30) eggplant.name = eggplant eggplant.collisionType = "eggplant" physics.addBody(eggplant, "dynamic", {radius = 20, friction = .4, density = 1, bounce = 0}) eggplant.x = 100 eggplant.y = 0 eggplant.gravityScale = 1 eggplant.rotation = 0 numberOfEggPlants = numberOfEggPlants + 1 -- Increase the counter by 1 end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local addeggplant = widget.newButton( { width = 104, height = 128, defaultFile = "shapes/pipebutton.png", overFile = "shapes/pipebutton.png", label = "", onEvent = handleButtonEvent } ) -- Center the button addeggplant.x = 100 addeggplant.y = 5 -- Change the button's label text addeggplant:setLabel( "" ) sceneGroup:insert(addeggplant)&nbsp;

Hope that helps in some way.

Cheers, sorry for the late reply, this works perfectly, just that when I put 

“numberOfEggPlants = numberOfEggPlants - 1”

In my remove button it only removes one eggplant. If I had 3 eggplants on screen, the button will remove the first one but then if clicked again the second one won’t be removed, do you know why? I don’t want them to all be removed at once, just one at a time.

Here is my remove button code if you need it

 ------------------------------------------------Remove eggplant button local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if event.phase == "ended" and eggplant.alpha ~= 0 then eggplant.alpha = 0 physics.removeBody(eggplant) speed.alpha = 0.65 speed.isSensor = true physics.addBody(speed, "dynamic", {radius = 15, friction = .4, density = 0, bounce = 0}) speed.gravityScale = 0 speed.rotation = 0 speed.linearDamping = 900000000000000000000 speed.angularDamping = 9000000000000000000 speed.isSensor = true speed.isFixedRotation = true numberOfEggPlants = numberOfEggPlants - 1 ---- Decrease the counter by 1 end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local removeeggplant = widget.newButton( { width = 64, height = 70, defaultFile = "images/restartdefault.png", overFile = "images/restartover.png", label = "", onEvent = handleButtonEvent } ) -- Center the button removeeggplant.x = 1025 removeeggplant.y = 40 -- Change the button's label text removeeggplant:setLabel( "" ) sceneGroup:insert(removeeggplant)

If you want three in a succession, I would take all the code and put it into it’s own Spawn function. Then in your button handler, call timer.performWithDelay() to run with 3 iterations, and use the spawn function as the function for the timer to run.

Rob