ผลต่างระหว่างรุ่นของ "01219245/cocos2d-html5/Sprites"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 41: แถว 41:
 
There are 3 main files for loading a Cocos2d-html5 game.  In this section, we shall describe what should be in each file, and the changes we made from the original template.
 
There are 3 main files for loading a Cocos2d-html5 game.  In this section, we shall describe what should be in each file, and the changes we made from the original template.
  
* '''<tt>index.html</tt>''': This is the only HTML page for the game.  It contains a '''<tt>canvas</tt>''' element where the game would draw on. Note that we can set the canvas size (which would be the game screen size) with the <tt>width</tt> and <tt>height</tt> attributes of this element.
+
=== <tt>index.html</tt> ===
 +
This is the only HTML page for the game.  It contains a '''<tt>canvas</tt>''' element where the game would draw on. Note that we can set the canvas size (which would be the game screen size) with the <tt>width</tt> and <tt>height</tt> attributes of this element.
  
 
<syntaxhighlight lang="html5">
 
<syntaxhighlight lang="html5">
แถว 49: แถว 50:
 
It also loads '''<tt>cocos2d.js</tt>''' script file.
 
It also loads '''<tt>cocos2d.js</tt>''' script file.
  
* '''<tt>cocos2d.js</tt>'''
+
=== <tt>cocos2d.js</tt> ===
 +
This is the JavaScript file loaded in <tt>index.html</tt>. This module is responsible for loading other application files.  Also, basic configuration of the framework is done here.
  
* '''<tt>main.js</tt>'''
+
In the beginning of the code, there is an object with the framework parameters
 +
 
 +
<syntaxhighlight lang="javascript">
 +
    var c = {
 +
        COCOS2D_DEBUG: 2, //0 to turn debug off, 1 for basic debug, and 2 for full debug
 +
        box2d: false,
 +
        chipmunk: false,
 +
        showFPS: true,
 +
        loadExtension: false,
 +
        frameRate: 60,
 +
        renderMode: 1,      //Choose of RenderMode: 0(default), 1(Canvas only), 2(WebGL only)
 +
        tag: 'gameCanvas', //the dom element to run cocos2d on
 +
        engineDir: '../../cocos2d/',
 +
        //SingleEngineFile:'',
 +
        appFiles:[
 +
            'src/GameLayer.js'    //add your own files in order here
 +
        ]
 +
    };
 +
</syntaxhighlight>
 +
 
 +
A few interesting parameters:
 +
* '''<tt>engineDir</tt>''': Since we put our program in <tt>mygames/tutorial1</tt>, the directory to the engine '''<tt>engineDir</tt>''' is set to '''<tt>../../cocos2d</tt>'''.
 +
* '''<tt>appFiles</tt>''': You should list all JavaScript files here.  Currently, there is just one file <tt>src/GameLayer.js</tt>
 +
 
 +
=== <tt>main.js</tt> ===
  
 
== Sprites ==
 
== Sprites ==

รุ่นแก้ไขเมื่อ 12:50, 3 กุมภาพันธ์ 2557

This is part of 01219245.

In this tutorial, we will create a simple html5 program with Cocos2d-x.

In what follows, you should use Firefox as a browser. Chrome has stricter permission so if you want to view Cocos2d-html5 game, you'll have to install a web server software in your laptop.

Getting started

This tutorial is based on Cocos2d-html5 version 2.2.2, however, it should work for other versions as well.

Download the library Cocos2d-html5 from here.

Find a location for the library and unzip the library there. In that directory, it should look like this:

\- Cocos2d-html5-v2.2.2
   |- AUTHORS.txt
   |- CHANGELOG.txt
   |- cocos2d
   |- extensions
   |- external
   |- HelloHTML5World
   |- index.html
   |- lib
   |- licenses
   |- README.mdown
   |- samples
   |- template
   \- tools

Let's create directory mygames inside Cocos2d-html5-v2.x.x and we will put all our games there.

\- Cocos2d-html5-v2.2.2
   |- ..
   \- mygames

A Cocos2d-html5 application needs a few start-up codes. While Cocos2d-html5 provides template files for us, we shall use a slightly modified template instead. You can download it at 219245-template.zip.

To get started, download 219245-template.zip and unzip it into mygames. Rename directory 219245-template to tutorial1 and create a Git repository there.

Starting code

There are 3 main files for loading a Cocos2d-html5 game. In this section, we shall describe what should be in each file, and the changes we made from the original template.

index.html

This is the only HTML page for the game. It contains a canvas element where the game would draw on. Note that we can set the canvas size (which would be the game screen size) with the width and height attributes of this element.

<canvas id="gameCanvas" width="800" height="600"></canvas>

It also loads cocos2d.js script file.

cocos2d.js

This is the JavaScript file loaded in index.html. This module is responsible for loading other application files. Also, basic configuration of the framework is done here.

In the beginning of the code, there is an object with the framework parameters

    var c = {
        COCOS2D_DEBUG: 2, //0 to turn debug off, 1 for basic debug, and 2 for full debug
        box2d: false,
        chipmunk: false,
        showFPS: true,
        loadExtension: false,
        frameRate: 60,
        renderMode: 1,       //Choose of RenderMode: 0(default), 1(Canvas only), 2(WebGL only)
        tag: 'gameCanvas', //the dom element to run cocos2d on
        engineDir: '../../cocos2d/',
        //SingleEngineFile:'',
        appFiles:[
            'src/GameLayer.js'     //add your own files in order here
        ]
    };

A few interesting parameters:

  • engineDir: Since we put our program in mygames/tutorial1, the directory to the engine engineDir is set to ../../cocos2d.
  • appFiles: You should list all JavaScript files here. Currently, there is just one file src/GameLayer.js

main.js

Sprites

Creating a sprite image file

Sprite class

Moving sprites

Keyboard inputs