Answer the question
In order to leave comments, you need to log in
cocos2d-x button not working?
Good afternoon!
Started learning cocos2d-x. I'm trying to add a button and hang up a function on pressing, but nothing works.
#include "HelloWorldScene.h"
#include "MenuLay.h"
#include "ui/CocosGUI.h"
#include <iostream>
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
void HelloWorld::addSprite() {
auto myspr = Sprite::create("mario.png");
this->addChild(myspr);
}
bool HelloWorld::init()
{
if (!Layer::init())
{
return false;
}
auto button = ui::Button::create("CloseNormal.png", "CloseSelected.png", "CloseSelected.png");
button->setTitleText("Spawn");
button->setPosition(Vec2(100,100));
button->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type)
{
switch (type)
{
case cocos2d::ui::Widget::TouchEventType::BEGAN:
break;
case cocos2d::ui::Widget::TouchEventType::MOVED:
break;
case cocos2d::ui::Widget::TouchEventType::ENDED:
addSprite();
break;
case cocos2d::ui::Widget::TouchEventType::CANCELED:
break;
default:
break;
}
});
this->addChild(button);
return true;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question