Orca原创,转贴请标明链接Orca的移动开发轨迹。
目前整个学习笔记的版本是cocos2d-x 2.1.0beta3。一、sprite的创建
1.基本的创建方法
先打开HelloWorldScene.cpp,重写一遍背景的加入。
// 进行测试,首先是加入普通sprite
// 计算整个屏幕的size
CCDirector * _pdirector = CCDirector::sharedDirector();
CCSize winSize = _pdirector->getVisibleSize();
CCSprite *bg = CCSprite::create("helloworld.png");
bg->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(bg);
这是最基本的加入sprite的方式。先用文件生成指向sprite对象的指针,然后设置这个对象的属性。设置完了以后再加入当前的layer。
sprite即精灵,这是我们整个游戏当中的基础对象。
对于sprite的属性,我们可以查阅官方文档。
可见它继承于Node,TextureProtocol,RGBAProtocol
可以先在sprite.h里面看一下sprite类的声明。
先看有关创建部分
/** Creates an sprite with an image filename.
The rect used will be the size of the image.
The offset will be (0,0).
*/
static CCSprite* create(const char *pszFileName);
/** Creates an sprite with an image filename and a rect.
The offset will be (0,0).
*/
static CCSprite* create(const char *pszFileName, const CCRect& rect);
其实我们可以发现,以前有许多创建sprite的接口,但是都已经被标注为This interface will be deprecated sooner or later.
也就是说,现在官方推荐使用的创建sprite的方式就只剩下两个重载函数。
1.static CCSprite* create(