Chopper Command in Cocos2d-x: Lua

For the Cocos2d-x version of the game, I’ll use their new IDE, called Code. It supports projects using either the JavaScript or the Lua bindings to the framework. And you can use the IDE to create new bindings, something I’ll show in a later tutorial.

So first you need to download the IDE. You will also need to download the Cocos2d-x framework, version 3.0 or above if you haven’t done so already. You’ll find it in the same linked page.

The IDE is Eclipse based, so should be familiar to anyone who has ever used a similar program. When you create a new Lua Project the IDE may ask for the path to the Framework. You will find the same dialogue if you go to Preferences->Cocos and then either JavaScript or Lua. In the dropdown select custom… then find the folder for your downloaded copy of the Cocos2d-x framework.

Go ahead and do it for both Lua and JavaScript, the steps are the same, except that you need to download the Cocos2d-x JS library and use that as the framework library.

(Note: If you want to deploy to Android you will also need to point the IDE to your downloaded copy of the NDK)

Create a new Project and set its target to iPhone Retina. I’m not going to worry too much about resolutions in this tutorial and so the game will run with a weird configuration. The size will be set to 960×640. In essence the game will run as if setup for a screen twice the size of an iPhone 3Gs landscape.

You can double check your configurations by opening the json file config.json. Mine looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 
 
 
{
 
  "init_cfg": {
 
    "isLandscape": true,
 
    "name": "ChopperCommand",
 
    "width": 960,
 
    "height": 640,
 
    "entry": "src/main.lua",
 
    "consolePort": 6010,
 
    "debugPort": 10000,
 
    "forwardConsolePort": 10089,
 
    "forwardUploadPort": 10091
 
  },
 
  "simulator_screen_size": [
 
    {
 
      "title": "iPhone 3Gs (480x320)",
 
      "width": 480,
 
      "height": 320
 
    },
 
    {
 
      "title": "iPhone 4 (960x640)",
 
      "width": 960,
 
      "height": 640
 
    },
 
    {
 
      "title": "iPhone 5 (1136x640)",
 
      "width": 1136,
 
      "height": 640
 
    },
 
    {
 
      "title": "iPad (1024x768)",
 
      "width": 1024,
 
      "height": 768
 
    },
 
    {
 
      "title": "iPad Retina (2048x1536)",
 
      "width": 2048,
 
      "height": 1536
 
    },
 
    {
 
      "title": "Android (800x480)",
 
      "width": 800,
 
      "height": 480
 
    },
 
    {
 
      "title": "Android (854x480)",
 
      "width": 854,
 
      "height": 480
 
    },
 
    {
 
      "title": "Android (1280x720)",
 
      "width": 1280,
 
      "height": 720
 
    },
 
    {
 
      "title": "Android (1920x1080)",
 
      "width": 1920,
 
      "height": 1080
 
    }
 
  ]
 
}

If you run the project now you will see Cocos2d-x 3.0 new hello world application for Lua projects. Go to src/main.lua and either delete or comment out the following line:

1
2
 
gameScene:playBgMusic()

And change the resolution:

1
2
 
cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(960, 640, 0)

Next, delete all resources from the res/ folder. Go ahead and delete the GameScene.lua file from the src/ folder as well.

Add a new lua script to your src folder, and call it ChopperCommandScene.lua

Now replace the scene instantiation code in main.lua with:

1
2
 
local scene = require("ChopperCommandScene")

Don’t run the application yet. It won’t work. There’s nothing in ChopperCommandScene yet, it’s not even a blank scene. We’ll create the base scene now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
 
 
require "Cocos2d"
 
require "Cocos2dConstants"
 
 
 
local ChopperCommandScene = class("ChopperCommandScene",function()
 
    return cc.Scene:create()
 
end)
 
 
 
function ChopperCommandScene.create()
 
    local scene = ChopperCommandScene.new()
 
 
 
    -- sprites
 
    scene._btnFire = nil
 
    scene._btnMove = nil
 
    scene._mountain1 = nil
 
    scene._mountain2 = nil
 
    scene._player = nil
 
 
 
    scene._shootBtnSqrRadius = 0
 
    scene._controlsSqrRadius = 0
 
    scene._touchingControl = false
 
    scene._touchingFire = false
 
    scene._touchControl = nil
 
    scene._touchFire = nil
 
    scene._shooting = false
 
    scene.bullet = nil
 
 
 
    return scene
 
end
 
 
 
function ChopperCommandScene:ctor()
 
    self.visibleSize = cc.Director:getInstance():getVisibleSize()
 
    self.origin = cc.Director:getInstance():getVisibleOrigin()
 
    self.schedulerID = nil
 
end
 
 
 
end
 
 
 
return ChopperCommandScene

Now we have Class extending the Scene class from Cocos2d-x and we also initialised some of the main properties in the game.

It might be helpful to read this: http://lua-users.org/wiki/SimpleLuaClasses

It covers the method for OOP in Lua that Cocos2d-x emulates. Or at least something very similar.

Right now the important thing to remember is that class(ClassName) will call the constructor (ctor) of the class. So:

1
2
3
4
5
6
 
local ChopperCommandScene = class("ChopperCommandScene",function()
 
    return cc.Scene:create()
 
end)

Will call the method ChopperCommandScene:ctor()

Go ahead and mess around with the code so far. (Note: use lua’s print() in order to write stuff to the console. )

We’ll add the first few sprites next.

4 thoughts to “Chopper Command in Cocos2d-x: Lua”

  1. This is nice. Cocos2D-x is indeed really powerful but its lua integration was somehow lacking in 2.0.

    I wonder what’s the status of it for 3.x. Does it have physics and all the C++ API exposed? How difficuly is to integrate ads or gamecenter in it?

    1. The idea for both the lua and javascript bindings was to have as close to 100% of the framework available through the bindings. So far I have not encountered one api which cannot be referenced through either the javascript or lua bindings. Plus, the ide makes is super easy to add your own classes. It’s just a matter of picking which script language you prefer. I find lua a bit cleaner and less hacky when used with OOP concepts.

  2. i wanted to learn Lua game dev using cocos2d-x but i dont see any support from the community. your website is awesome to have such. what should I do if I want to pursue Lua in cocos2dx? right now im using c++ as I can ask in the forums whenever I stumble:)

  3. Great article, I’ve been very impressed with cocos2d-x and lua. Have you figured out how to support retina graphics options automatically in Lua?

Comments are closed.