Cocos2dx/EditBox
提供: 初心者エンジニアの簡易メモ
目次
EditBoxとは
入力欄のこと
サンプル
cocos2d::ui::EditBox* _editPassword = ui::EditBox::create(Size(visibleSize.width/3, 30), "extensions/orange_edit.png"); _editPassword->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2)); _editPassword->setFont("American Typewriter", 16); _editPassword->setFontColor(Color3B::GREEN); _editPassword->setPlaceHolder("Password:"); _editPassword->setMaxLength(6); _editPassword->setInputFlag(ui::EditBox::InputFlag::PASSWORD); _editPassword->setInputMode(ui::EditBox::InputMode::SINGLE_LINE); _editPassword->setText("初期文字"); addChild(_editPassword);
文字取得
_editPassword->getText();
イベント付きサンプル
HelloworldScene.h
#include "ui/CocosGUI.h" class HelloWorld : public cocos2d::Layer, cocos2d::ui::EditBoxDelegate { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(HelloWorld); private: virtual void editBoxEditingDidBegin(cocos2d::ui::EditBox* editBox); virtual void editBoxEditingDidEnd(cocos2d::ui::EditBox* editBox); virtual void editBoxTextChanged(cocos2d::ui::EditBox* editBox, const std::string& text); virtual void editBoxReturn(cocos2d::ui::EditBox* editBox); };
HelloworldScene.cpp
USING_NS_CC; using namespace ui; // 略 bool HelloWorld::init() { // 略 cocos2d::ui::EditBox* _editPassword = ui::EditBox::create(Size(visibleSize.width/3, 30), "extensions/orange_edit.png"); _editPassword->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2)); _editPassword->setFont("American Typewriter", 16); _editPassword->setFontColor(Color3B::GREEN); _editPassword->setPlaceHolder("Password:"); _editPassword->setMaxLength(6); _editPassword->setInputFlag(ui::EditBox::InputFlag::PASSWORD); _editPassword->setInputMode(ui::EditBox::InputMode::SINGLE_LINE); _editPassword->setDelegate(this); addChild(_editPassword); return true; } void HelloWorld::editBoxEditingDidBegin(EditBox *editBox) { } void HelloWorld::editBoxEditingDidEnd(EditBox *editBox) { } void HelloWorld::editBoxTextChanged(EditBox *editBox, const std::string& text) { } void HelloWorld::editBoxReturn(EditBox *editBox) { }
参考:http://falstar.hateblo.jp/entry/2016/07/18/213355
イベントを取得したいときのみEditBoxDelegateとsetDelegate(this)を追加する
Extensionは非推奨
cocos2d-x ui::EditBoxを使う
http://falstar.hateblo.jp/entry/2016/07/18/213355
数字のみに限定する
setInputMode(EditBox::InputMode::NUMERIC);
他色々ある
ANY The user is allowed to enter any text, including line breaks. 日本語&改行も含む EMAIL_ADDRESS The user is allowed to enter an e-mail address. NUMERIC The user is allowed to enter an integer value. 端末によってはカンマなどを許容する PHONE_NUMBER The user is allowed to enter a phone number. URL The user is allowed to enter a URL. DECIMAL The user is allowed to enter a real number value. This extends kEditBoxInputModeNumeric by allowing a decimal point. SINGLE_LINE The user is allowed to enter any text, except for line breaks.
libMyGame.so エラーが出る場合
cocos2dx-3.17でEditBoxを使って以下エラーがでる場合、Resourcesに指定した画像ファイルがあるか確認する
/data/app/jp.co・・・・/lib/arm/libMyGame.so
(_ZN7cocos2d2ui7EditBox6createERKNS_4SizeEPNS0_12Scale9SpriteES6_S6_+168)