微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2d-iphone – 更改sprite使用的png

在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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐