首頁 資料庫 mysql教程 零基础学Cocos2d

零基础学Cocos2d

Jun 07, 2016 pm 03:49 PM
create 基礎

// create a scene. it's an autorelease object Scene *scene = HelloWorld :: createScene (); // run director- runWithScene (scene); 那么接下来,我们看看这场戏到底内部是执行流程的

// create a scene. it's an autorelease object

    Scene *scene = HelloWorld::createScene();


    // run

    director->runWithScene(scene);


那么接下来,我们看看这场戏到底内部是执行流程的啊。

零基础学Cocos2d

OK,首先看看HelloWorldScene.h 到底有什么东西。

静态创建函数

static cocos2d::Scene* createScene();


初始化

virtualbool init();


菜单的一个回调函数

void menuCloseCallback(cocos2d::Ref* pSender);


这个。。。。看宏定义上面的注释说是创建一个特定的类

CREATE_FUNC(HelloWorld);


/**
 * define a create function for a specific type, such as Layer
 * @param \__TYPE__  class type to add create(), such as Layer
 */
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}
登入後複製

看完后 ,哦,,,,
CREATE_FUNC(HelloWorld);
登入後複製

就是相当于

在 HelloWorldScene.h 的定义

static HelloWorld* create();

在 HelloWorldScene.m 的实现

HelloWorld* HelloWorld::create()
{
    //创建一个 HelloWorld 对象
    HelloWorld* helloWorld = new HellWorld();
    //判断 HelloWorld 对象是否创建以及初始化成功
    if (helloWorld && helloWorld->init())
    {
        //创建成功,初始化成功后,让其自动释放内存
        helloWorld->autorelease();
        //返回 HelloWorld 实例
        return helloWorld;
    } else {
        //如果创建失败,将安全删除 HelloWorld 对象
        delete helloWorld;
        helloWorld = NULL;
        return NULL;
    }
}
登入後複製

好了,有宏的话,让我们剩下了不少代码的工作量啊。

接下来我们看看其他的吧


HelloWorldScene.cpp 的里面的函数的执行顺序是

Scene* HelloWorld::createScene();

bool HelloWorld::init();


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

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

    // return the scene
    return scene;
}
登入後複製

可以写成
Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    Scene* scene = Scene::create();
    
    // 'layer' is an autorelease object
    Layer* 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();
    Point origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // 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(Point(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(Point::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(Point(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(Point(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;
}
登入後複製

虽然这段很长,不过包含了很多要学习的知识。


从表面上看,这段代码都在讲初始化的那些事。

细心观察,这个和Objective-C 的 init 方法多类似啊,只是不是返回对象。

我们精简一下这段代码的框架

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    //初始化的内容
    
    return true;
}
登入後複製


接下来看看里面进行的初始化的内容吧


向导演问了相关舞台的数据

Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
登入後複製


然后搞一个按钮出来,这个按钮可以触发指定的事件


// 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // 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(Point(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(Point::ZERO);
    this->addChild(menu, 1);
登入後複製



或者写成

// 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    MenuItemImage* closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    Menu* menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);
登入後複製



MenuItemImage 类 创建一个对象,放入两张图片,和一个回调函数。

第一张图片是正常状态的,第二张是选择状态时的,回调函数,this 应该是目标

和iOS 创建按钮很相似,而区别是没有触发事件的手势设置。

接下来就是设置 MenuItemImage 类 实例的位置

通过 MenuItemImage 类 实例 创建一个 Menu 类的实例。

设置坐标

最后,将这个Menu类的实例加入当前 Layer中

接下来就是创建一个Label 类了。

根据官方发布文档所描述。3.0将采用一个Label 类 来创建不同类型的Label,而且优化了很多性能,这些也是后话了。

// 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(Point(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);

    
登入後複製


创建一个LabelTTF类的实例,参数1是内容,参数2是字体,参数3是字体大小

然后就是设置 这个实例的位置

然后加入层



// add "HelloWorld" splash screen"
    auto sprite = Sprite::create("HelloWorld.png");

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

    // add the sprite as a child to this layer
    this->addChild(sprite, 0);
登入後複製
我们接下来就是把加载一张图片,全屏显示


首先用精灵创建一个实例,参数是一张图片。

然后设置精灵的位置。

最后把精灵加入层里,



最后我们看看回调函数吧,当点击按钮时,就会触发这个回调函数,因为已经关联上了。

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#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();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
登入後複製

这里实现的功能很简单,就是退出应用程序而已。



好了,就这样就结束了。接下来就是详情了。

表面上看,Cocos2d-X 真的不难~~~

呵呵

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP基礎教學:從入門到精通 PHP基礎教學:從入門到精通 Jun 18, 2023 am 09:43 AM

PHP基礎教學:從入門到精通

學習Go語言變數的基礎知識 學習Go語言變數的基礎知識 Mar 22, 2024 pm 09:39 PM

學習Go語言變數的基礎知識

PHP基礎入門:如何使用echo函數輸出文字內容 PHP基礎入門:如何使用echo函數輸出文字內容 Jul 30, 2023 pm 05:38 PM

PHP基礎入門:如何使用echo函數輸出文字內容

C語言函數詳解:基礎到進階,全面解析函數的使用 C語言函數詳解:基礎到進階,全面解析函數的使用 Feb 18, 2024 pm 02:25 PM

C語言函數詳解:基礎到進階,全面解析函數的使用

create語句能用來建立表格結構並追加新的記錄嗎 create語句能用來建立表格結構並追加新的記錄嗎 Jul 25, 2022 am 11:25 AM

create語句能用來建立表格結構並追加新的記錄嗎

PHP函數用法:從基礎到進階 PHP函數用法:從基礎到進階 Jun 15, 2023 pm 11:11 PM

PHP函數用法:從基礎到進階

不要錯過立即獲取免費基礎 C# 認證的機會來自Microsoft 不要錯過立即獲取免費基礎 C# 認證的機會來自Microsoft Sep 01, 2023 pm 12:45 PM

不要錯過立即獲取免費基礎 C# 認證的機會來自Microsoft

PHP學習筆記:基礎語法及變數定義 PHP學習筆記:基礎語法及變數定義 Oct 09, 2023 am 08:03 AM

PHP學習筆記:基礎語法及變數定義

See all articles