18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CCSpriteFrame内の別画像に切り替える

Posted at

cocos2d-xでCCSpriteFrame を使って初期化したCCSpriteの画像を別のものに変更したい場合、setTextureでは表示が更新されなかったので、調べた結果をメモ。

このような場合、setTextureRect()とsetDisplayFrame()でスプライトフレーム内の参照位置を更新することで切り替えが可能。
特にCCSpriteBatchNodeと組み合わせる場合、同一SpriteFrame内での切り替えになりますので、このやり方が必要になるかも。


CCSprite *sprite;

// SpriteFrameで初期化
sprite = CCSprite::spriteWithSpriteFrameName("hoge.png");



色々な処理



//SpriteFrameの画像を変更する
 
CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("fuga.png");
sprite->setTexture(frame->getTexture());
sprite->setTextureRect(frame->getRect());
sprite->setDisplayFrame(frame);

以上

18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?