1
\$\begingroup\$

I'm writing a game about making pizza in Corona SDK. When you buy chefs, the new chef is supposed to make some pizzas on its own like automation.

I tried everything I knew about, I couldn't think of anything that would do what I'd like so I ask you guys to help me.

Here is the code I think is relevant:

local widget = require("widget")

local pizzeria = display.newImage("background.png")
pizzeria.x=160
pizzeria.y=230
pizzeria:scale(1,1)

local pizzas=0
local chefs = 0
local pizzatext = display.newText( "Pizzas:", 100, 200, display.contentWidth*0.6, display.contentHeight * 0.7, native.systemFont, 16)
pizzatext:setFillColor( 10, 0, 0)

local pizzacount = display.newText(pizzas,165,200,display.contentWidth*0.7,display.contentHeight*0.7,native.systemFont,16) 
pizzacount:setFillColor(10,0,0)

local chefstext = display.newText("chefs:",320,200,display.contentWidth*0.7,display.contentHeight*0.7,native.systemFont,16) 
chefstext:setFillColor(10,0,0)

local chefscount = display.newText(chefs,363,200,display.contentWidth*0.7,display.contentHeight*0.7,native.systemFont,16) 
chefscount:setFillColor(10,0,0)

local function pizzamaker( event )
  if ( "ended" == event.phase ) then
    pizzas=pizzas+1
    pizzacount.text=pizzas
  end
end

local function addchef( event )
  if("ended" == event.phase) then
    if(pizzas>=5) then
      chefs=chefs+1
      chefscount.text=chefs
      pizzas=pizzas-5
      pizzacount.text=pizzas
    end
  end
end

local pizzamaker = widget.newButton
{
  left = 100,
  top = 400,
  defaultFile="button.jpeg",
  overFile = "button.jpeg",
  label = "Make Pizza",
  height = 50,
  width= 100,
  onEvent = pizzamaker
}

local addchef = widget.newButton
{
  left = 100,
  top = 100,
  defaultFile="pizzachef.png",
  overFile="pizzachef.png",
  height = 50,
  width = 50,
  onEvent = addchef
}
\$\endgroup\$
0
\$\begingroup\$

You can use timer.performWithDelay(delay, listener, iterations) so that every x milliseconds the listener gets called for n iterations. Here is the corresponding documentation.

In this case, we would need to delclare a new function which makes pizzas, above the addChef function.

local function makeChefPizza()
    pizzas=pizzas+1
end

Then, we add timer.performWithDelay to your addChef function:

local function addchef( event )
  if("ended" == event.phase) then
    if(pizzas>=5) then
      chefs=chefs+1
      chefscount.text=chefs
      pizzas=pizzas-5
      pizzacount.text=pizzas

      timer.performWithDelay(5000, makeChefPizza, -1) -- This line here
    end
  end
end

This will call the makeChefPizza every 5000 milliseconds (5 seconds) and iterate forever (-1 means forever), see docs.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.