Search…

Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 1: Tạo Scene

30/09/20203 min read
Hướng dẫn cách tạo scene cho game với Cocos2d-x

Chuỗi bài viết hướng dẫn hiện thực lại game ZERO đơn giản. Giúp làm quen cách để hoàn thành 1 game, cũng như cách tổ chức project trong game.

Bài viết này sẽ hướng dẫn cách tạo scene cho game trong Cocos2d-x.

Tạo project

Để có thể viết game, công việc đầu tiên là tạo mới một project.

Sau khi tạo xong, vào project mở solution (win32) lên.

ss_1

Tạo scene

Sau khi tạo một project, cocos2d-x sẽ tự động tạo ra 4 file:

  • AppDelegate.h
  • AppDelegate.cpp
  • HelloWorldScene.h
  • HelloWorldScene.cpp.

Trong đó với HelloWorldScene là scene mặc định. 

Tiếp theo là hướng dẫn cách để tạo các scene mới trong game từ HelloWorldScene. Các scene cần tạo trong game này là:

  • LoadScene
  • MainMenuScene
  • GameScene
  • GameOverScene.

Đổi tên 2 file HelloWorldScene.h và HelloWorldScene.cpp ở trên lần lượt thành LoadScene.h và LoadScene.cpp. 

Trong file LoadScene.h đổi các từ khóa HelloWordScene thành LoadScene được như sau:

#ifndef __LOAD_SCENE_H__
#define __LOAD_SCENE_H__

#include "cocos2d.h"

class LoadScene : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    
    // implement the "static create()" method manually
    CREATE_FUNC(LoadScene);
};

#endif // __LOAD_SCENE_H__

Tương tự trong file LoadScene.cpp

#include "LoadScene.h"

USING_NS_CC;

Scene* LoadScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = LoadScene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool LoadScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    
    return true;
}

Trong file AppDelegate.cpp thay dòng

#include "HelloWorldScene.h"

thành

#include "LoadScene.h"

và dòng

auto scene = HelloWorld::createScene();

thành

auto scene = LoadScene::createScene();

Tiếp theo vào thư mục classes của project.

Từ 2 file LoadScene.h và LoadScene.cpp có sẵn, tiến hành copy ra và đổi tên thành các scene khác như sau:

Tạo scene cocos2d-x

Kéo thả các scene vừa tạo vào môi trường làm việc được như hình sau:

Tạo scene cocos2d-x

Vào từng scene vừa tạo và tiến hành đổi nội dung tương tự như với LoadScene. 

Download source code

Dưới đây là source code sau khi kết thúc bài viết này. 

SOURCE_CODE

Bài chung series

IO Stream

IO Stream Co., Ltd

30 Trinh Dinh Thao, Hoa Thanh ward, Tan Phu district, Ho Chi Minh city, Vietnam
+84 28 22 00 11 12
developer@iostream.co

383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city
Business license number: 0311563559 issued by the Department of Planning and Investment of Ho Chi Minh City on February 23, 2012

©IO Stream, 2013 - 2024