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 a menu scene


Let's create a new file and add a menu scene to our game:

  1. Right-click on the src folder and select New | Lua File; call the new file MenuScene.lua.

  2. Let's create a class that extends a scene. We first load our own module of all the game's constants (this file already exists in the starter project):

    local constants = require ("constants")
  3. Then we build our class:

    local MenuScene = class("MenuScene", function()
        return cc.Scene:create()
    end)
    
    function MenuScene.create()
        local scene = MenuScene.new()
        return scene
    end
    
    function MenuScene:ctor()
        self.visibleSize =  cc.Director:getInstance():getVisibleSize()
        self.middle = {x = self.visibleSize.width * 0.5,  y = self.visibleSize.height * 0.5}
        self.origin = cc.Director:getInstance():getVisibleOrigin()
        self:init()
    end
    return MenuScene

    We'll add the methods next, including the init method we called in the class constructor (always called ctor), but I wanted to stress the importance of returning...