game design and development workshop day 1

65
Game Design & Development Workshop with Cocos2d-x PeopleSpace 9 & 10 July 2016

Upload: troy-miles

Post on 23-Feb-2017

297 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Game Design and Development Workshop Day 1

Game Design & Development Workshop with Cocos2d-xPeopleSpace 9 & 10 July 2016

Page 2: Game Design and Development Workshop Day 1
Page 3: Game Design and Development Workshop Day 1

Troy MilesTroy Miles

Over 37 years of programming experience

Speaker and author

Author of jQuery Essentials

[email protected]

@therockncoder

Page 4: Game Design and Development Workshop Day 1

Introduction

Page 5: Game Design and Development Workshop Day 1

What is Cocos2d-x?

Open source game engine under MIT license

It is optimized for 2D graphics using OpenGL

Used by more than 400,000 developers worldwide

Page 6: Game Design and Development Workshop Day 1

History

2008 Ricardo Quesada in Argentina creates Cocos2d

Ported to iPhone with the opening of the iPhone App Store

2010 Zhe Wang in China forks it creating Cocos2d-x

2013 Ricardo joins Cocos2d-x team

7 July 2016 version 3.12 released

Page 7: Game Design and Development Workshop Day 1

Target Platforms

Android

iOS

macOS

Windows

Page 8: Game Design and Development Workshop Day 1

Development Platforms

Visual Studio 2012+ (Windows)

Xcode 4.6+ (Mac)

CMake 2.6+ (Ubuntu)

All platforms need Python v2.7.x (not 3!)

Page 9: Game Design and Development Workshop Day 1

Development Languages

C++

Lua

JavaScript

Page 10: Game Design and Development Workshop Day 1
Page 11: Game Design and Development Workshop Day 1

Why JavaScript?

Arguably the most popular programming language

It is universal

It is fast

Easier to learn than C++

Page 12: Game Design and Development Workshop Day 1

OS specific limitations

iOS / Mac OS X apps require Xcode and a Mac

Windows apps require Visual Studio and a PC

Page 13: Game Design and Development Workshop Day 1

Prerequisites

Page 14: Game Design and Development Workshop Day 1

Mac Development

Python 2.7.x

Apple Xcode

(Jetbrains’ AppCode)

Page 15: Game Design and Development Workshop Day 1

Windows Development

Python 2.7.x

Visual Studio Studio Community

Page 16: Game Design and Development Workshop Day 1

Before we move on

Be sure that your dev tools are work

For Mac, build a command line tool

For Windows, build a console app

Page 17: Game Design and Development Workshop Day 1

Installation

Page 18: Game Design and Development Workshop Day 1

cocos2d-x.org

Tutorials

Forum

Blog

Downloads

Page 19: Game Design and Development Workshop Day 1

Download

http://www.cocos2d-x.org/download

Under the column, Cocos2d-x,

Click, “DOWNLOAD V3.12”

Unzip it

Page 20: Game Design and Development Workshop Day 1

Installation

From terminal or command prompt

cd to the root of the directory

setup.py

(will modify your path and other env variables)

open the project for your environment

Page 21: Game Design and Development Workshop Day 1

Hello World

Page 22: Game Design and Development Workshop Day 1

Cocos command optionsnew - creates a new game

run - compile, deploy, and run on game on target

deploy - deploy game to target

compile - compiles game

luacompile - minifies Lua files and compiles

jscompile - minifies JS files and compiles

Page 23: Game Design and Development Workshop Day 1

Cocos command options

-h, —help - Shows the help

-v, —version - Shows the current version

Page 24: Game Design and Development Workshop Day 1

Creating a new game

Open the cmd window

Validate that cocos is in your path, cocos <enter>

cocos new gamename -p com.company.name -l js

Page 25: Game Design and Development Workshop Day 1

new command explained

gamename: name of your project

-p com.mycompany.mygame: package name

-l js: programming language used for the project, valid values are: cpp, lua, and js

Page 26: Game Design and Development Workshop Day 1

Build Hello World

From you IDE, build the code

The first time it will take a while

You will get a lot of warnings, but don’t worry about them

Page 27: Game Design and Development Workshop Day 1

JavaScript Refresher

Page 28: Game Design and Development Workshop Day 1

Strict mode

'use strict’; or "use strict;"

First line of file or function

Can break working code!!

More stringent checking

Enables ES5 features

Page 29: Game Design and Development Workshop Day 1

Strict modeVariables must be declared

Functions defined only at the top scope level

“this” is undefined at the global level

Throws error when you forget the 'new' keyword in constructor

Can't use the same function parameter twice

Page 30: Game Design and Development Workshop Day 1

Falsey

Type Results

null FALSE

undefined FALSE

Number if 0 or NaN, FALSE

String if length = 0, FALSE

Object TRUE

Page 31: Game Design and Development Workshop Day 1

Immediately Invoked Function Expression (IIFE)

Has no name

Immediately executed

Used to create a namespaces

Use in many JS libraries

Page 32: Game Design and Development Workshop Day 1

IIFE (continued)

(function() { /* Nothing inside of the function can be seen on on the outside, unless we want it to */ }());

Page 33: Game Design and Development Workshop Day 1

Object Literals

A pair of curly braces surrounding name/value pairs

Can appear anywhere an expression can

The property’s name can be ANY string

Quotes only need when the name is not a legal variable name

Page 34: Game Design and Development Workshop Day 1

Object Literals

var empty = {};

var name = { “first-name”: “Alan”, “last-name”: “Turing”};

Page 35: Game Design and Development Workshop Day 1

Arrays vs. ObjectsArrays are not true arrays

Sort of a special kind of object literal

Both can mix types

Not the same

Arrays inherit from Array.prototype

Objects inherit from Object.prototype

Page 36: Game Design and Development Workshop Day 1

More on objects

JavaScript objects are always dynamic

New properties can always be assigned

There is no class in JavaScript

Page 37: Game Design and Development Workshop Day 1

Array functions

.isArray()

.every()

.forEach()

.indexOf()

.lastIndexOf()

.some()

.map()

.reduce()

.filter()

Page 38: Game Design and Development Workshop Day 1

forEachlet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // forEach iterates over the array, once for each element, but there is no way to // break outnums.forEach(function (elem, index, arr) { console.log(index + ': ' + elem); });

Page 39: Game Design and Development Workshop Day 1

maplet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // map iterates over all of the elements and returns a new array with the same // number of elementslet nums2 = nums.map((elem) => elem * 2); console.log(nums2);

Page 40: Game Design and Development Workshop Day 1

filterlet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // filter iterates over the array and returns a new array with only the elements // that pass the testlet nums3 = nums.filter((elem) => !!(elem % 2)); console.log(nums3);

Page 41: Game Design and Development Workshop Day 1

reducelet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // reduce iterates over the array passing the previous value and the current// element it is up to you what the reduction does, let's concatenate the stringslet letters2 = letters.reduce((previous, current) => previous + current); console.log(letters2); // reduceRight does the same but goes from right to leftlet letters3 = letters.reduceRight((previous, current) => previous + current); console.log(letters3);

Page 42: Game Design and Development Workshop Day 1

everylet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}]; let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']; let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log(nums); // every makes sure that every element matches the expressionlet isEveryNumbers = junk.every((elem) => typeof elem === 'number'); console.log('Are all members of junk numbers: ' + isEveryNumbers);

Page 43: Game Design and Development Workshop Day 1

ES6 aka ES2015

for-of

let

const

Template strings

Arrow functions

Page 44: Game Design and Development Workshop Day 1

let

let allows us to create a block scoped variables

they live and die within their curly braces

best practice is to use let instead of var

Page 45: Game Design and Development Workshop Day 1

let// let allows us to create a block scoped variables// they live and die within the curly braceslet val = 2; console.info(`val = ${val}`); { let val = 59; console.info(`val = ${val}`); } console.info(`val = ${val}`);

Page 46: Game Design and Development Workshop Day 1

const

const creates a variable that can't be changed

best practice is to make any variable that should not change a constant

does not apply to object properties or array elements

Page 47: Game Design and Development Workshop Day 1

constconst name = 'Troy'; console.info(`My name is ${name}`); // the line below triggers a type errorname = 'Miles';

Page 48: Game Design and Development Workshop Day 1

Template strings

Defined by using opening & closing back ticks

Templates defined by ${JavaScript value}

The value can be any simple JavaScript expression

Allows multi-line strings (return is pass thru)

Page 49: Game Design and Development Workshop Day 1

Template strings let state = 'California'; let city = 'Long Beach'; console.info(`This weekend's workshop is in ${city}, ${state}.`); // template strings can run simple expressions like addition let cup_coffee = 4.5; let cup_tea = 2.5; console.info(`coffee: $${cup_coffee} + tea: $${cup_tea} = $${cup_coffee + cup_tea}.`); // they can allow us to create multi-line strings console.info(`This is line #1.this is line #2.`);

Page 50: Game Design and Development Workshop Day 1

Arrow (lambda) function

let anon_func = function (num1, num2) { return num1 + num2; }; console.info(`Anonymous func: ${anon_func(1, 2)}`); let arrow_func = (num1, num2) => num1 + num2; console.info(`Arrow func: ${arrow_func(3, 4)}`);

Page 51: Game Design and Development Workshop Day 1

In a loop what do the break and continue instructions do?A. break cause the program to halt all execute and

continue resumes execution

B. break exits the loop and continue goes immediately to the top of the loop

C. break goes immediately to the top of the loop and continue exits the loop

Page 52: Game Design and Development Workshop Day 1

for-offor of lets us iterate over an array

.entries() gives us the index and the element

with destructuring we can use as variables

unlike forEach, continue and break work

break stop the loop

continue stops the current iteration

Page 53: Game Design and Development Workshop Day 1

for-offor (let elem of pirateArray) { console.info(elem); } // if we want the element and its index we can get it toofor (let [index, elem] of pirateArray.entries()) { if (index === 5) { break; } console.info(`${index}. ${elem}`); } // unlike forEach, continue and break workfor (let [index, elem] of pirateArray.entries()) { if (index === 5) { continue; } console.info(`${index}. ${elem}`); }

Page 54: Game Design and Development Workshop Day 1

JavaScript

Uses Mozilla’s SpiderMonkey JS Engine v33

Released 14 Oct 2014

Same engine used in FireFox 33

Support a lot of ES6 features

(except templates strings)

Page 55: Game Design and Development Workshop Day 1

Right handed coordinate system

Different than web

Origin (0,0) located at lower left hand of screen

x position increases going right

y position increases going up

max x,y at upper right hand corner

Page 56: Game Design and Development Workshop Day 1

Hello World

Cocos2D-x ships as source code

Build it to compile the libraries

Page 57: Game Design and Development Workshop Day 1

Directory Structure

Classes

cocos2d

Resources

Platform directories

Page 58: Game Design and Development Workshop Day 1

Startup up Sequence

launches from main.cpp

calls AppDelegate.cpp

(starts up Cocos2d-x)

calls main.js

(launches your scene)

Page 59: Game Design and Development Workshop Day 1

Graphics Diagnostics

configured in project.json

82 <-- number of draw calls

0.016 <-- time it took to render the frame

60.0 <-- frames per second

Page 60: Game Design and Development Workshop Day 1

Don’t forget

Add JavaScript files to the project.json

Add resources (images/sound/music) to resources.js

Forgetting to add them is a very common bug

Page 61: Game Design and Development Workshop Day 1

Lexicon

Director

Node

Scene

Layer

Sprite

Action

Particle

Event

Page 62: Game Design and Development Workshop Day 1

Let’s build a game together

Page 63: Game Design and Development Workshop Day 1

Steps

Add a background

Add a hero

Move the hero

Detect touches

Fire bullets

Add enemy

Animate enemy

Detect collision

Give points

Detect end of game

Page 64: Game Design and Development Workshop Day 1

Steps

Adding sound

Adding music

Adding scenes

Page 65: Game Design and Development Workshop Day 1

Vote: Tomorrow’s Game

A match 3 puzzle game (think Bedazzled)

A mini platformer (think Mario Land)