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 – creating our game scene


The GameScene class is already added to the start project and some of the code is already in place. We'll focus first on building the game's interface and listening to touches:

  1. Let's work on the addTouchEvents method:

    function GameScene:addTouchEvents()
        local bg = cc.Sprite:create("background.jpg")
        bg:setPosition(self.middle.x, self.middle.y)
        self:addChild(bg)
       
        local function onTouchBegan(touch, event)
            self.gridController:onTouchDown(touch:getLocation())
            return true
        end
    
        local function onTouchMoved(touch, event)
            self.gridController:onTouchMove(touch:getLocation())
        end
    
        local function onTouchEnded(touch, event)
            self.gridController:onTouchUp(touch:getLocation())
        end
    
        local listener = cc.EventListenerTouchOneByOne:create()
           listener:registerScriptHandler (onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
       listener:registerScriptHandler (onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED...