Skip to content

Instantly share code, notes, and snippets.

@sakura-crowd
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakura-crowd/222c8099d1427be0b905 to your computer and use it in GitHub Desktop.
Save sakura-crowd/222c8099d1427be0b905 to your computer and use it in GitHub Desktop.
var button = mcclib.createImageLabelButton(
function(sender){
window.alert("button pushed");
},
"閉じる", cc.size(50, 30), "Arial", 12, cc.TEXT_ALIGNMENT_CENTER, cc.TEXT_ALIGNMENT_CENTER,
res.gui_button_normal_png, res.gui_button_selected_png, res.gui_button_disabled_png);
this.addChild(button, 10000);
/// 画像を読みこみ指定されたサイズのスプライトを作成します。
/// @param {string} path 画像のファイルパスです。
/// @param {cc.Size} size スプライトのサイズです。画像はこのサイズに拡大縮小されます。
/// @returns {cc.Sprite} 作成したスプライトです。
mcclib.createSprite = function(path, size) {
var sprite = cc.Sprite.create(path);
if (size != undefined) {
var beforeSize = sprite.getContentSize();
sprite.setScale(size.width / beforeSize.width, size.height / beforeSize.height);
sprite.setContentSize(size);
}
return sprite;
};
/// ボタン用の背景画像とラベルを組み合わせたボタンを作成します。
/// @param {function} onButton ボタン押下時のイベントです。
/// @param {string} title ラベルに表示する文字列です。
/// @param {cc.Size} size ボタンの大きさです。 undefined ならば読み込んだ画像のサイズです。
/// @param {string} fontName ラベルに表示する文字列のフォント名です。既定値は Arial です。
/// @param {number} fontSize ラベルに表示する文字列の大きさです。既定値は 12 です。
/// @param {number} hAlign ラベルの文字列の水平方向の寄せ方です。既定値は cc.TEXT_ALIGNMENT_CENTER です。
/// @param {number} vAlign ラベルの文字列の垂直方向の寄せ方です。既定値は cc.TEXT_ALIGNMENT_CENTER です。
/// @param {string} normalImagePath ボタンの通常時の画像のファイルパスです。
/// @param {string} selectedImagePath ボタンが押されたときの画像のファイルパスです。
/// @param {string} disabledImagePath ボタンが無効なときの画像のファイルパスです。
/// @returns {cc.Menu} 作成されたボタンです。
/// @example
/// var button = mcclib.createImageLabelButton(
/// function(sender){
/// window.alert("button pushed");
/// },
/// "タイトル", cc.size(200, 100), "Arial", 12, cc.TEXT_ALIGNMENT_CENTER, cc.TEXT_ALIGNMENT_CENTER,
/// res.gui_button_normal_png, res.gui_button_disabled_png, res.gui_button_disabled_png);
/// this.addChild(button);
mcclib.createImageLabelButton = function(onButton, title, size, fontName, fontSize, hAlign, vAlign, normalImagePath, selectedImagePath, disabledImagePath) {
// 引数の調整
fontName = fontName || "Arial";
fontSize = fontSize || 12;
hAlign = hAlign || cc.TEXT_ALIGNMENT_CENTER;
vAlign = vAlign || cc.TEXT_ALIGNMENT_CENTER;
normalImagePath = normalImagePath || res.gui_button_normal_png;
selectedImagePath = selectedImagePath || res.gui_button_selected_png;
disabledImagePath = disabledImagePath || res.gui_button_disabled_png;
// メニューアイテムの作成
var menuitem = new cc.MenuItemSprite();
menuitem.onButton = onButton;
var normalSprite = mcclib.createSprite(normalImagePath, size);
var selectedSprite = mcclib.createSprite(selectedImagePath, size);
var disabledSprite = mcclib.createSprite(disabledImagePath, size);
menuitem.initWithNormalSprite(
normalSprite,
selectedSprite,
disabledSprite,
menuitem.onButton, menuitem);
// ラベルの作成
var label = cc.LabelTTF.create(title, fontName, fontSize, size, hAlign, vAlign);
var menuitemSize = menuitem.getContentSize();
label.setPosition(menuitemSize.width / 2, menuitemSize.height / 2);
label.setContentSize(menuitemSize);
menuitem.addChild(label);
var menu = cc.Menu.create(menuitem);
return menu;
};
@sakura-crowd
Copy link
Author

cocos2d-js 3.0 rc1 のフレームワークを使っています。
ブログに詳細を記述しています。
http://sakuracrowd.hatenablog.com/entry/2014/09/01/200250

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment