加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x 创建精灵的五种方法

发布时间:2020-12-14 16:52:56 所属栏目:百科 来源:网络整理
导读:?? cocos2d-x游戏开发5中创建精灵方法: 方法一:直接创建精灵 适合于要显示的是这张图片的全部区域, Sprite * sprite = Sprite::create( "Icon.png" ); 上面那句话也可以根据需要这样来写: String* fileName = String::createWithFormat( "Icon_%d.jpg" ,f
??

cocos2d-x游戏开发5中创建精灵方法:


方法一:直接创建精灵

适合于要显示的是这张图片的全部区域,


Sprite * sprite = Sprite::create( "Icon.png" );
上面那句话也可以根据需要这样来写:





String* fileName = String::createWithFormat( "Icon_%d.jpg" ,flag);
Sprite* sprite = Sprite::create(fileName->getCString());
sprite->setPosition(ccp(100,100));
this ->addChild(sprite);

方法二:参数 图片名称 矩形区域

适合于需要显示此图片的部分区域




Sprite * sprite = Sprite::create( "Icon.png" ,RectMake(0,30,30));
sprite->setPosition(ccp(100,100));
this ->addChild(sprite);

方法三: 利用帧缓存中的一帧的名称声称一个对象

适合于plist打包好的文件





SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile( "test_icon.plist" );
Sprite * sprite = Sprite::createWithSpriteFrameName( "Icon.png" );
sprite->setPosition(ccp(100,100));
this ->addChild(sprite);

方法四: 利用另外一帧生成一个精灵对象

适合于做帧动画使用





SpriteFrame * frame = SpriteFrame::create( "Icon.png" ,40,30));
Sprite * sprite = Sprite::createWithSpriteFrame(frame);
sprite->setPosition(ccp(310,150));
addChild(sprite);

方法五:利用纹理

适合于需要频繁使用的图片







SpriteBatchNode* spriteTexture = SpriteBatchNode::create( "iocn.png" );
spriteTexture->setPosition(PointZero);
addChild(spriteTexture);
Sprite* sprite = Sprite::createWithTexture(spriteTexture->getTexture());
sprite->setPosition(ccp(visiblesize.width/2,100));
spriteTexture->addChild(sprite,2);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读