How to create button (MenuItemSprite) with custom method for creating Sprites

How to create button (MenuItemSprite) with custom method for creating Sprites


Hello everybody, I am running Cocos2d-x 2.2.2 on Win 7 in Eclipse. And I am new to Cocos2d-x, I’m following Lynda tutorial about making games for Android, and I am making one, but now I stuck.

I want to create button MenuItemSprite and I wrote function to create buttons, but after button is created it can’t be pressed, it only appears on screen. I don’t know why menu_selector isn’t starting…

I have got this in HelloCpp (in my project it’s called GlowneMenu):

    CCMenuItemSprite *howToPlayPrzycisk = CCMenuItemSprite::create(PrzyciskMenu::buttonWithText("HOW TO PLAY", (Tools::getArtScale() > 1)), NULL, this, menu_selector(GlowneMenu::menuCloseCallback));
    CCMenu *menu = CCMenu::create(howToPlayPrzycisk,NULL);
    menu->setPosition(ccp(s.width/2, origin.y + s.height/2));
    this->addChild(menu, 4);

and in my PrzyciskMenu.cpp - class that creates buttons I’ve got two methods:

#include "PrzyciskMenu.h"
#include "Tools.h"

using namespace cocos2d;

bool PrzyciskMenu::initWithText(const char * PrzyciskTekst, bool czyHD)
    if(!CCSprite::init()) { return false;}

    CCString *PrzyciskHD = (czyHD) ? CCString::create("pusty_przycisk-HD.png") : CCString::create("pusty_przycisk.png");
    int czRozmiar = 20 * Tools::getArtScale();;
    CCSprite *Tlo = CCSprite::create(PrzyciskHD->getCString());
    CCLabelTTF *Tekst = CCLabelTTF::create(PrzyciskTekst, "fonts/exocetlight.ttf", czRozmiar + czRozmiar * czyHD);

    // this->setScale(Tools::getScale());

    return true;

PrzyciskMenu* PrzyciskMenu::buttonWithText(const char * PrzyciskTekst, bool czyHD)
    PrzyciskMenu *nowy = new PrzyciskMenu;
    nowy->initWithText(PrzyciskTekst, czyHD);
    return nowy;

Buttons created by those methods appear on screen, but no interaction with them are possible.

In tutorial from Lynda buttons are created differently:

bool GameButton::initWithText(const char * text, bool isBig)
    if (!CCSprite::init()) {
        return false;
    CCString* btnFrame = (isBig) ? CCString::create("button_big.png") : CCString::create("button_small.png");
    int fSize = 18 * Utils::getArtScaleFactor();
    CCLabelTTF *label = CCLabelTTF::create(text, CCString::createWithFormat("%s.ttf",FONT_MAIN)->getCString(), fSize + isBig * fSize);

    CCLabelTTF *labelShadow = CCLabelTTF::create(text, CCString::createWithFormat("%s.ttf",FONT_MAIN)->getCString(), fSize + isBig * fSize);
    labelShadow->setPosition(ccp(this->getContentSize().width/2 - (2 + isBig * 2),this->getContentSize().height/2));


    return true;

But when I use


and start my apk only black screen is visible, but background music is running.

When I use something like this in GlowneMenu.cpp:

    CCMenuItemSprite *howToPlayPrzycisk0 = CCMenuItemSprite::create(CCSprite::create("pusty_przycisk.png"), PrzyciskMenu::buttonWithText("HOW TO PLAY", (Tools::getArtScale() > 1)), this, menu_selector(GlowneMenu::menuCloseCallback));
    CCMenu *menu0 = CCMenu::create(howToPlayPrzycisk0,NULL);
    menu0->setPosition(ccp(origin.x + s.width/2, origin.y + s.height/2 + 50));
    this->addChild(menu0, 4);

Button is visible, but when I touch it, button created by my method is not in the same spot as first image, maybe some positioning issues? But in this last example menu_selector runs just fine.

Can somebody can help me? I don’t know what I’m doing wrong? How to create buttons with custom method?