gpt4 book ai didi

cocos2d-iphone - 更改 Sprite 使用的 png

转载 作者:行者123 更新时间:2023-12-04 23:25:59 26 4
gpt4 key购买 nike

在 cocos2d-x 中,如何更改 Sprite 使用的 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 );

最佳答案

将您的 Sprite 打包成 Sprite 表,然后使用 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);

关于cocos2d-iphone - 更改 Sprite 使用的 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com