cocos2d-x学习之(二)有关精灵的一些秘密

来源:互联网 发布:软件测试缺陷生命周期 编辑:程序博客网 时间:2024/04/28 10:14
精灵的创建

CCSprite *sprite = CCSprite::create(“图片名.png”);

CCSpriteBatchNode
优点:CCSpriteBatchNode 中的所有CCSprite只会被渲染1次,因此可以提高游戏的FPS。
限制:加入到 CCSpriteBatchNode 中的CCSprite必须使用同一张纹理图。
问:什么时候应该用CCSpriteBatchNode?
答:比如游戏中的子弹 就很适合用它,因为子弹都是一个样子。
答:通过TexturePacker生成的纹理图也适合使用它。
/*
*  TexturePacker纹理:
*
*/
eg: 
   //创建批次处理对象,并添加到场景里
 CCSpriteBatchNode *batchNode =CCSpriteBatchNode::create("Icon-72.png");
 this->addChild(batchNode);
    
 for (int i =0; i <999; i++) 
{
      CCSprite *sprite =CCSprite::create("Icon-72.png");
      sprite->setPosition(ccp(CCRANDOM_0_1()*480,CCRANDOM_0_1()*320));
      //将精灵添加到batchNode对象
      batchNode->addChild(sprite);
 }

CCSpriteFrameCache缓冲池
  //创建缓冲池
CCSpriteFrameCache *frameCache =CCSpriteFrameCache::sharedSpriteFrameCache();
  //将图片加载到精灵帧缓冲池
frameCache->addSpriteFramesWithFile("大张图片.plist","大张图片.png");
CCSprite *sprite1 =CCSprite::createWithSpriteFrameName("小张图片.png");
sprite1->setPosition(ccp(50,200));
this->addChild(sprite1);
  /*
   *  软件TexturePacker的使用
   *
   */

九妹图片的拉伸

在.h中导入:

#include "cocos-ext.h"

usingnamespacecocos2d::extension;

//九妹拉伸图片
   CCScale9Sprite *btnNormal =CCScale9Sprite::create(“图片源.png");
   CCScale9Sprite *btnDown =CCScale9Sprite::create("图片源.png");
//创建标签
   CCLabelTTF *title =CCLabelTTF::create("Touch Me","Marker Felt",30);
//CCControlButton其实是空壳,用来加载东西的
   CCControlButton *controlButton =CCControlButton::create(title,btnNormal);
//强制设置按钮大小,不然图片会随着Label文字的长度拉伸
   controlButton->setPreferredSize(CCSize(300,50));
//设置按钮按下的图片
   controlButton->setBackgroundSpriteForState(btnDown,CCControlStateSelected);
        controlButton->setPosition(ccp(200,200));
        
//添加点击事件,创建实例方法
   controlButton->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchDown),CCControlEventTouchDown);
在.h中
//创建点击事件的实例方法
private:   void touchDown(CCObject *pSender,CCControlEvent event);


精灵的动画
创建精灵动画的步骤:
         创建一组CCSpriteFrame对象,每张动画图片为一个CCSpriteFrame对象
         用这组CCSpriteFrame对象创建一个CCAnimation对象,该对象包含的动画所需的一些配置信息
         利用CCAnimation对象创建一个CCAnimate对象,精灵直接调用runAction即可执行CCAnimate动画
eg:   
   //创建精灵缓冲池
    CCSpriteFrameCache *frameCache =CCSpriteFrameCache::sharedSpriteFrameCache();
    frameCache->addSpriteFramesWithFile("AnimBear.plist","AnimBear.png");
    int iFrameNum =8;
    CCSpriteFrame *frame =NULL;
    //创建数组接收所有的图片
    CCArray *frameArray =CCArray::create();
    for (int i =0; i < iFrameNum; i++) {
        frame = frameCache->spriteFrameByName(CCString::createWithFormat("bear_0%d.png",i)->getCString());
        frameArray->addObject(frame);
    }

    CCAnimation *animation =CCAnimation::createWithSpriteFrames(frameArray);
    animation->setLoops(-1);         //设置播放的次数
    animation->setDelayPerUnit(0.1f);//设置图片交换速度

     //将动画包装成一个动作
    CCAnimate *acton =CCAnimate::create(animation);
    //调用动作的函数
    runSprite->runAction(createAnimate2());

精灵的移动
//创建MoveTo动作对象,To是绝对移动的,By是相对移动的
CCMoveTo *moveTo =CCMoveTo::create(2.8f,ccp(0,150));
sprite->runAction(moveTo);  //精灵执行动作
//拉伸运动,参数时间,X反向的拉伸,Y反向的拉伸
CCScaleTo *scaleTo =CCScaleTo::create(2.0f,1.5f,2.3f);
sprite->runAction(scaleTo);
//参数时间,每次跳的位置,跳高,跳频率
CCJumpBy *jumpBy =CCJumpBy::create(3.0f,ccp(50,10),100,1);
//设置动作永远重复
CCRepeatForever *repeatForever =CCRepeatForever::create(jumpBy);
//设置动作执行的次数
CCRepeat *repeat =CCRepeat::create(jumpBy,3);
ccBezierConfig bezier;
bezier.controlPoint_1 =ccp(100,200); //波谷偏向值
bezier.controlPoint_2 =ccp(200,50);//波峰偏向值
bezier.endPosition =ccp(300,100);   //动作终点
CCBezierBy *bezierBy =CCBezierBy::create(4.0f, bezier);
 sprite->runAction(bezierBy);
//组合动作,CCSpawn所有动作一起执行,CCSequence是动作一个一个播放
CCAction *action = CCSpawn::create(...,...,..,NULL);
//回调函数
CCCallFunc *callFunc = CCCallFunc::create(this,callfunc_selector(HelloWorld::backFunc));
0 0
原创粉丝点击