cocos2d-x ios游戏开发初认识(五) CCsprite精灵类

这次写一下精灵创建的几种类型:

一、通过文件创建:

在原有的基础上加入例如以下代码:

    //一、通过文件创建精灵

   CCSprite *bg =CCSprite::create("map.png");

   CCSize winSize  =CCDirector::sharedDirector()->getWinSize(); //得到屏幕的尺寸

    bg->setPosition(ccp(winSize.width/2, winSize.height/2));

   this->addChild(bg);

    return true;


执行:


背景图片被加入进来。。

二、通过帧创建:

    //二、通过帧创建

   CCSpriteFrame *frame =CCSpriteFrame::create("Peashooter1.tiff",CCRectMake(0,0,71,71));

   CCSprite *plant1 =CCSprite::createWithSpriteFrame(frame);

    plant1->setPosition(ccp(400,400));

   this->addChild(plant1);

    return true;

执行:


能够看到在屏幕的草坪上有颗豌豆。。。

三、通过纹理创建:

    //三、通过纹理创建

   CCImage *image =newCCImage();

    image->autorelease();

    image->initWithImageFile("Peashooter1.tiff");

    //创建OpenGL2d纹理图像从图片、文本或原始数据

    CCTexture2D *texture =newCCTexture2D();

    texture->autorelease();

    texture->initWithImage(image);

    

   CCSprite *plant2 =CCSprite::createWithTexture(texture);

    plant2->setPosition(ccp(500,500));

   this->addChild(plant2);

    return true;


能够看到背景图片上多了一个小豌豆。。 

四、通过精灵帧的缓存创建精灵:

创建之前先要下载 Zwoptex 这个软件,它的作用就是将非常多种图片合成一张图片和一个plist文件,打开软件点击creat


导入一些图片:


点击Layout自己主动布局


改变大小:


点击file以下的Publish Settings 来设置输出的路径和文件名称


点击Done 完毕我是保存到桌面,然后将这两个文件加入到project中去。


编写代码:

    //四、通过帧缓存创建

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Person.plist");

    CCSpriteFrame *frameCashe =CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(".png");//创建帧

   CCSprite *plant3 =CCSprite::createWithSpriteFrame(frameCashe);

    plant3->setPosition(ccp(600,600));


也能够这样创建:

CCSprite *plant3 = CCSprite::createWithSpriteFrameByName(".png");


   this->addChild(plant3);

    return true;

这样的方式创建的精灵非经常常使用。。。

执行:


能够看到 多出来一个图片。。。 这就是精灵创建的最主要的创建方法。


原文地址:https://www.cnblogs.com/zfyouxi/p/4262941.html