tech/game

21/07/13 TicTacToe - BUTTONS : Touch Events

tech-lover 2021. 7. 14. 01:26

https://youtu.be/V-CAZKzZ0S0

https://docs.coronalabs.com/api/event/touch/index.html

 

Solar2D Documentation — API Reference | Events | touch

Overview When the user's finger touches the screen, a hit event is generated and dispatched to display objects in the display hierarchy. Only those objects that intersect the hit location (the location of the finger on the screen) will be candidates for re

docs.coronalabs.com

 

- overlay.lua

 

```

....

 

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)

        print("overlay scene: show-will")

    elseif ( phase == "did" ) then

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

        print("overlay scene: show-did")

 

        local function onBtnPlayTouch(event)

            print("onBtnPlayTouch")

 

            if (event.phase == "began"then

                print( "Touch event began on: " .. event.target.id )

 

                -- set touch focus

                display.getCurrentStage():setFocus( event.target )

 

                btnPlay.xScale = 0.95

                btnPlay.yScale = 0.95

 

            elseif ( event.phase == "ended" ) then

 

                print( "Touch event ended on: " .. event.target.id )

 

                btnPlay.xScale = 1

                btnPlay.yScale = 1

 

                -- unset touch focus

                display.getCurrentStage():setFocus( nil )

 

                composer.gotoScene( "scenes-tictactoe.cutscene", { time=1000, effect="crossFade" })

 

            end

 

            return true

        

        end

        btnPlay:addEventListener("touch", onBtnPlayTouch)

 

        local function onBtnReviewTouch(event)

            print("onBtnReviewTouch")

 

            if ( event.phase == "began" ) then

                print( "Touch event began on: " .. event.target.id )

 

                -- set touch focus

                display.getCurrentStage():setFocus( event.target )

 

                btnReview.xScale = 0.9

                btnReview.yScale = 0.9

 

                overlayBg.isVisible = false

                btnPlay.isVisible = false

 

            elseif ( event.phase == "ended" ) then

                print( "Touch event ended on: " .. event.target.id )

 

                overlayBg.isVisible = true

                btnPlay.isVisible = true

 

                btnReview.xScale = 1

                btnReview.yScale = 1

 

                -- unset touch focus

                display.getCurrentStage():setFocus( nil )

 

            end

            return true

 

        end

        btnReview:addEventListener("touch", onBtnReviewTouch)            



    end

end

 

 

...

 

```