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

cocos2d-iphone – 更改sprite使用的png

发布时间:2020-12-14 18:56:38 所属栏目:百科 来源:网络整理
导读:在cocos2d-x中,如何更改精灵使用的png? 下面的作品,但似乎有点长,我想知道是否有一个替代方案,阻止我不得不打电话给新的? // create sprite with original pngm_pSpr = CCSprite::create( "1.png" );m_pSpr-setPosition( ccp( 100,100 ) );this-addChild(
在cocos2d-x中,如何更改精灵使用的png?

下面的作品,但似乎有点长,我想知道是否有一个替代方案,阻止我不得不打电话给新的?

// create sprite with original png
m_pSpr = CCSprite::create( "1.png" );
m_pSpr->setPosition( ccp( 100,100 ) );
this->addChild( m_pSpr );

// now change the png that is used by the sprite

// new image from png file
CCImage* img = new CCImage();
img->initWithImageFile( "2.png",CCImage::kFmtPng );

// new texture from image
CCTexture2D* tex = new CCTexture2D();
tex->initWithImage( img );

// finally change texture of sprite
m_pSpr->setTexture( tex );

解决方法

将精灵打包到spritesheet中,然后使用CCSprite的setDisplayFrame().

// make sure the spritesheet is cached
auto cacher = CCSpriteFrameCache::sharedSpriteFrameCache();
cacher->addSpriteFramesWithFile("Spritesheet.plist");

// create the sprite
m_pSpr = CCSprite::create( "1.png" );

// set it's display frame to 2.png      
CCSpriteFrame* frame = cacher->spriteFrameByName("2.png");
if( frame)
    m_pSpr->setDisplayFrame(frame);

(编辑:李大同)

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

    推荐文章
      热点阅读