首页 > 技术文章 > cocos2d-x-3.x 学习总结(一)

dangeal 2016-05-22 11:18 原文

这周学习了《cocos2d-x 3.x 游戏开发之旅》的第三章,做如下总结:

 

1、关于创建标签对象 

书中是

Label* label = Label::create("I am Label Item.", "Arial", 30);

可是总是提示出错,若果这样创建时则正确:

CCLabelTTF* label = CCLabelTTF::create("I am Label Item.", "Arial", 30);

参考链接 http://www.cnblogs.com/xuling/archive/2012/02/29/2372721.html

 

 

2、关于回调函数

回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。

详解参考链接http://baike.baidu.com/link?url=pQ6Fb2k1zZBdMuyecugbb3x2dU9uKWZm4cV1PapsOxTf4LRuOXOCx3khe2MVAvrzKfRrI4eRMsdLteJ3Vatnaq

menu_selector(HelloWorld::menuCloseCallback)

 

3、关于this 指针 ,详解参考链接http://blog.chinaunix.net/uid-21411227-id-1826942.html

  • 在每一个成员函数中都包含一个特殊的指针,这个指针的名字是固定的,称为this指针。它是指向本类对象的指针,它的值是当前被调用的成员函数所在的对象的起始地址。
  •  例如:当调用成员函数a.volume时,编译系统就把对象a的起始地址赋给this指针,于是在成员函数引用数据成员时,就按照this的指向找到对象a的数据成员。
  •  this指针是隐式使用的,它是作为参数被传递给成员函数的
bool HelloWorld::init()
{
    if(!Layer::init())
    {
        return false;
    }
    
    //用一张图片创建一个精灵对象
    Sprite* sprite = Sprite::create("CloseNormal.png");
    //设置精灵坐标
    sprite->setPosition(Point(800,200));
    //将精灵添加到层
    this->addChild(sprite);
    
       return true;
}

 

 

在看一个例子:

#include <iostream>
using namespace std;

class Box
{
public:
    Box(int h = 0, int w = 12, int len = 15):height(h),width(w),length(len){}//声明有默认参数的构造函数,用参数初始化表对数据成员初始化

    int volume();

private:
    int height, width, length;
};


int Box::volume()
{
    return (this->height * this->width * this->length);
}

int main()
{
    Box a[3] = {Box(10, 12, 15), Box(15, 18, 20), Box(16, 20, 26)};

    cout << "volume of a[0] is " << a[0].volume() << endl;
    cout << "volume of a[1] is " << a[1].volume() << endl;
    cout << "volume of a[2] is " << a[2].volume() << endl;

    system("pause");
}

在这里:

return (this->height * this->width * this->length);//显式使用this指针

return (height * width * length); //隐式使用this指针

 

4、关于c_str()函数

log("%s%d", valStr.asString().c_str(), valInt.asInt());
语法: 
const char *c_str();
c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。


参考链接 http://blog.csdn.net/nancy_m/article/details/7583550








推荐阅读