cocos2d-x 3.0的入門程序:helloworld

看過了這麼多不一樣方向的應用,發現不少程序入門都是helloworld
helloworld是全部程序員的絕對初戀

先看一下程序的運行結果吧

ios


而後就是他的工程代碼

程序員

工程的目錄有兩個app

 

    Classes:程序中的類框架

          AppDelegate.h/cpp:Cocos2d-x程序框架
          AppMacros.h:所用到的宏,主要是設置分辯率及對應的資源目錄         
          HelloWorldScene.h/cpp:場景顯示層函數

    win32:WIN32程序所涉及的主函數ui

          main.cpp:winMain主函數this

 

WinMain函數:spa

 

#include "main.h"
#include "AppDelegate.h"
#include "cocos2d.h"

USING_NS_CC;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // create the application instance
    AppDelegate app;
	//運行建立程序
    return Application::getInstance()->run();
}

 

      一切都被封裝到程序類AppDelegate中,這是一個基於Cocos2d-x的cocos2d::Application類的派生類。.net

它將程序框架封裝爲一個類,提供了統一的多平臺上基本程序框架的實現。code

 

AppDelegate.cpp:

#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() 
{
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director建立導演
    auto director = Director::getInstance();
	//建立opengl窗口
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
        director->setOpenGLView(glview);
    }

    // turn on display FPS 打開FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call thi 1秒60幀
    director->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object建立場景和層
    auto scene = HelloWorld::createScene();

    // run啓動場景
    director->runWithScene(scene);

    return true;
}
//當收到電話,遊戲轉入後臺服務
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
//當電話完成,選擇恢復遊戲時,響應這句
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

  

 

下面咱們來看一下HelloWorld場景,它是一個基於cocos2d::Layer的派生類。cocos2d::Layer是什麼?在這裏,我想打個比方來創建一些基本的認知,比方說咱們生活在地球上,地球屬於宇宙內的一部分。從Cocos2d-x的框架體系來看,咱們是Sprite精靈,地球是Layer,而宇宙是Scene。

 

        一個程序要想表現出精彩的世界,要先創建一個宇宙Scene,而後增長地球,月球,太陽等Layer,而後在這些Layer上增長相應的物體。而咱們站在地球上,地球運動,咱們也會跟着一塊兒運動。

 

  OK,如今咱們來看一下如何建立Scene和Layer:

 

HelloWorldScene.h:

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::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 HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    //獲得屏幕的大小,獲得原點
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
	//   // 建立一個菜單項,它由兩張圖片來表現普通狀態和按下狀態,設置按下時調用menuCloseCallback函數響應關閉  
    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    //指定菜單位置,菜單項
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object菜單項放到菜單裏
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);//放到當前的層

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    //建立標籤
    auto label = LabelTTF::create("Hello World", "Arial", 24);
    
    // position the label on the center of the screen標籤位置
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer將標籤放到層中
    this->addChild(label, 1);

    // add "HelloWorld" splash screen"建立圖片精靈
    auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen精靈位置
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer將精靈放到層中
    this->addChild(sprite, 0);
    
    return true;
}

//點close菜單項的時候來回調的
void HelloWorld::menuCloseCallback(Ref* pSender)
{
	////若是是WP8平臺,彈出消息框提示一下。  
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif
	//終止程序。
    Director::getInstance()->end();
	//若是是ios平臺
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

  Layer中增長了精靈,按鈕,文字等表現物,有了這些表現物,一個Layer纔有價值。

參考:http://blog.csdn.net/honghaier/article/details/24518863(謝謝)

相關文章
相關標籤/搜索