Game Development Reference
In-Depth Information
Time for action - changing the grid with
GridController
The GridController object initiates all grid changes since it's where we handle
touches. In the game, the user can drag a gem to swap places with another, or first select
the gem they want to move and then select the gem they want to swap places with in a two-
touch process. Let's add the touch handling for that:
1. In GridController , let's add the logic to onTouchDown :
function GridController:onTouchDown (touch)
if (self.gameLayer.running == false) then
local scene = require("GameScene")
local gameScene = scene.create()
cc.Director:getInstance():replaceScene(gameScene)
local bgMusicPath =
cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath,
true)
return
end
If we are displaying the game over screen, restart the scene.
2. Next, we find the gem the user is trying to select:
self.touchDown = true
if (self.enabled == false) then return end
local touchedGem = self:findGemAtPosition (touch)
if (touchedGem.gem ~= nil ) then
if (self.gameLayer.selectedGem == nil) then
self:selectStartGem(touchedGem)
else
if (self:isValidTarget(touchedGem.x,
touchedGem.y, touch) == true) then
self:selectTargetGem(touchedGem)
else
Search WWH ::




Custom Search