Cocos2D-X渲染框架之CCSprite

来源:互联网 发布:webstorm破解版 mac 编辑:程序博客网 时间:2024/06/07 12:47

这是渲染框架中最小的一个单元了,游戏精灵是游戏制作中非常重要的一个概念。它是游戏内容最好的呈现。游戏开发者主要的控制对象也会是精灵。精灵对象可以是游戏画面中的任何一对象,可以是游戏中的主角,也可以是汽车,或者是一个树,一个高富帅,一个穷屌丝,哪怕是一片树叶也可以是一个精灵对象。从技术角度来看,精灵对象就是一个可以不断变化的图片,而其本身也具备了一些特殊的属性和方法,比如纹理、尺寸、翻转、透明以及角度的改变。精灵作为游戏的重要元素,最后也会被开发者新富裕一些新的逻辑或者物理属性。


初始化,为精灵添加图片的几种方法:

CCSptire::create("文件名");

CCSptire::initWithFile("文件名");

使用纹理来创建精灵(先创建纹理对象):

CCSptire::create("纹理名");

CCSptire::initWithFile("纹理名");

使用精灵框帧创建精灵:

CCSptire::create("精灵帧名");

CCSptire::initWithSptiteFrame("精灵帧名");

CCSprite】与其他类的关系

classCC_DLLCCSprite:publicCCNode,publicCCTextureProtocol,publicCCRGBAProtocol

新建一个精灵对象

CCSprite * ulBaby=CCSprite::createWithSpriteFrameName("UL1.png");   

添加一个精灵对象在layer

this->addChild(ulBaby);

创建精灵的几种方式:

·直接创建:

CCSprite* pSprite =CCSprite::create("HelloWorld.png");

this->addChild(pSprite,0);//如果看不见,好好设置位置

·使用纹理来创建精灵

CSprite * sprite1=CCSprite::createWithTexture(CCTextureCache::sharedTextureCache()->addImage("UL.png"));

this->addChild(sprite1, 0);//如果看不见,好好设置位置

·使用精灵帧来创建精灵CCSprite * ulBaby=CCSprite::createWithSpriteFrameName("UL1.png");  

this->addChild(sprite1, 0);//如果看不见,好好设置位置

下面来展示张纹理图,就是由多张精灵对象的纹理拼接在一起的情况,这张图可以通过多张图片,使用叫【TexturePacker-3.1.3】的软件生成的,同时还会生成一个plist文件,之后的动画会用到(找不到的留个邮箱我会抽空发过去)。在这种情况下,开发者就需要为精灵对象设定一个矩形区域用来显示纹理内容。


 

CCTexture2D::PVRImagesHavePremultipliedAlpha(true);    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("UL.plist");        CCSprite * ulBaby=CCSprite::createWithSpriteFrameName("UL1.png");    ulBaby->setPosition(ccp(470, 320));    ulBaby->setScale(0.4);    this->addChild(ulBaby);    CCArray * animFrames=CCArray::createWithCapacity(6);    char str[100]={0};    for (int i=1; i<9; i++) {        sprintf(str, "UL%d.png",i);        CCSpriteFrame * frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);        animFrames->addObject(frame);    }    CCAnimation * animation=CCAnimation::createWithSpriteFrames(animFrames,0.18f);    animation->setLoops(-1);    ulBaby->runAction(CCAnimate::create(animation));        ulBaby->runAction(CCAnimate::create(animation));    CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrameByName("UL.plist");        CCActionInterval * move = CCMoveTo::create(4, ccp(30, 100));    ulBaby->runAction(move);

这是段小男孩奔跑的动画,留着给大家玩吧,促进学习兴趣的。



详细内容和具体使用方法后面还会陆续跟上。

原创粉丝点击