Chopper Command: Updating to 3.3

I finally had the chance to go back and finish the Cocos2d-x version of Chopper Command. But first I decided to update the framework. I started with version 3.2 and now it’s already version 3.4. However, I will not use version 3.4 as there are too many issues with the new IDE and the new Framework when working with Lua. Problems creating projects, and publishing to devices.

So I updated the project to version 3.3, which to my mind is still the best Cocos2d-x version out there. Hopefully there will be patches soon for all the weirdness of version 3.4

I refactored the code a bit, and decided to make GameSprite actually extend Sprite.

So the new game scene 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
 
local GameConstants = require('GameConstants')
 
 
 
local ChopperCommandScene = class("ChopperCommandScene",function()
 
    return cc.Scene:create()
 
end)
 
 
 
function ChopperCommandScene.create()
 
 
 
    local scene = ChopperCommandScene.new()
 
    scene:init()
 
    return scene
 
end
 
 
 
function ChopperCommandScene:init ()
 
    self.running = true
 
    self.btnFire = nil
 
    self.btnMove = nil
 
    self.mountain1 = nil
 
    self.mountain2 = nil
 
    self.bg = nil
 
    self.player = nil
 
    self.screens = {}
 
    self.radarScreens = {}
 
    self.radar = {}
 
    self.numScreens = 5
 
    self.speed = -0.5
 
    self.screenToRadarRatio = 0
 
    self.bulletsPool = {}
 
    self.bulletsPoolIndex = 1
 
    self.shooting = false
 
    self.timeBetweenBullets = 0
 
    self.intervalBetweenBullets = 8
 
    self.shootBtnSqrRadius = 0
 
    self.controlsSqrRadius = 0
 
    self.touchingControl = false
 
    self.touchingFire = false
 
    self.touchControl = nil
 
    self.touchFire = nil
 
    self.shooting = false
 
    self.enemySpeed = 3
 
    self.state = 0
 
    self.level = 1
 
    self.lives = 3
 
    self.playerScore = 0
 
    self:addMountains()
 
    self:addPlayer()
 
    self:addControls()
 
end
 
 
 
function ChopperCommandScene:update()
 
 
 
    if (self.running == false) then return end
 
    self:updateTouch()
 
    self.player:update()
 
 
 
    self:scrollBG()
 
 
 
end
 
 
 
function ChopperCommandScene:updateTouch()
 
 
 
    if self.touchingFire and self.touchFire ~= nil then
 
 
 
        local fx, fy = self.btnFire:getPosition()
 
        if math.pow (self.touchFire.x - fx, 2) + math.pow (self.touchFire.y - fy, 2) < self.shootBtnSqrRadius then
 
            self.shooting = true
 
        else
 
            self.shooting = false
 
        end
 
    end
 
 
 
    if self.touchingControl and self.touchControl ~= nil then
 
 
 
        local cx, cy = self.btnMove:getPosition()
 
 
 
        if math.pow (self.touchControl.x - cx, 2) + math.pow (self.touchControl.y - cy, 2) < self.controlsSqrRadius then
 
 
 
            local diffx = self.touchControl.x - cx
 
            local diffy = self.touchControl.y - cy
 
 
 
            local rad = math.atan2 (diffy, diffx)
 
 
 
            local angle = math.deg (rad)
 
 
 
            if angle < 360 then angle = angle + 360 end
 
            if angle > 360 then angle = angle - 360 end
 
 
 
            if angle > 315 or angle < 45 then
 
                self.player.moveRight = true
 
                self.player.moveLeft = false
 
            end 
 
 
 
 
 
            if angle >= 45 and angle <= 135 then
 
                self.player.moveDown = true
 
                self. player.moveUp = false
 
            else
 
                self.player.moveDown = false
 
            end
 
 
 
            if angle > 135 and angle < 225 then
 
                self.player.moveLeft = true
 
                self.player.moveRight = false
 
            end 
 
 
 
            if angle > 215 and angle < 322 then
 
                self.player.moveUp = true
 
                self.player.moveDown = false
 
            else
 
                self.player.moveUp = false
 
            end
 
 
 
        else
 
            self.player.moveLeft = false
 
            self.player.moveDown = false
 
            self.player.moveUp = false
 
            self.player.moveRight = false
 
        end
 
    end
 
end
 
 
 
 
 
function ChopperCommandScene:scrollBG ()
 
 
 
    self.mountains2:setPositionX (  self.mountains2:getPositionX() + self.player.screenScroll * 0.5)
 
    self.mountains1:setPositionX (  self.mountains1:getPositionX() + self.player.screenScroll * 0.25)
 
 
 
    if self.player.screenScroll < 0 then
 
        if self.mountains2:getPositionX() <  -self.visibleSize.width then
 
            self.mountains2:setPositionX( self.mountains2:getPositionX() + self.visibleSize.width - ((self.mountains2:getPositionX() + self.visibleSize.width))) 
 
        end
 
 
 
        if self.mountains1:getPositionX() <  -self.visibleSize.width then
 
            self.mountains1:setPositionX( self.mountains1:getPositionX() + self.visibleSize.width - ((self.mountains1:getPositionX() + self.visibleSize.width)))
 
        end
 
 
 
    elseif self.player.screenScroll > 0 then
 
        if self.mountains2:getPositionX() > -100 then
 
            self.mountains2:setPositionX( self.mountains2:getPositionX() - self.visibleSize.width + ((self.mountains2:getPositionX() + 100)))
 
        end
 
 
 
        if self.mountains1:getPositionX() > -100 then
 
            self.mountains1:setPositionX( self.mountains1:getPositionX() - self.visibleSize.width + ((self.mountains1:getPositionX() + 100)))
 
        end
 
 
 
    end
 
end
 
 
 
 
 
function ChopperCommandScene:ctor()
 
    self.visibleSize = cc.Director:getInstance():getVisibleSize()
 
    self.origin = cc.Director:getInstance():getVisibleOrigin()
 
    self.schedulerID = nil
 
end
 
 
 
 
 
function ChopperCommandScene:addMountains ()
 
 
 
    self.bg = cc.Sprite:create("game_bg.png")
 
    self.bg:setPosition(self.origin.x + self.visibleSize.width / 2, self.origin.y + self.visibleSize.height / 2)
 
    self:addChild(self.bg)
 
 
 
    local function tick ()
 
        self:update()
 
    end
 
 
 
    self.schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
 
 
 
    local function onNodeEvent(event)
 
        if "exit" == event then
 
            cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.schedulerID)
 
        end
 
    end
 
 
 
    self.bg:registerScriptHandler(onNodeEvent)
 
 
 
    local mountain = nil
 
 
 
    self.mountains2 = cc.Layer:create()
 
    self.mountains2:addChild ( cc.Sprite:create("mountain2.png") )
 
    self:addChild(self.mountains2)
 
 
 
    mountain = cc.Sprite:create("mountain2.png")
 
    mountain:setPositionX(mountain:getContentSize().width)
 
    self.mountains2:addChild ( mountain )
 
 
 
    mountain = cc.Sprite:create("mountain2.png")
 
    mountain:setPositionX ( 2 * mountain:getContentSize().width )
 
    self.mountains2:addChild ( mountain )
 
    self.mountains2:setPositionY ( self.visibleSize.height * 0.822 )
 
 
 
    self.mountains1 = cc.Layer:create()
 
    self.mountains1:addChild ( cc.Sprite:create("mountain1.png") )
 
    self:addChild(self.mountains1)
 
 
 
    mountain = cc.Sprite:create("mountain1.png")
 
    mountain:setPositionX(mountain:getContentSize().width)
 
    self.mountains1:addChild ( mountain )
 
 
 
    mountain = cc.Sprite:create("mountain1.png")
 
    mountain:setPositionX ( 2 * mountain:getContentSize().width )
 
    self.mountains1:addChild ( mountain )
 
    self.mountains1:setPositionY ( self.visibleSize.height * 0.81 )
 
 
 
    self.mountains1:setPositionX(-500)
 
    self.mountains2:setPositionX(-500)
 
 
 
end
 
 
 
function ChopperCommandScene:addPlayer ()
 
 
 
    -- create player animation
 
    local texture1 = cc.Director:getInstance():getTextureCache():addImage("player1.png")
 
 
 
    local rect  = cc.rect(0, 0, texture1:getContentSize().width, texture1:getContentSize().height)
 
    local playerboundaries = cc.rect(self.visibleSize.width * 0.1, self.visibleSize.height * 0.22 ,self.visibleSize.width * 0.8, self.visibleSize.height * 0.54)
 
 
 
    self.player = require("Player").create (  playerboundaries, self )
 
    self.player:setNewPosition(self.visibleSize.width * 0.5, self.visibleSize.height * 0.5)
 
    self:addChild(self.player)
 
end
 
 
 
function ChopperCommandScene:addControls ()
 
 
 
    self.btnFire = cc.Sprite:create("btn_fire.png")
 
    self.btnMove = cc.Sprite:create("btn_move.png")
 
 
 
    self.btnFire:setOpacity (50)
 
    self.btnMove:setOpacity (50)
 
 
 
    self.btnFire:setPosition (self.visibleSize.width * 0.1, self.origin.y + self.visibleSize.height * 0.25)
 
    self.btnMove:setPosition (self.visibleSize.width * 0.85, self.origin.y + self.visibleSize.height * 0.25)
 
 
 
    self.shootBtnSqrRadius = math.pow ( self.btnFire:getContentSize().width * 0.5, 2 )
 
    self.controlsSqrRadius = math.pow ( self.btnMove:getContentSize().width * 0.5, 2 )
 
 
 
    self:addChild ( self.btnFire )
 
    self:addChild ( self.btnMove )
 
 
 
    -- touch events
 
    local function onTouchBegan(touch, event)
 
 
 
        local target = event:getCurrentTarget()
 
 
 
        if target == self.btnFire then
 
            self.touchingFire = true
 
            self.touchFire = touch:getLocation()
 
        elseif target == self.btnMove then
 
            self.touchingControl = true
 
            self.touchControl = touch:getLocation()
 
        end
 
 
 
        return true
 
    end
 
 
 
    local function onTouchMoved (touch, event)
 
 
 
        local target = event:getCurrentTarget()
 
 
 
        if target == self.btnMove then
 
            self.touchingControl = true
 
            self.touchControl = touch:getLocation()
 
        end
 
 
 
    end
 
 
 
    local function onTouchEnded(touch, event)
 
        local target = event:getCurrentTarget()
 
 
 
        if target == self.btnFire then
 
            self.touchingFire = false
 
            self.touchFire = nil
 
            self.shooting = false
 
        elseif target == self.btnMove then
 
            self.touchingControl = false
 
            self.touchControl = nil
 
            self.player.moveLeft = false
 
            self.player.moveDown = false
 
            self.player.moveUp = false
 
            self.player.moveRight = false
 
        end
 
 
 
    end
 
 
 
    local listener = cc.EventListenerTouchOneByOne:create()
 
 
 
    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
 
    listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
 
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
 
 
 
    local eventDispatcher = self.bg:getEventDispatcher()
 
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self.btnMove)
 
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener:clone(), self.btnFire)
 
 
 
end
 
 
 
return ChopperCommandScene

The class has all the properties in place for the final game, it’s just waiting for the remaining logic to be added to it.

And the new Player class 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
 
local GameConstants = require('GameConstants')
 
 
 
local Player = class("Player", function()
 
    return require("GameSprite"):create()
 
end)
 
 
 
function Player.create ( boundaries, game )
 
 
 
    local player = Player.new ()
 
    player.game = game    
 
    player.boundaries = boundaries
 
    player.minX = boundaries.x
 
    player.maxX = boundaries.x + boundaries.width
 
    player.minY = boundaries.y
 
    player.maxY = boundaries.y + boundaries.height
 
    player.range = boundaries.width * 0.35
 
    player:reset (GameConstants.RESET_HARD)
 
    player.radarSkin = cc.Sprite:create("radar_player.png")
 
    return player
 
end
 
 
 
 
 
function Player:ctor(  )
 
 
 
    self.boundaries = nil
 
    self.screenScrollTarget = 0.0
 
    self.moveLeft = false
 
    self.moveRight = false
 
    self.moveUp = false
 
    self.moveDown = false
 
 
 
    self.vector = cc.p (0.0, 0.0)
 
 
 
    self.minX = 0.0
 
    self.maxX = 0.0
 
    self.minY = 0.0
 
    self.maxY = 0.0
 
    self.range = 0.0
 
 
 
    self.acceleration = cc.p (0.5, 4.0)
 
    self.maxSpeed = 20.0
 
 
 
    self.transition = 0.0
 
    self.screenScrollNow = 0.0
 
    self.screenScroll = 0.0
 
 
 
    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.tile = nil
 
    self.radarSkin = nil
 
    self.nextPosition = cc.p(0,0)
 
    self.boundaries = nil
 
 
 
    local animation = cc.Animation:create()
 
    animation:addSpriteFrameWithFile("player1.png")
 
    animation:addSpriteFrameWithFile("player2.png")
 
    animation:setDelayPerUnit(0.2 / 2.0)
 
    animation:setRestoreOriginalFrame(true)
 
    animation:setLoops(-1)
 
 
 
    self.skin = cc.Sprite:create('player1.png')
 
    self:addChild(self.skin)
 
 
 
    local animate = cc.Animate:create(animation)
 
    self.skin:runAction( animate )
 
 
 
    self.squaredRadius = 50 * 50
 
    self.halfWidth = 50
 
    self.halfHeight = 16
 
 
 
 
 
end
 
 
 
 
 
function Player:reset ( type )
 
 
 
    self.active = true
 
 
 
    self:setScaleX (1.0)
 
    self:setPosition ( self.minX, self.minY + (self.maxY - self.minY) * 0.5 )
 
    self.nextPosition = cc.p ( self.minX,  self:getPositionY() )
 
 
 
end
 
 
 
function Player:update ()
 
 
 
    if self:isVisible() == false then
 
        return 
 
    end 
 
 
 
    if self.active == false then
 
        return
 
    end
 
 
 
    --handle up and down movement (easy peasy)
 
    self.vector.y = 0
 
 
 
    if self.moveUp == true then
 
        self.vector.y = - self.acceleration.y
 
    elseif self.moveDown == true then
 
        self.vector.y = self.acceleration.y
 
    end
 
 
 
    self.nextPosition.y = self.nextPosition.y + self.vector.y
 
 
 
    if self.nextPosition.y < self.minY then
 
        self.nextPosition.y = self.minY
 
    end
 
 
 
    if self.nextPosition.y > self.maxY then
 
        self.nextPosition.y = self.maxY
 
    end
 
 
 
 
 
    --handle flip
 
    if self:getScaleX() > 0 and self.moveLeft == true then
 
        self:setScaleX(-1)
 
    end
 
 
 
    if self:getScaleX() < 0 and self.moveRight == true then
 
        self:setScaleX(1)
 
    end
 
 
 
 
 
    --if player is inside update region
 
    if  (self:getScaleX() > 0 and self:getPositionX() <= self.minX + self.range) 
 
        or (self:getScaleX() < 0 and self:getPositionX() >= self.maxX - self.range ) then
 
        if self.moveRight == true or self.moveLeft == true then
 
            self.vector.x =  self.vector.x + self.acceleration.x
 
        else
 
            self.vector.x =  -self.acceleration.x * 8
 
        end
 
 
 
        if self:getScaleX() > 0 then
 
            self.screenScrollTarget = self.maxSpeed * ((self:getPositionX() - self.minX)/(self.minX + self.range)) * -1
 
        end
 
 
 
        if self:getScaleX() < 0 then
 
            self.screenScrollTarget = self.maxSpeed * ((self.maxX - self:getPositionX() )/(self.minX + self.range))
 
        end
 
    else
 
        --player is flipping
 
        self.vector.x =  -self.acceleration.x * 16
 
 
 
        if self.screenScrollTarget ~= self.vector.x * self:getScaleX() then
 
            self.transition = 0
 
            self.screenScrollTarget = self.vector.x * self:getScaleX()
 
        end
 
    end
 
 
 
    if self.vector.x > self.maxSpeed then
 
        self.vector.x = self.maxSpeed
 
    end
 
 
 
    if self.vector.x <  - self.maxSpeed then
 
        self.vector.x =  - self.maxSpeed
 
    end
 
 
 
    self.nextPosition.x =  self.nextPosition.x + self.vector.x * self:getScaleX()
 
 
 
    if self:getScaleX() > 0 and self:getPositionX() <= self.minX + self.range then
 
 
 
        if self.nextPosition.x >= self.minX + self.range then
 
            self.nextPosition.x = self.minX + self.range
 
        end
 
    end
 
 
 
    if self:getScaleX() < 0 and self:getPositionX() >= self.maxX - self.range then
 
        if self.nextPosition.x <= self.maxX - self.range then
 
            self.nextPosition.x = self.maxX - self.range
 
        end
 
    end
 
 
 
    if self.nextPosition.x < self.minX then
 
        self.nextPosition.x = self.minX
 
    end
 
 
 
    if self.nextPosition.x > self.maxX then
 
        self.nextPosition.x = self.maxX
 
    end
 
 
 
 
 
    self:setPositionX (  self.nextPosition.x )
 
    self:setPositionY (  self.nextPosition.y )
 
 
 
    self:updateScreenSpeed ()
 
 
 
end
 
 
 
 
 
function Player:hit ()
 
 
 
    if self.active == true then
 
        self.active = false
 
       self.game.running = false
 
       self.skin:stopAllActions()
 
        local textureCache = cc.Director:getInstance():getTextureCache()
 
        self.skin:setTexture(textureCache:addImage("boom1.png"))
 
    end
 
end
 
 
 
 
 
function Player:setNewPositionX ( value )
 
    self:setPositionX ( value )
 
    self.nextPosition.x = value
 
end
 
 
 
function Player:setNewPositionY ( value )
 
    self:setPositionY ( value )
 
    self.nextPosition.y = value
 
end
 
 
 
function Player:setNewPosition ( valueX, valueY )
 
   self:setPosition ( valueX, valueY )
 
   self.nextPosition = cc.p(self:getPositionX(), self:getPositionY())
 
end
 
 
 
function Player:updateScreenSpeed ()
 
 
 
    if self.screenScrollNow ~= self.screenScrollTarget then
 
        self.screenScrollNow = self.screenScrollNow + (self.screenScrollTarget - self.screenScrollNow) * self.transition
 
        self.transition =  self.transition + 0.01
 
 
 
        self.screenScroll = self.screenScrollNow
 
    end
 
 
 
    if self.transition > 1 then
 
        self.transition = 0
 
    end
 
 
 
end
 
 
 
return Player

So finally, as mentioned before, we can move on and create the Player bullet, in my next post.