Introduction to Mobile Game Programming with Cocos2d-JS

59
Intro to Mobile Game Programming with Cocos2d -JS 2014 - So Cal Code Camp - San Diego Troy Miles

description

We will explore using Cocos2d-JS, an open source game engine, to build games for the two major smart phone platforms - Android and iOS. Cocos2d-JS is free and it runs on many platforms giving you the ability to port your game not only to the most popular phone platforms, but to HTML5 browsers, Windows and Mac OS X.

Transcript of Introduction to Mobile Game Programming with Cocos2d-JS

Page 1: Introduction to Mobile Game Programming with Cocos2d-JS

Intro to Mobile Game Programming with Cocos2d-JS2014 - So Cal Code Camp - San Diego Troy Miles

Page 2: Introduction to Mobile Game Programming with Cocos2d-JS

Troy MilesTroy Miles aka the RocknCoder

Over 30 years of programming experience

Wrote a few games for Interplay in the 80's and 90’s

Speaker and writer on all things web & mobile

Page 3: Introduction to Mobile Game Programming with Cocos2d-JS

Contacting Me

@therockncoder

[email protected]

http://therockncoder.blogspot.com/

https://github.com/Rockncoder

https://www.youtube.com/user/rockncoder

Page 4: Introduction to Mobile Game Programming with Cocos2d-JS

Text

https://bitly.com/bundles/rockncoder/5

Page 5: Introduction to Mobile Game Programming with Cocos2d-JS

Agenda, Session 1

What is Cocos2d-JS?

Setting Up Your Environment

Parts is Parts

Workflow

Page 6: Introduction to Mobile Game Programming with Cocos2d-JS

Agenda, Session 2Let’s look at a game

Audio

Cocos Studio + Other Tools

Ads

Publishing

Promoting

Page 7: Introduction to Mobile Game Programming with Cocos2d-JS

Why make mobile games?

Page 8: Introduction to Mobile Game Programming with Cocos2d-JS
Page 9: Introduction to Mobile Game Programming with Cocos2d-JS
Page 10: Introduction to Mobile Game Programming with Cocos2d-JS

Titles using Cocos2d-x

Page 11: Introduction to Mobile Game Programming with Cocos2d-JS

Text

Avengers AllianceMarvel Entertainment

Page 12: Introduction to Mobile Game Programming with Cocos2d-JS

Text

Family Guy: The Quest for StuffTinyCo, Inc.

Page 13: Introduction to Mobile Game Programming with Cocos2d-JS

Diamond DashWooga

Page 14: Introduction to Mobile Game Programming with Cocos2d-JS

Star Wars: Tiny Death StarLucasArts

Page 15: Introduction to Mobile Game Programming with Cocos2d-JS

BADLANDFrogmind

Page 16: Introduction to Mobile Game Programming with Cocos2d-JS
Page 17: Introduction to Mobile Game Programming with Cocos2d-JS

Cocos2d-JS

History

Cocos2d-JS

Platforms and Restrictions

Page 18: Introduction to Mobile Game Programming with Cocos2d-JS

History2008 - Ricardo Quesada, a game developer in Argentina, writes a game framework named Cocos2d

2009 - After the iPhone SDK release, Cocos2d rewritten in Objective-C to become Cocos2d-iPhone

2010 - Zhe Wang, a developer in China, creates a fork, Cocos2d-x

Chukong Technologies Inc, Zhe's company, develops Cocos2d-JS, -HTML5, and -Lua

Page 19: Introduction to Mobile Game Programming with Cocos2d-JS

Cocos2d-JS

Cocos2d-x JavaScript version

Full Cocos2d-x functionality

Simplified JavaScript friendly API

Code Once, Run Everywhere

Mobile Native, Desktop, Web

Page 20: Introduction to Mobile Game Programming with Cocos2d-JS
Page 21: Introduction to Mobile Game Programming with Cocos2d-JS

Platforms

Mac OS X

Windows

Page 22: Introduction to Mobile Game Programming with Cocos2d-JS

Mac OS X

iOS

Android

HTML 5

Mac OS X

Page 23: Introduction to Mobile Game Programming with Cocos2d-JS

Windows

Windows 7

Android

HTML5

Windows Phone 8 (soon)

Page 24: Introduction to Mobile Game Programming with Cocos2d-JS

Prerequisites

Page 25: Introduction to Mobile Game Programming with Cocos2d-JS

Android

Java 6 or 7

Android ADT (NOT Android Studio)

Android NDK

Page 26: Introduction to Mobile Game Programming with Cocos2d-JS

iOS

Xcode

Page 27: Introduction to Mobile Game Programming with Cocos2d-JS

HTML5

No extra prerequisites

Page 28: Introduction to Mobile Game Programming with Cocos2d-JS

Desktops

You can also build desktop apps for Windows and Mac OS X

You will need Visual Studio and Xcode respectively

I won’t speak further about this

Page 29: Introduction to Mobile Game Programming with Cocos2d-JS

Python 2The cocos command line needs python to run

python 2.7.5+

It won’t work with python 3

https://www.python.org/downloads/

Page 30: Introduction to Mobile Game Programming with Cocos2d-JS

Installation

http://cocos2d-x.org/download

Unzip the file

execute ./setup.py

Page 31: Introduction to Mobile Game Programming with Cocos2d-JS

cocos command options

new - Creates a new project

compile - Compiles the current project to binary

deploy - Deploy a project to the target

run - Compiles & deploy project and then runs it on the target

jscompile - minifies and/or compiles js files

Page 32: Introduction to Mobile Game Programming with Cocos2d-JS

cocos new

cocos new -l js

cocos new -l js —no-native

cocos new projectName -l js -d ./Projects

cocos new projectName -l js

Page 33: Introduction to Mobile Game Programming with Cocos2d-JS

Parts is Parts

Project Files & Folders

Terminology

Coordinate system

Page 34: Introduction to Mobile Game Programming with Cocos2d-JS

Project files and foldersframeworks/runtime/tools

res - your resources

src - your JS source code

project.json - configuration

main.js - JS boot code

index.html - HTML markup

Page 35: Introduction to Mobile Game Programming with Cocos2d-JS

project.json

debug - the current debug setting

showFPS - whether or not to show the frame rate

frameRate - the desired frame rate

modules - cocos2d and potentially plugins

jsList - all of your source code

Page 36: Introduction to Mobile Game Programming with Cocos2d-JS

Demo time

Page 37: Introduction to Mobile Game Programming with Cocos2d-JS

The code flow

index.html

main.js's

app.js

Page 38: Introduction to Mobile Game Programming with Cocos2d-JS

Terminologycc - Cocos2d, most classes prepended with this

Director - takes care of navigation between scenes

Node - Almost every element is a node

Scene - a screen in your game

Sprite - a 2D image that can be moved, rotated, scaled & more

Layer - a special node which accepts user input

Page 39: Introduction to Mobile Game Programming with Cocos2d-JS

More terminology

Action - an order given to a node, like animations

Menu - Creates an onscreen menu

LabelTTF - The simplest option for displaying text

Page 40: Introduction to Mobile Game Programming with Cocos2d-JS

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 41: Introduction to Mobile Game Programming with Cocos2d-JS

Workflow

Program Editor

Deploying

Modifying

Debugging

Page 42: Introduction to Mobile Game Programming with Cocos2d-JS

Program editorI used WebStorm 8

It is available for Windows, Mac, and Linux

It has a Chrome extension, JetBrains IDE Support, allows debugging

Page 43: Introduction to Mobile Game Programming with Cocos2d-JS

Deploying

Choose

Right click on index.html

Click Debug “index.html”

Page 44: Introduction to Mobile Game Programming with Cocos2d-JS

Modifying

Double tap a file to open it in the editor

Modify the text as desire

Click the refresh icon to run the code again

Page 45: Introduction to Mobile Game Programming with Cocos2d-JS

Debugging

To set a break point, click in the gutter to the left of the text

To set a conditional breakpoint, right click it and specify a condition

Click again to delete it

Page 46: Introduction to Mobile Game Programming with Cocos2d-JS

Let’s look at a gameParts

Scenes

Player

User Input

Physics plug-in

Collision detection

Page 47: Introduction to Mobile Game Programming with Cocos2d-JS

Audio

File formats

Sounds

Music

Page 48: Introduction to Mobile Game Programming with Cocos2d-JS

File formats

All of the devices can play mp3

Only some of the browsers can

Page 49: Introduction to Mobile Game Programming with Cocos2d-JS
Page 50: Introduction to Mobile Game Programming with Cocos2d-JS

Playing sounds

Playing sounds is easy

Simply call the audioEngine’s playEffect method

Page 51: Introduction to Mobile Game Programming with Cocos2d-JS

Cocos Studio + Other Tools

Animation Editor

UI Editor

Scene Editor

Data Editor

Tiled

Page 52: Introduction to Mobile Game Programming with Cocos2d-JS

Ads

Ads are tough since there is no readily available JS module

AdMob / AdSense

iAds

Microsoft Ads

Page 53: Introduction to Mobile Game Programming with Cocos2d-JS

Publishing

Becoming a developer

Materials

Deploying

Page 54: Introduction to Mobile Game Programming with Cocos2d-JS

iOS

$99 a year for an individual in the local currency

$299 for an enterprise

Free for the university program

Only developers can put apps on devices

Page 55: Introduction to Mobile Game Programming with Cocos2d-JS

Android

$25 for a lifetime

Anyone can put apps on a device

Page 56: Introduction to Mobile Game Programming with Cocos2d-JS

MaterialsSigned app

Icons

Screenshots

Big Images

Descriptions

Website

Videos

Page 57: Introduction to Mobile Game Programming with Cocos2d-JS

Promoting

Family and Friends

Social Media

Web site

Advertising

Page 58: Introduction to Mobile Game Programming with Cocos2d-JS

Summary

Cocos2d-JS allows you to create fast cross platform games

Installation and setup can be a bit tough

Monetizing with Ads is also tough

Page 59: Introduction to Mobile Game Programming with Cocos2d-JS

Text

https://bitly.com/bundles/rockncoder/5