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

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(หน้าที่ถูกสร้างด้วย ': ''This is part of 01219245, 2nd semester 2557.'' In this tutorial, we will recreate a clone of a wonderful [http://en.wikipedia....')
 
แถว 27: แถว 27:
  
 
== The player and its movement ==
 
== The player and its movement ==
 +
You should start by creating a new Cocos-JS project.
 +
 +
cocos new -l js --no-native ชื่อโปรเจ็ค
 +
 +
This will create an example HelloWorld project.  You should clean up the example by following instructions outlined in [[01219245/cocos2d-js/Sprites#Let.27s_clean_up_HelloWorld_and_start_with_our_empty_game|Tutorial 100]].
 +
 +
Then, create a git repository at the project directory.
 +
 +
{{gitcomment|Create your git repository.}}
 +
 +
=== Creating the sprite ===
 +
In this step, we shall create a sprite for the player, and show it in the middle of the screen.
 +
 +
Use a graphic editor to create an image for our player.  The image should be of size 40 pixels x 40 pixels.  Save the image as <tt>images/dot.png</tt> and try to make it look cute.
 +
 +
We shall create class <tt>Player</tt> as <tt>src/Player.js</tt>.
 +
 +
<syntaxhighlight lang="javascript">
 +
var Player = cc.Sprite.extend({
 +
    ctor: function() {
 +
        this._super();
 +
        this.initWithFile( 'res/images/dot.png' );
 +
    }
 +
});
 +
</syntaxhighlight>
 +
 +
We shall create the player in <tt>GameLayer.init</tt>.  To do so add these lines:
 +
 +
<syntaxhighlight lang="javascript">
 +
        this.player = new Player();
 +
        this.player.setPosition( new cc.Point( screenWidth / 2, screenHeight / 2 ) );
 +
        this.addChild( this.player );
 +
        this.player.scheduleUpdate();
 +
</syntaxhighlight>
 +
 +
Note that we use constants <tt>screenWidth</tt> and <tt>screenHight</tt> (which are 800 and 600, respectively).  Don't forget to add this constant in <tt>main.js</tt>.
 +
 +
Try to refresh the game.  You should see your sprite in the middle of the screen.
 +
 +
{{gitcomment|Commit your work.}}

รุ่นแก้ไขเมื่อ 03:11, 11 กุมภาพันธ์ 2558

This is part of 01219245, 2nd semester 2557.

In this tutorial, we will recreate a clone of a wonderful Flappy Bird. Let's call it Flappy Dot (as our player would look like a dot). We will develop basic game mechanics in this tutorial. We will try to add special effects to the game in the next tutorial.

Make sure you have completed Tutorial 100 on basic sprites.

Task breakdown

Before we start, make sure you know how this game works. You may want to try it for a bit. I guess many of your friends have it on their phones. This is how our game would look like:

219245-dotscr.png

As usual, let's start by thinking about the possible list of increments we would add to an empty project to get this game.

When you get your list, please see the steps that we plan to take here.

  • Show the player on the screen.
  • The player can jump and fall. (Implement player physics)
  • Show a single pillar pair.
  • Move the pillar pair across the screen.
  • Let the pillar pair reappear.
  • Check for player-pillar collision.
  • Make the game with one pillar pair.
  • Show more than one pillar pairs.

The player and its movement

You should start by creating a new Cocos-JS project.

cocos new -l js --no-native ชื่อโปรเจ็ค

This will create an example HelloWorld project. You should clean up the example by following instructions outlined in Tutorial 100.

Then, create a git repository at the project directory.

Gitmark.png Create your git repository.

Creating the sprite

In this step, we shall create a sprite for the player, and show it in the middle of the screen.

Use a graphic editor to create an image for our player. The image should be of size 40 pixels x 40 pixels. Save the image as images/dot.png and try to make it look cute.

We shall create class Player as src/Player.js.

var Player = cc.Sprite.extend({
    ctor: function() {
        this._super();
        this.initWithFile( 'res/images/dot.png' );
    }
});

We shall create the player in GameLayer.init. To do so add these lines:

        this.player = new Player();
        this.player.setPosition( new cc.Point( screenWidth / 2, screenHeight / 2 ) );
        this.addChild( this.player );
        this.player.scheduleUpdate();

Note that we use constants screenWidth and screenHight (which are 800 and 600, respectively). Don't forget to add this constant in main.js.

Try to refresh the game. You should see your sprite in the middle of the screen.

Gitmark.png Commit your work.