I’m creating a sandwich stacking game and have found myself at a roadblock. I have a function that I’d like to delete every time I press a particular button, so that the game could fully restart. When ever the function starts, after a seven second delay, burgers items begin to fall from the screen. When you click pause and exit to main menu, I’ve been able to transition to the start screen, but the now paused, falling items won’t terminate in order to be called again. Here’s my code, as well as the zip. something needs to be added in between the Obj32OnPressHandler() in lines 334.
– load sound effect: Fireplace-SoundBible.com-127901833.wav
– play audio on channel 1-32 (select channel 0 for the default):
Obj47 = audio.play( Obj46, { loops = 1000, })
local widget = require “widget”
–number of lives and initial score
lives = 3
score= 0
–Physics Setup
local physics = require “physics”
physics.start()
physics.setGravity( 0.0, 1.2 )
– image “kitchen background.jpeg”
Obj53 = display.newImageRect( “Examples/kitchen_background.jpeg”, 322, 573 )
Obj53.x = 159
Obj53.y = 276
Obj53.id = “Obj53”
Obj53.isVisible = false
– Life textbox
Obj44_options = {
text = " ",
x = 334,
y = 461,
width = 220,
height = 40,
font = “NoiseMachine”,
fontSize = 28,
align = “center”,
}
Obj44 = display.newText( Obj44_options )
Obj44.id = “Obj44”
Obj44.isVisible = false
– Score textbox
Obj48_options = {
text = " ",
x = 109,
y = 502,
width = 53,
height = 40,
font = “NoiseMachine”,
fontSize = 28,
align = “center”,
}
Obj48 = display.newText( Obj48_options )
Obj48.id = “Obj48”
– Pause Background
Obj100 = display.newRoundedRect(158, 686, 213, 313, 16)
Obj100.id = “Obj100”
Obj100:setStrokeColor(0.8, 0.8, 0.0, 1.0)
Obj100:setFillColor(0.0, 0.4, 0.6, 1.0)
Obj100.strokeWidth = 4
Obj100.isVisible = false
–Pause Text
Obj101_options = {
text = “Paused”,
x = 157,
y = 566,
width = 220,
height = 40,
font = “NoiseMachine”,
fontSize = 36,
align = “center”,
}
Obj101 = display.newText( Obj101_options )
Obj101.id = “Obj101”
Obj101.isVisible = false
– New Snippet
objects = {“topbun”, “tomato”, “cheese”, “patty”};
function spawnObject( )
objIdx = math.random(#objects)
objName = objects[objIdx]
object = display.newImageRect(“Examples/images/” … objName … “.png”, 107, 53)
object.x = math.random(30,300)
object.y = screenTop
if objIdx == 1 then
object.id = “topbun”
elseif objIdx == 2 then
object.id = “tomato”
elseif objIdx == 3 then
object.id = “cheese”
else
object.id = “patty”
end
object_image_outline = graphics.newOutline( 14, “Examples/images/cheese.png”, “/” )
physics.addBody(object, “dynamic”, {density = 2, friction = 40, bounce = 0})
function objectCollision(self, event )
if event.phase == “began” then
if event.target.id == “topbun” and event.other.id == “bottomBun” then
print(“You finished”)
print(event.target.id)
elseif event.target.id == “topbun” and event.other.id == “tomato” then
print(“You finished”)
print(event.target.id)
elseif event.target.id == “topbun” and event.other.id == “patty” then
print(“You finished”)
print(event.target.id)
elseif event.target.id == “topbun” and event.other.id == “cheese” then
print(“You finished”)
print(event.target.id)
elseif event.target.id == “patty” and event.other.id == “bottomBun” then
print(“One Patty”)
print(event.target.id)
elseif event.target.id == “patty” and event.other.id == “patty” then
print(“Double Patty”)
print(event.target.id)
elseif event.target.id == “patty” and event.other.id == “cheese” then
print(“Patty and cheese”)
print(event.target.id)
elseif event.target.id == “patty” and event.other.id == “tomato” then
print(“Patty and Tomato”)
print(event.target.id)
elseif event.target.id == “tomato” and event.other.id == “tomato” then
print(“Double Tomato”)
print(event.target.id)
elseif event.target.id == “tomato” and event.other.id == “bottomBun” then
print(“You must be hungry”)
print(event.target.id)
elseif event.target.id == “tomato” and event.other.id == “patty” then
print(“Patty and Tomato”)
print(event.target.id)
elseif event.target.id == “tomato” and event.other.id == “cheese” then
print(“Cheese and Tomato”)
print(event.target.id)
elseif event.target.id == “cheese” and event.other.id == “cheese” then
print(“Double Cheese”)
print(event.target.id)
elseif event.target.id == “cheese” and event.other.id == “patty” then
print(“Cheese and Patty”)
print(event.target.id)
elseif event.target.id == “cheese” and event.other.id == “tomato” then
print(“Cheese and tomato”)
print(event.target.id)
elseif event.target.id == “cheese” and event.other.id == “bottomBun” then
print(“Just Cheese”)
print(event.target.id)
end
end
end
object.collision = objectCollision
object:addEventListener( “collision”, object )
end
– fn to handle touches on Obj3
– https://docs.coronalabs.com/api/event/touch/index.html
– note: this fn must be positioned in the stack above Obj3
function Obj3TouchHandler( event )
– Obj5: default touch/drag support
– smart drag for Obj3:
t = event.target
if event.phase == “began” then
if t.id ~= nil then print("started dragging "…t.id); end
if t.name ~= nil then print("started dragging "…t.name); end
display.getCurrentStage():setFocus( t )
t.isFocus = true
– drag starts at:
t.x0 = event.x - t.x
elseif t.isFocus then
if event.phase == “moved” then
t.x = event.x - t.x0
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
if t.id ~= nil then print("stopped dragging "…t.id); end
if t.name ~= nil then print("stopped dragging "…t.name); end
end
end
– touch has been handled (optional final statement):
– return true
end
– fn to handle collisions on Obj3
– https://docs.coronalabs.com/api/event/collision/index.html
– note: this fn must be positioned in the stack above Obj3
function Obj3CollisionHandler( event )
– Obj25: default collision support
– information about the collision:
if event.phase == “began” then
if event.other ~= nil then
if event.other.name ~= nil then
print("Obj3 colliding with "…event.other.name )
elseif event.other.id ~= nil then
print("Obj3 colliding with "…event.other.id )
end
end
end
end
– image “images/bottombun.png”
Obj3 = display.newImageRect( “Examples/images/bottombun.png”, 107, 53 )
Obj3.x = 160
Obj3.y = 403
Obj3.id = “Obj3”
Obj3:addEventListener( “collision”, Obj3CollisionHandler )
Obj3:addEventListener( “touch”, Obj3TouchHandler )
physics.addBody( Obj3, “static”, { bounce = 0.0, friction = 4.0, density = 1.0 })
– image “images.png”
Obj26 = display.newImageRect( “Examples/burgerbackground.png”, 360, 533 )
Obj26.x = 163
Obj26.y = 240
Obj26.id = “Obj26”
– fn to handle button press on Obj30
– https://docs.coronalabs.com/api/library/widget/newButton.html
– note: this fn must be positioned in the stack above Obj30
function Obj102OnPressHandler( event )
– Obj40: default button event support
– put code here to respond to presses on this button
print(“You pressed the button Obj102”)
physics.pause()
timer.pause(Obj71)
Obj103 = transition.to(pausePanel, {time=1000, y=-405})
pausePanel:toFront()
end
– remember to move button image resources into your project folder!
Obj102 = widget.newButton({
x = 299,
y = 498,
width = 40,
height = 40,
id = “Obj102”,
label = “”,
labelAlign = “center”,
font = “Helvetica Neue”,
fontSize = 22,
labelColor = { default = {0.25, 0.54, 0.87, 1.0}, over = {0.17, 0.37, 0.61, 1.0} },
defaultFile = “Daniel/pauseButton.png”,
overFile = “Daniel/img_40517.png”,
onPress = Obj102OnPressHandler,
})
Obj102.isVisible= false
–Return to game button
function Obj29OnPressHandler( event )
– Obj31: default button event support
– put code here to respond to presses on this button
print(“You pressed the button Obj29”)
physics.start(spawnObject)
timer.resume(Obj71)
Obj42 = transition.to(pausePanel, {time=1000, y=409})
end
–Return to Game text
Obj29 = widget.newButton({
x = 160,
y = 738,
width = 193,
height = 21,
id = “Obj29”,
label = “Return to Game”,
font = “NoiseMachine”,
fontSize = 26,
strokeColor = { default = {1.0, 1.0, 1.0, 1.0}, over = {0.7, 0.7, 0.7, 1.0} },
onPress = Obj29OnPressHandler,
})
–Exit to Main Menu Button
function Obj32OnPressHandler( event )
print(“You pressed the button Obj32”)
Obj107 = transition.to(pausePanel, {time=1000, y=405})
Obj26.isVisible = true
Obj30.isVisible = true
Obj33.isVisible = true
Obj37.isVisible = false
Obj3.isVisible = false
Obj53.isVisible = false
Obj100.isVisible = false
Obj101.isVisible = false
Obj102.isVisible = false
end
–Exit to main menu
Obj32 = widget.newButton({
x = 158,
y = 793,
width = 154,
height = 20,
id = “Obj32”,
label = “Exit to Menu”,
font = “NoiseMachine”,
fontSize = 26,
strokeColor = { default = {1.0, 1.0, 1.0, 1.0}, over = {0.7, 0.7, 0.7, 1.0} },
onPress = Obj32OnPressHandler,
})
– image “pauseBurger.png”
Obj35 = display.newImageRect( “Daniel/pauseBurger.png”, 133, 120 )
Obj35.x = 157
Obj35.y = 641
Obj35.id = “Obj35”
–Lives Text
Obj43_options = {
text = “Lives:”,
x = 180,
y = 461,
width = 220,
height = 40,
font = “NoiseMachine”,
fontSize = 28,
align = “center”,
}
Obj43 = display.newText( Obj43_options )
Obj43.id = “Obj43”
function Obj30OnPressHandler( event )
–
print(“You pressed the button Obj30”)
Obj26.isVisible = false
Obj30.isVisible = false
Obj33.isVisible = false
Obj36.isVisible = false
Obj37.isVisible = false
Obj3.isVisible = true
Obj53.isVisible = true
Obj100.isVisible = true
Obj101.isVisible = true
Obj102.isVisible = true
Obj71 = timer.performWithDelay( 7000, spawnObject, 6 )
end
Obj30 = widget.newButton({
x = 160,
y = 240,
width = 200,
height = 40,
id = “Obj30”,
label = “Start Game”,
labelAlign = “center”,
labelColor = { default = {0.25, 0.54, 0.87, 1.0}, over = {0.17, 0.37, 0.61, 1.0} },
font = “Helvetica Neue”,
fontSize = 22,
strokeWidth = 4,
strokeColor = { default = {0.25, 0.54, 0.87, 1.0}, over = {0.17, 0.37, 0.61, 1.0} },
fillColor = { default = {1.0, 1.0, 1.0, 1.0}, over = {0.7, 0.7, 0.7, 1.0} },
shape = “roundedRect”,
cornerRadius = 6,
onPress = Obj30OnPressHandler,
})
– fn to handle button press on Obj33
– https://docs.coronalabs.com/api/library/widget/newButton.html
– note: this fn must be positioned in the stack above Obj33
function Obj33OnPressHandler( event )
– Obj35: default button event support
– put code here to respond to presses on this button
print(“You pressed the button Obj33”)
Obj36.isVisible = true
Obj37.isVisible = true
Obj38.isVisible = true
Obj41.isVisible = true
Obj3.isVisible = false
Obj53.isVisible = false
end
Obj33 = widget.newButton({
x = 158,
y = 309,
width = 200,
height = 40,
id = “Obj33”,
label = “How to Play”,
labelAlign = “center”,
labelColor = { default = {0.25, 0.54, 0.87, 1.0}, over = {0.17, 0.37, 0.61, 1.0} },
font = “Helvetica Neue”,
fontSize = 22,
strokeWidth = 4,
strokeColor = { default = {0.25, 0.54, 0.87, 1.0}, over = {0.17, 0.37, 0.61, 1.0} },
fillColor = { default = {1.0, 1.0, 1.0, 1.0}, over = {0.7, 0.7, 0.7, 1.0} },
shape = “roundedRect”,
cornerRadius = 6,
onPress = Obj33OnPressHandler,
})
Obj36 = display.newRect(167, 232, 253, 260)
Obj36.id = “Obj36”
Obj36:setFillColor(1.0, 0.0, 0.2, 1.0)
Obj36.isVisible = false
Obj37_options = {
text = “Move the bun to catch the correct toppings. If you collect a bad item you will lose a life.”,
x = 167,
y = 231,
width = 220,
height = 220,
font = “KannadaMN-Bold”,
fontSize = 26,
align = “center”,
}
Obj37 = display.newText( Obj37_options )
Obj37.id = “Obj37”
Obj37.isVisible = false
– fn to handle button press on Obj38
– https://docs.coronalabs.com/api/library/widget/newButton.html
– note: this fn must be positioned in the stack above Obj38
function Obj38OnPressHandler( event )
– Obj40: default button event support
– put code here to respond to presses on this button
print(“You pressed the button Obj38”)
Obj36.isVisible = false
Obj37.isVisible = false
Obj38.isVisible =false
Obj41.isVisible = false
Obj3.isVisible = false
Obj53.isVisible = false
end
Obj38 = widget.newButton({
x = 96,
y = 407,
width = 120,
height = 40,
id = “Obj38”,
label = “Back”,
labelAlign = “center”,
labelColor = { default = {1.0, 0.2, 0.0, 1.0}, over = {0.7, 0.14, 0.0, 1.0} },
font = “Helvetica Neue”,
fontSize = 22,
strokeWidth = 4,
strokeColor = { default = {1.0, 0.2, 0.0, 1.0}, over = {0.7, 0.14, 0.0, 1.0} },
fillColor = { default = {1.0, 1.0, 1.0, 1.0}, over = {0.7, 0.7, 0.7, 1.0} },
shape = “roundedRect”,
cornerRadius = 6,
onPress = Obj38OnPressHandler,
})
Obj38.isVisible = false
– fn to handle button press on Obj41
– https://docs.coronalabs.com/api/library/widget/newButton.html
– note: this fn must be positioned in the stack above Obj41
function Obj41OnPressHandler( event )
– Obj43: default button event support
– put code here to respond to presses on this button
print(“You pressed the button Obj41”)
Obj26.isVisible = false
Obj30.isVisible = false
Obj33.isVisible = false
Obj36.isVisible = false
Obj37.isVisible = false
Obj3.isVisible = true
Obj53.isVisible = true
Obj100.isVisible = true
Obj101.isVisible = true
Obj102.isVisible = true
Obj33.isVisible = false
Obj41.isVisible = false
Obj38.isVisible= false
Obj71 = timer.performWithDelay( 7000, spawnObject, 6 )
end
Obj41 = widget.newButton({
x = 224,
y = 407,
width = 113,
height = 40,
id = “Obj41”,
label = “Play”,
labelAlign = “center”,
labelColor = { default = {1.0, 0.0, 0.0, 1.0}, over = {0.7, 0.0, 0.0, 1.0} },
font = “Helvetica Neue”,
fontSize = 22,
strokeWidth = 4,
strokeColor = { default = {1.0, 0.0, 0.0, 1.0}, over = {0.7, 0.0, 0.0, 1.0} },
fillColor = { default = {1.0, 1.0, 1.0, 1.0}, over = {0.7, 0.7, 0.7, 1.0} },
shape = “roundedRect”,
cornerRadius = 6,
onPress = Obj41OnPressHandler,
})
Obj41.isVisible = false
–Pause Panel Display Group
pausePanel = display.newGroup()
pausePanel:insert(Obj100)
pausePanel:insert(Obj101)
pausePanel:insert(Obj29)
pausePanel:insert(Obj32)
pausePanel:insert(Obj35)
pausePanel.alpha = 1.0
pausePanel.avgX = 158
pausePanel.avgY = 684
pausePanel.id = “pausePanel”