Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...