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

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

#1

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));
    menu->setScale(Tools::getScale());
    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);
    Tekst->setFontFillColor({209,141,68});
    Tlo->setPosition(ccp(this->getContentSize().width/2,this->getContentSize().height/2));
    Tekst->setPosition(ccp(this->getContentSize().width/2,this->getContentSize().height/2));
    this->addChild(Tlo,1);
    this->addChild(Tekst,1);

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

    return true;
}

PrzyciskMenu* PrzyciskMenu::buttonWithText(const char * PrzyciskTekst, bool czyHD)
{
    PrzyciskMenu *nowy = new PrzyciskMenu;
    nowy->initWithText(PrzyciskTekst, czyHD);
    nowy->autorelease();
    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();
    this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(btnFrame->getCString()));
    CCLabelTTF *label = CCLabelTTF::create(text, CCString::createWithFormat("%s.ttf",FONT_MAIN)->getCString(), fSize + isBig * fSize);
    label->setPosition(ccp(this->getContentSize().width/2,this->getContentSize().height/2));
    this->addChild(label,1);

    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));
    labelShadow->setColor(ccBLACK);
    labelShadow->setOpacity(150);
    this->addChild(labelShadow,0);

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

    return true;
}

But when I use

this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(PrzyciskHD->getCString()));

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));
    menu0->setScale(Tools::getScale());
    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?


#2

BUMP