Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x 3.0 常用对象的创建方式大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x 3.0 中所有对象几乎都可以用create函数来创建,其他的创建方式也是有create函数衍生。

下面来介绍下create函数创建一般对象的方法,省得开发中经常忘记啥的。

1、精灵Sprite的4种创建方式

(1)根据图片资源路径来创建

  1. //根据图片路径来创建
  2. autosprite1=Sprite::create(filepath);
  3. //根据图片路径来创建,并设置要显示的图片大小
  4. autosprite2=Sprite::create(filepath,Rect(0,width,height));

(2)根据plist文件中的frame name创建,图片名称前要加#符号来区分

copy
    //参数:帧名字,framename
  1. autosprite=Sprite::create('#ball.png');
(3)根据缓存plist中的sprite frame来创建,这种用的比较多
copy
    SpriteFrameCache::geTinstance()->addSpriteFramesWithFile("loadingAndHp.plist","loadingAndHp.png");//加载图片资源作为缓存
  1. //从缓存图片中根据图片名字来创建
  2. autoloading_bk=Sprite::createWithSpriteFramename("loading_bk.png");
(4)根据纹理texture创建
copy
    //根据纹理图片来创建
  1. autobatch=SpriteBatchNode::create("Images/grossini_dance_atlas.png",1);//加载缓存纹理
  2. //根据纹理来创建精灵
  3. autosprite=Sprite::createWithTexture(batch->getTexture());
  4. //根据纹理来创建精灵,并设置显示区域大小
  5. autosprite=Sprite::createWithTexture(batch->getTexture(),Rect(x,y,51); font-family:Arial; font-size:14px; line-height:26px">


    2、文字LabelTTF的创建方式

    (1)根据字体、大小等多参数创建

    copy
      autocenter=LabelTTF::create("Hellococos2d-x",//要显示字符串
    1. "PaintBoy",0); BACkground-color:inherit">//字体
    2. 32,//字号
    3. Size(s.width/2,200),0); BACkground-color:inherit">//显示的宽和高
    4. TextHAlignment::CENTER,0); BACkground-color:inherit">//显示的位置,定位
    5. TextVAlignment::TOp);
    (2)根据自定义对象FontDefinition来创建
    copy
      FontDefinitionshadowTextDef;
    1. shadowTextDef._fontSize=20;//字号
    2. shadowTextDef._fontName=std::string("MarkerFelt");//字体
    3. shadowTextDef._shadow._shadowEnabled=true;//设置是否有阴影
    4. shadowTextDef._shadow._shadowOffset=shadowOffset;
    5. shadowTextDef._shadow._shadowOpacity=1.0;//设置透明度
    6. shadowTextDef._shadow._shadowBlur=1.0;
    7. shadowTextDef._fontFillColor=TintColorRed;
    8. //shadowonlylabel
    9. autofontShadow=LabelTTF::createWithFontDefinition("ShadowOnlyRedText",shadowTextDef);//根据自定义的字体创建要显示的内容


    3、对于3.0中SpriteBatchNode,不鼓励使用,文档看这里点击打开链接,在此不再赘述SpriteBatchNode的用法。

    4、帧动画创建方式,通过多张图片文件来创建一个动画

    copy
      Animation*animation=Animation::create();
    1. animation->setDelayPerUnit(1.0/fps);
    2. for(inti=0;i<=count;i++){
    3. constchar*filename=String::createWithFormat(fmt,i)->getCString();
    4. SpriteFrame*frame=SpriteFrameCache::geTinstance()->spriteFrameByName(
    5. fileName);
    6. animation->addSpriteFrame(framE);
    7. }
    Animation是由Sprite frame帧组、单个frame延时,持续时间等组成的,它是一组“数据”,而Animate是一个Action,它是基于Animation对象创建的。

    5、序列帧动画Sprite sheet animation

    (1)通过.plist文件来创建

    copy
      SpriteFrameCache::geTinstance()->addSpriteFramesWithFile(fileName);//加载.plist文件到缓存中
    1. AnimationCache*cache=AnimationCache::geTinstance()->addAnimationsWithFile(fileName);//根据动画.plist文件来创建动画缓存
    2. Animation*animation=cache->animationByName(fileName);//直接从缓存中创建动画
    3. Animate*animate=Animate::create(animation);//生成动画动作

    (2)通过数组来创建

    copy
      Array*animFrames=Array::createWithCapacity(15);
    1. charstr[100]={0};
    2. inti=1;i<15;i++)
    3. {
    4. sprintf(str,"grossini_dance_%02d.png",i);
    5. SpriteFrame*frame=cache->spriteFrameByName(str);
    6. animFrames->addObject(framE);
    7. }
    8. Animation*animation=Animation::createWithSpriteFrames(animFrames,0.3f);

    大佬总结

    以上是大佬教程为你收集整理的cocos2d-x 3.0 常用对象的创建方式全部内容,希望文章能够帮你解决cocos2d-x 3.0 常用对象的创建方式所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。