tech/game

21/07/13 TicTacToe - LUA GROUPS: Learn how to use groups to act as layers to display you app content and organise objects

tech-lover 2021. 7. 13. 13:41

https://youtu.be/lFb28H0fbR0

 

```

local composer = require( "composer" )

local scene = composer.newScene()

 

-- -----------------------------------------------------------------------------------

-- Code outside of the scene event functions below will only be executed ONCE unless

-- the scene is removed entirely (not recycled) via "composer.removeScene()"

-- -----------------------------------------------------------------------------------

 

local grpBackground

local grpMain

local grpUI

 

local grpBoardBG

local grpBoardFG



local backgroundImage

local gridSqure

 

local optionsNotes = 

{

    text = "O",

    x = display.contentCenterX,

    y = display.contentCenterY,

    width = 200,

    font = native.systemFont,

    fontSize = 100,

    align = "center"  -- Alignment parameter

}

 

local optionsCrosses = 

{

    text = "X",

    x = display.contentCenterX,

    y = display.contentCenterY,

    width = 200,

    font = native.systemFont,

    fontSize = 100,

    align = "center"

}

 

-- -----------------------------------------------------------------------------------

-- Scene event functions

-- -----------------------------------------------------------------------------------

 

-- create()

function scene:create( event )

 

    local sceneGroup = self.view

    -- Code here runs when the scene is first created but has not yet appeared on screen

 

    grpBackground = display.newGroup()  

    grpMain = display.newGroup()   

    grpUI = display.newGroup() 

 

    sceneGroup:insert(grpBackground)

    sceneGroup:insert(grpMain)

    sceneGroup:insert(grpUI)

 

    grpBoardBG = display.newGroup() 

    grpBoardFG = display.newGroup() 

    grpMain:insert(grpBoardBG)

    grpBoardBG:insert(grpBoardFG)

 

    grpBoardFG:toBack()

    grpBoardFG:toFront()

 

    backgroundImage = display.newImageRect(grpBackground, "images-tictactoe/background-orange.jpg"7681024)

    backgroundImage.x = display.contentCenterX

    backgroundImage.y = display.contentCenterY





end

 

 

-- show()

function scene:show( event )

 

    local sceneGroup = self.view

    local phase = event.phase

 

    if ( phase == "will" ) then

        -- Code here runs when the scene is still off screen (but is about to come on screen)

         

        -- Starting box coordinates (top left box 1)

        local xPos = display.contentCenterX - 100

        local yPos = display.contentCenterY - 100

 

        local function drawSquare(xPosIn, yPosIn)

            print("sqaure")

            gridSquare = display.newRect(grpBoardBG, xPosIn, yPosIn, 100100)

            gridSquare.strokeWidth = 3

            gridSquare:setStrokeColor( 000)

            gridSquare.isVisible = true

            gridSquare:toBack() 

 

        end



        local function drawRow(xPosIn, yPosIn) 

            print("drawRow()")

 

            local numCols = 3

            local xPos = xPosIn

            local yPos = yPosIn

 

            for count=1, numCols, 1 do

                print("for loop")

 

                drawSquare(xPos, yPos)

                xPos = xPos + 100



            end

 

        end

 

        local function drawGrid(xPosIn, yPosIn)

            print("drawGrid()")

 

            local numRows = 3

            local xPos = xPosIn

            local yPos = yPosIn

 

            for count=1, numRows, 1 do

            

                print("for loop rows")

                drawRow(xPos, yPos)

                yPos = yPos + 100

 

            end

 

        end

 

        drawGrid(xPos, yPos)

 

        grpBoardBG.y = grpBoardBG.y + 100

 

    elseif ( phase == "did" ) then

        -- Code here runs when the scene is entirely on screen

 

        local charNotes = display.newText( optionsNotes )

        grpBoardFG:insert(charNotes)

        charNotes:setFillColor( 100 )

        charNotes.isVisible = true

    

        local charCrosses = display.newText( optionsCrosses )

        grpBoardFG:insert(charCrosses)

        charCrosses:setFillColor( 001 )

        charCrosses.isVisible = true        

 

    end

end

 

 

-- hide()

function scene:hide( event )

 

    local sceneGroup = self.view

    local phase = event.phase

 

    if ( phase == "will" ) then

        -- Code here runs when the scene is on screen (but is about to go off screen)

 

    elseif ( phase == "did" ) then

        -- Code here runs immediately after the scene goes entirely off screen

 

    end

end

 

 

-- destroy()

function scene:destroy( event )

 

    local sceneGroup = self.view

    -- Code here runs prior to the removal of scene's view

 

end

 

 

-- -----------------------------------------------------------------------------------

-- Scene event function listeners

-- -----------------------------------------------------------------------------------

scene:addEventListener( "create", scene )

scene:addEventListener( "show", scene )

scene:addEventListener( "hide", scene )

scene:addEventListener( "destroy", scene )

-- -----------------------------------------------------------------------------------

 

return scene

```