Hi,
I’m trying to learn lua by making a Tic Tac Toe game. In game.lua I have made a grid, but my question is how would I use touch events to put X’s and O’s on the screen. Also how would I code when to put X and when to put O? Here is the code so far:
----------------------------------------------------------------------------------------- -- -- game.lua -- ---------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- local function toMenu(event) if (event.phase == "ended") then composer.gotoScene( "menu", slideRight) end end local screenW = display.contentWidth local screenH = display.contentHeight local halfW = display.contentWidth \* 0.5 --Assign display width and height to constants local \_W = display.contentWidth; local \_H = display.contentHeight; -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. local background = display.newImage(sceneGroup, "images/bg.jpeg") background.x = width \* 0.5 background.y = height \* 0.5 local background1 = display.newRect(sceneGroup, 0, 0, screenW, screenH) background1.anchorX = 0 background1.anchorY = 0 background1:setFillColor( 0.5 ) txt\_menu = display.newText(sceneGroup, "Menu", 0,0,"myfont", 18) txt\_menu.x = 290 txt\_menu.y = 15 txt\_menu:setFillColor( 0.1,0.1,0.1 ) txt\_menu:addEventListener( "touch", toMenu ) txt\_menu = display.newText(sceneGroup, "Player 2 : ", 0,0,"myfont", 18) txt\_menu.x = 170 txt\_menu.y = 15 txt\_menu:setFillColor( 0.1,0.1,0.1 ) txt\_menu = display.newText(sceneGroup, "Player 1 : ", 0,0,"myfont", 18) txt\_menu.x = 45 txt\_menu.y = 15 txt\_menu:setFillColor( 0.1,0.1,0.1 ) local grid = display.newImage(sceneGroup, "images/grid.png") grid.x = display.contentCenterX grid.y =225 grid:scale(0.35,0.35) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene