LoginSignup
0
0

More than 5 years have passed since last update.

ボタンでスプライトを切り替える

Posted at

ボタン宣言時のλ関数内で、リンク先の方法が使えなかったので試行錯誤した。
(cf. http://bribser.co.jp/blog/changeimage/)

要点は、
・ベクター形式で画像を格納
・setVisibleで表示非表示を切り替え
・ボタン押下時の操作はsetVisibleのみ

心配なのはメモリの食いだが、とりあえずこの方法を保存しておく。
画像切り替えに置けるメモリ消費はないはず。

以下コード

changeSprite.cpp
// 親スプライト
Sprite* prnt = Sprite::create();
// 各画像を子スプライトとして登録
Vector childSpr = Vector();
std::string pngName;
for (int i = 0; i < 4; i++) {
pngName = StringUtils::format("%d.png", i);   //適当に
childSpr.pushBack(Sprite::create(pngName));
// すべて非表示で登録
childSpr.at(i)->setVisible(false);
childSpr.at(i)->setPosition(prntPos); //prntPosは適当な位置、前もって宣言
prnt->addChild(childSpr.at(i));
}
// スコープ番号のスプライトを表示
childSpr.at(_scpNum)->setVisible(true); //スコープ番号はグローバル変数で宣言


//test button
auto chgBtn = MenuItemImage::create("button.png",
"pressed.png",
[this, childSpr](Ref* ref){
childSpr.at(_scpNum)->setVisible(false);
_scpNum++;
if (_scpNum >= SCOPE_PNG_NUM) { //最大画像登録数
_scpNum = 0; //最初に戻る
}
childSpr.at(_scpNum)->setVisible(true);
});
0
0
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
0
0