Ben*_*Ben 4 cocos2d-iphone cocos2d-x
在cocos2d-x中,如何更改精灵使用的png?
以下作品,但似乎有点长,我想知道是否有一个替代方案,阻止我不得不打电话new?
// 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 );
Run Code Online (Sandbox Code Playgroud)
将精灵打包到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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1753 次 |
| 最近记录: |