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