1
\$\begingroup\$

I have objects on the screen that create the main menu, and I want the the screen to clear when I get press the play button. I did it the way the other question on this subject said, but when I simulate the code the screen doesn't clear when I press play. Can you explain why this is wrong and if it is what I should do instead.

This is the function I am using to clear the screen:

--Clear Screen of All Objects
function clearScreen(numOfObjectsOnScreen)
    for i=1, numOfObjectsOnScreen do
        objectsOnScreen[i]:removeSelf()
        objectsOnScreen[i] = nil
    end
end

I am incrementing the variable that contains the number of Objects on Screen every time I create an object in the main menu.

This is the code for my play button where the clearScreen function is excecuted

local playButton = display.newImage( "Assets\\Art\\GUI\\MainMenu\\PlayButton.png", display.contentCenterX, display.contentHeight * .75)
playButton:scale(2, 2)
insertObjectsOnScreen(playButton)
incrementVariable(mainMenuObjects)

--Play Button Event Handling
function playButton:touch( event )
    if ( event.phase == "began" ) then
        playButton:scale(.9, .9)
    elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
        playButton:scale(10 / 9, 10 / 9)
        clearScreen(mainMenuObjects)
        mainMenuObjects = 0
        gameStatus = "inGame"
        beginGame()
    end
end
playButton:addEventListener( "touch", playButton )

I am pretty sure this is enough for you to be able to answer this question, but if not here is a all the code I have done so far

--Main.Lua
-----------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------
---Variables
-----------------------------------------------------------------------------------------

--All things on screen at one given time
objectsOnScreen = {}

--Scene the Game is on
gameStatus = "mainMenu"

--Number of Objects in Each Scene
mainMenuObjects = 0
inGameObjects = 0
shopObjects = 0

-----------------------------------------------------------------------------------------
---Functions
-----------------------------------------------------------------------------------------

-- Setting up Image Sheet Options
function setImageSheet(w, h, numF)
    local sheetOptions =
    {
        width = w,
        height = h,
        numFrames = numF
    }

    return sheetOptions
end

--Resize Object to Whole Screen
function resizeToFullScren(object)
    local ow = object.contentWidth / display.contentWidth
    local oh = display.contentHeight / object.contentHeight
    object:scale(ow, oh)
end

--Insert Images / Sprites into Objects on Screen
function insertObjectsOnScreen(object)
    table.insert(objectsOnScreen, object)
end

--Clear Screen of All Objects
function clearScreen(numOfObjectsOnScreen)
    for i=1, numOfObjectsOnScreen do
        objectsOnScreen[i]:removeSelf()
        objectsOnScreen[i] = nil
    end
end

--Increment a Variable by 1
function incrementVariable(x)
    x = x + 1
end

----------------------------------------------------------------------------    ------------
---Main Menu Init
----------------------------------------------------------------------------    ------------

--Establishing Background
local background = display.newImage("Assets\\Art\\GUI\\MainMenu\\Background.png", display.contentCenterX, display.contentCenterY)
    resizeToFullScren(background)
    insertObjectsOnScreen(background)
    incrementVariable(mainMenuObjects)


--Establishing Button
local sheet_redButton = graphics.newImageSheet( "Assets\\Art\\Buttons\\redButton_PressedAnimation.png", setImageSheet(500.5, 404, 2))
local sequence_redButton = {
    {
        name = "pressButton",
        start = 1,
        count = 2,
        loopCount = 0,
        loopDirection = "forward"
    }
}
local redButton = display.newSprite( sheet_redButton, sequence_redButton)
redButton.x = display.contentCenterX
redButton.y = display.contentHeight / 5
redButton:scale(1.5, 1.5)
insertObjectsOnScreen(redButton)
incrementVariable(mainMenuObjects)

--Button Event Handling
function redButton:touch( event )
    if ( event.phase == "began" ) then
        redButton:setFrame(2)
    elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
        redButton:setFrame(1)
    end
end
redButton:addEventListener( "touch", redButton )

--Establishing Play Button
local playButton = display.newImage( "Assets\\Art\\GUI\\Main Menu\\PlayButton.png", display.contentCenterX, display.contentHeight * .75)
playButton:scale(2, 2)
insertObjectsOnScreen(playButton)
incrementVariable(mainMenuObjects)

--Play Button Event Handling
function playButton:touch( event )
    if ( event.phase == "began" ) then
        playButton:scale(.9, .9)
    elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
        playButton:scale(10 / 9, 10 / 9)
        clearScreen(mainMenuObjects)
        mainMenuObjects = 0
        gameStatus = "inGame"
        beginGame()
    end
end
playButton:addEventListener( "touch", playButton )

--Establishing Mute Button
mute = true
local sheet_muteButton = graphics.newImageSheet( "Assets\\Art\\GUI\\Main Menu\\MuteButton.png", setImageSheet(232.5, 228, 2))
local sequence_muteButton = {
    {
        name = "pressButton",
        start = 1,
        count = 2,
        loopCount = 0,
        loopDirection = "forward"
    }
}
local muteButton = display.newSprite( sheet_muteButton, sequence_muteButton)
muteButton.x = -display.contentWidth*.8
muteButton.y = display.contentHeight*.85
muteButton:scale(2, 2)
insertObjectsOnScreen(muteButton)
incrementVariable(mainMenuObjects)

--Mute Button Event Handling
function muteButton:touch( event )
    if ( event.phase == "ended" and muteButton.frame == 1) then
        muteButton:setFrame(2)
        mute = true
    elseif ( event.phase == "ended" and muteButton.frame == 2 ) then
        muteButton:setFrame(1)
        mute = false
    end
end
muteButton:addEventListener( "touch", muteButton )

---------------------------------------------------------------------------------------
--Start Game - init
---------------------------------------------------------------------------------------

function beginGame()

end
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I'm not sure this is the actual right way to do it, but for very simple apps I do it like this:

local playButton = display.newImage()
-- all you code
local menuGroup = display.newGroup()
menuGroup:insert( playButton )

function playButton:touch( event )
    if ( event.phase == "began" ) then
        playButton:scale(.9, .9)
    elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
        menuGroup.isVisible = false
        beginGame()
    end
end

I'm pretty sure this would be bad for larger games, because the group is not deleted from memory, but it definitely works for smaller ones and I like it because I can also get back to the menu by just doing:

menuGroup.isVisible = true

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .