How to make an inventory? sorta..

Ok so I have a button that spawns and despawns a specific physics body. Is there anyway that I can make this button only spawn a set number of physics body then the button no longer spawns them in? Then if I despawned the an object I’d then be able to spawn it back in again.

Cheers

Just use some sort of counter

local spawnedObj = 0

And every time you spawn an object increment it

spawnedObj = spawnedObj + 1

And decrement when you remove one:

spawnedObj = spawnedObj - 1

Before you spawn a new one check to see if the max number has already been spawned, if not proceed…

Could you elaborate a little bit more please? I am very new.

Using this code as an example maybe?

local physics = require("physics") physics.start() physics.setGravity( 0, 5) local eggplant = display.newImageRect( scene.perRunGroup, "images/eggplant.png", 50, 50, display.contentCenterX, display.contentCenterY, 30) eggplant.x = 70 eggplant.y = 100 physics.addBody(eggplant, "dynamic", {radius = 25, friction = .4, density = 0.0035, bounce = 0.8}) eggplant.gravityScale = 0.5 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) end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local addeggplant = widget.newButton( { width = 80, height = 40, defaultFile = "images/level.png", overFile = "images/level.png", label = "", onEvent = handleButtonEvent } ) -- Center the button addeggplant.x = 150 addeggplant.y = 20 -- Change the button's label text addeggplant:setLabel( "Add" ) sceneGroup:insert(addeggplant) -------------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) end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local removeeggplant = widget.newButton( { width = 80, height = 40, defaultFile = "images/level.png", overFile = "images/level.png", label = "", onEvent = handleButtonEvent } ) -- Center the button removeeggplant.x = 40 removeeggplant.y = 20 -- Change the button's label text removeeggplant:setLabel( "Remove" ) sceneGroup:insert(removeeggplant)

Here I have an eggplant and a remove/add button for the eggplant.

So how would I add a counter to each of the buttons so that the add button will add 3 eggplants max. 

Just use some sort of counter

local spawnedObj = 0

And every time you spawn an object increment it

spawnedObj = spawnedObj + 1

And decrement when you remove one:

spawnedObj = spawnedObj - 1

Before you spawn a new one check to see if the max number has already been spawned, if not proceed…

Could you elaborate a little bit more please? I am very new.

Using this code as an example maybe?

local physics = require("physics") physics.start() physics.setGravity( 0, 5) local eggplant = display.newImageRect( scene.perRunGroup, "images/eggplant.png", 50, 50, display.contentCenterX, display.contentCenterY, 30) eggplant.x = 70 eggplant.y = 100 physics.addBody(eggplant, "dynamic", {radius = 25, friction = .4, density = 0.0035, bounce = 0.8}) eggplant.gravityScale = 0.5 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) end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local addeggplant = widget.newButton( { width = 80, height = 40, defaultFile = "images/level.png", overFile = "images/level.png", label = "", onEvent = handleButtonEvent } ) -- Center the button addeggplant.x = 150 addeggplant.y = 20 -- Change the button's label text addeggplant:setLabel( "Add" ) sceneGroup:insert(addeggplant) -------------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) end if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local removeeggplant = widget.newButton( { width = 80, height = 40, defaultFile = "images/level.png", overFile = "images/level.png", label = "", onEvent = handleButtonEvent } ) -- Center the button removeeggplant.x = 40 removeeggplant.y = 20 -- Change the button's label text removeeggplant:setLabel( "Remove" ) sceneGroup:insert(removeeggplant)

Here I have an eggplant and a remove/add button for the eggplant.

So how would I add a counter to each of the buttons so that the add button will add 3 eggplants max.