更改精灵使用的png

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)

Nat*_*iss 5

将精灵打包到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)

  • 是的,纹理只会加载到内存中一次.所有精灵都使用精灵帧,所有精灵帧都有指向加载到内存纹理的指针.几个精灵将使用相同的纹理. (2认同)