Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17086

Buttons in separate class error

$
0
0

@Faust24 wrote:

Hi there!
I need to display some buttons in every scene I have in the game, so I've crated a class where I have these buttons.

Header:

#ifndef __MAIN_BUTTONS_H__
#define __MAIN_BUTTONS_H__

#include "cocos2d.h"
#include "ui/CocosGUI.h"


class MainButtons
{
public:
    MainButtons();

    cocos2d::ui::Button *FirstButton;
    cocos2d::ui::Button *SecondButton;
    cocos2d::ui::Button *ThirdButton;
    cocos2d::ui::Button *FourthButton;
};

#endif // __MAIN_BUTTONS_H__

Class constructor:

#include "MainButtons.h"
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "PlayerData.h"


USING_NS_CC;


MainButtons::MainButtons() {

    cocos2d::ui::Button *FirstButton = cocos2d::ui::Button::create("FirstButton.png");
    FirstButton->addTouchEventListener([&](Ref *sender, cocos2d::ui::Widget::TouchEventType type){

        switch(type)
        {
            case ui::Widget::TouchEventType::BEGAN:
                break;

            case ui::Widget::TouchEventType::ENDED:
                break;

            default:
                break;
        }

        });
...

Then, I create them in my scene

MainButtons mbTrans;
mbTrans.FirstButton->setPosition(Vec2(1000, 1400));
this->addChild(mbTrans.FirstButton);

I have no problems building it, but when it starts the error appears on the line with setPosition command

EXC_BAD_ACCESS(code=EXC_I386_GPFLT)

If I delete setPosition line, I get this error

CCASSERT(child->_parent == nullptr, "child already added. It can't be added again");

What am I doing wrong or I simply cannot create a separate class for some buttons?

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 17086

Trending Articles