I'm trying to create a button that is derived from the base class CCMenuItemImage. I want this button to be able to call it's function when it's first touched instead of after the touch ends. However, trying to subclass, I get an error saying it's an invald conversion.
button.ccp:
#include "button.h"
void Button::selected(){
CCLOG("SELECTED");
}
void Button::unselected(){
CCLOG("UNSELECTED");
}
Button* Button::create(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector) {
Button *button = new Button();
if (button && button->initWithNormalImage(normalImage, selectedImage, disabledImage, NULL, NULL))
{
button->autorelease();
return button;
}
CC_SAFE_DELETE(button);
return NULL;
}
button.h:
#ifndef BUTTON_H
#define BUTTON_H
#include "cocos2d.h"
class Button : public cocos2d::CCMenuItemImage{
public:
virtual void selected();
virtual void unselected();
};
#endif
SinglePlayer.ccp piece:
Button *left1 = Button::create("turncircle.png","turncircle.png", this, menu_selector(SinglePlayer::turning));
MenuItem select() is triggered on touch finished by default.
You need to subclass CCSprite with Touch registered with dispatcher and overwrite the ccTouchBegan
What I can understand is that you are trying to do manual control with the touches of your CCMenuItemImage. Actually all the touches are being handled in CCMenu not in MenuItem so you have to inherit CCMenu rather CCMenuItemImage to override the touches.
In my game I had this problem with CCTableView and CCMenuItem where MenuItem was a prioritised in taking gestures. So I tweaked it with inheriting CCMenu.
It also contain some extra code but just to make everything gets intact I am pasting everything.
ScrollMenu.h Class
class ScrollMenu:public CCMenu
{
public:
ScrollMenu();
virtual ~ScrollMenu(){};
bool isMovedGesture_;
bool istabBar_;
CCMenuItem * previousSelectedItem_;
static ScrollMenu* create(CCMenuItem* item,...);
virtual void registerWithTouchDispatcher();
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch *touch, CCEvent* event);
CREATE_FUNC(ScrollMenu);
};
class ScrollMenuLoader : public cocos2d::extension::CCNodeLoader
{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(ScrollMenuLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(ScrollMenu);
};
ScrollMenu.cpp Class
#include "ScrollMenu.h"
ScrollMenu* ScrollMenu::create(CCMenuItem* item, ...)
{
va_list args;
va_start(args, item);
ScrollMenu *pRet = new ScrollMenu();
if (pRet && pRet->initWithItems(item,args))
{
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
ScrollMenu::ScrollMenu()
{
isMovedGesture_ = false;
}
void ScrollMenu::registerWithTouchDispatcher()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
}
bool ScrollMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
return CCMenu::ccTouchBegan(touch, event);
}
void ScrollMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
isMovedGesture_ = true;
}
void ScrollMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
CCMenuItem * currentItem = this->itemForTouch(touch);
if(!currentItem && isMovedGesture_ && m_pSelectedItem)
{
if(!istabBar_ || (previousSelectedItem_ && previousSelectedItem_ != m_pSelectedItem))
{
m_pSelectedItem->unselected();
}
}
else if(currentItem)
{
if(currentItem == m_pSelectedItem)
{
if(!isMovedGesture_)
{
m_pSelectedItem->activate();
previousSelectedItem_ = m_pSelectedItem;
}
else{
if(previousSelectedItem_ != m_pSelectedItem)
{
m_pSelectedItem->unselected();
}
}
}
else
{
if(isMovedGesture_)
{
m_pSelectedItem->unselected();
m_pSelectedItem = currentItem;
m_pSelectedItem->activate();
previousSelectedItem_ = m_pSelectedItem;
}
}
if (!istabBar_) {
currentItem->unselected();
}
}
m_eState = kCCMenuStateWaiting;
isMovedGesture_ = false;
}
Related
I'm working on a TreeViewNode class derived from QObject and QGraphicsItem, responsible for creating the individual nodes of the tree view (which displays a family tree) and adding them to the scene. I made sure to include the setFlag(QGraphicsItem::ItemIsSelectable); method in the class constructor and I overloaded mousePressEvent like so:
void TreeViewNode::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
qDebug() << "Mouse pressed on node: " << m_node->getPatient()->get_Name().c_str();
emit clicked(this);
}
QGraphicsItem::mousePressEvent(event);
}
The clicked signal is defined in the header file as void clicked(TreeViewNode* node);
Then I have a updateSelectedPatient slot in MainWindow
void MainWindow::updateSelectedPatient(TreeViewNode* node) {
selected->setSelectedPatient(node->getNode()->getPatient());
}
and the corresponding connect statement in the MainWindow constructor
connect(treeView, &TreeViewNode::clicked, this, &MainWindow::updateSelectedPatient);
When I execute I see the nodes that have been added to the scene in the view but when I click on them nothing happens (I know through debugging and also because the setSelectedPatient() method updates a widget that shows the patient information of the currently selected patient); it seems like mousePressEvent() is not being called at all.. any help would be really appreciated, thank you for your time!
edit:
//TreeViewNode.hpp
class TreeViewNode : public QObject, public QGraphicsItem {
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
private:
node* m_node;
Family_tree* m_family;
QGraphicsScene* m_scene;
static std::set<node*> addedNodes;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
public:
TreeViewNode(node* node, Family_tree* family, QGraphicsScene* scene, QGraphicsItem* parent = nullptr);
TreeViewNode* getTreeViewNode(node* n);
node* getNode() const;
static void clearAddedNodes();
void updateNode(Patient& patient);
QRectF boundingRect() const override;
void drawBranches(QPainter* painter);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
signals:
void clicked(TreeViewNode* node);
};
//TreeViewNode.cpp
TreeViewNode::TreeViewNode(node* node, Family_tree* family, QGraphicsScene* scene, QGraphicsItem* parent) : QGraphicsItem(parent), m_node(node), m_family(family), m_scene(scene) {
if (m_node == m_family->get_root()) {m_scene->addItem(this);}
addedNodes.insert(m_node);
setFlag(QGraphicsItem::ItemIsSelectable);
qDebug() << "Scene item count: " << m_scene->items().count();
if (m_node->getFather()) {
auto father = m_node->getFather();
if (addedNodes.count(father) > 0) {return;}
TreeViewNode* fatherNode = new TreeViewNode(m_node->getFather(), family, m_scene, this);
fatherNode->setPos(-50, -100);
}
if (m_node->getMother()) {
auto mother = m_node->getMother();
if (addedNodes.count(mother) > 0) {return;}
TreeViewNode* motherNode = new TreeViewNode(m_node->getMother(), family, m_scene, this);
motherNode->setPos(50, -100);
}
if (m_node->getSpouse()) {
auto spouse = m_node->getSpouse();
if (addedNodes.count(spouse) > 0) {return;}
TreeViewNode* spouseNode = new TreeViewNode(m_node->getSpouse(), family, m_scene, this);
spouseNode->setPos(100, 0);
}
int childCount = 0;
for (auto child : m_node->getChildren()) {
if (addedNodes.count(child) > 0) {return;}
TreeViewNode* childNode = new TreeViewNode(child, family, m_scene, this);
childCount++;
double xPos, yPos;
xPos = pos().x() + 50 + 100 * (childCount - (m_node->getChildren().size() + 1) / 2.0);
yPos = pos().y() + 100;
childNode->setPos(xPos, yPos);
}
}
You could use a normal event filter and check if is it a mouse press, release or move event then cast it to a mouse event and then just do whatever you want with it.
Example:
bool YourWidgetClass::eventFilter(QObject* object, QEvent* event)
{
if (object == YourInstance)
{
switch (event->type())
{
case QEvent::GraphicsSceneMousePress:
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent*>(event);
switch (mouseEvent->button())
{
case Qt::LeftButton:
// Handle left button pressed here
return true;
case Qt::RightButton:
// Handle right button pressed here
return true;
default:
break;
}
break;
}
}
}
Don't forget to install the event filter on your class
YourClassCtor->installEventFilter(this)
I don't see from your question that you need it for anything special. the signal &QGraphicsScene::selectionChanged is not enough for you? Is the item set to interactive?
So simply put:
declare a slot:
public slots:
void selectionChanged();
define the slot:
void MainWindow::selectionChanged()
{
qDebug() << ...... ;
}
and connect:
connect(scene,&QGraphicsScene::selectionChanged,this,&MainWindow::selectionChanged);
I have a CTaskBarIcon *m_pTaskbar member variable in myApp class.
That variable will take an instance of CTaskBarIcon object.
When deleting the m_pTaskbar from within onExit method a runtime error occurs when quitting the program, and cause of the problem is to delete m_pTaskbar variable.
app.h
#include "taskBarIcon.h"
class myApp: public wxApp{
public:
// ....
private:
CTaskBarIcon *m_pTaskbar; // = NULL
};
app.cpp
int myApp::OnExit() {
if (m_pTaskbar != NULL) {
delete m_pTaskbar; // <-- The problem here
m_pTaskbar = NULL;
}
return 0;
}
int myApp::OnRun() {
mainFrm *_mainFrm = mainFrm::getInstance(); // The main window
_mainFrm->Show(false);
m_pTaskbar = new CTaskBarIcon(_mainFrm);
m_pTaskbar->SetIcon(wxIcon("appIcon"), _mainFrm->GetTitle());
return wxApp::OnRun();
}
CTaskBarIcon.cpp
CTaskBarIcon::CTaskBarIcon(mainFrm *handler) : m_pHandler(handler), m_pMenu(NULL) {
this->Bind(wxEVT_TASKBAR_CLICK, &CTaskBarIcon::rightButton_Click, this);
}
CTaskBarIcon::~CTaskBarIcon() {
if (m_pMenu != NULL) {
delete m_pMenu;
m_pMenu = NULL;
}
}
void CTaskBarIcon::rightButton_Click(wxTaskBarIconEvent & event) {
PopupMenu(CreatePopupMenu());
}
void CTaskBarIcon::popupMenu_showWindow(wxCommandEvent & event) {
m_pHandler->Iconize(false); // 'm_pHandler' is the main window
m_pHandler->Show();
m_pHandler->Raise();
this->Destroy();
}
void CTaskBarIcon::popupMenu_showAbout(wxCommandEvent & event) {
aboutFrm aboutWindow(m_pHandler, "About");
aboutWindow.ShowModal();
aboutWindow.Raise();
}
void CTaskBarIcon::popupMenu_exit(wxCommandEvent & event) {
m_pHandler->Close(true);
this->Destroy();
}
wxMenu* CTaskBarIcon::CreatePopupMenu() {
this->m_pMenu = new wxMenu();
m_pMenu->Append(wxID_SHOW, "&Show");
m_pMenu->Append(wxID_ABOUT, "&About");
m_pMenu->Append(wxID_EXIT, "E&xit");
m_pMenu->Bind(wxEVT_MENU, &CTaskBarIcon::popupMenu_showWindow, this, wxID_SHOW);
m_pMenu->Bind(wxEVT_MENU, &CTaskBarIcon::popupMenu_showAbout, this, wxID_ABOUT);
m_pMenu->Bind(wxEVT_MENU, &CTaskBarIcon::popupMenu_exit, this, wxID_EXIT);
return this->m_pMenu;
}
Is there a problem with my code?
There might be a problem with deleting a wxTaskBarIcon from its own event handler. Can you try if using CallAfter([this] { Destroy(); }) instead of just calling Destroy() directly fixes the problem?
Briefly, I have two sprites, one of which is a child of another.
With each sprite related the event listener, as described there.
If both sprites are child nodes of the layer, then everything works great.
Now I need to second sprite is a child node of the first sprite.
But in this case, the second sprite does not respond to events in general.
I'm in a panic and have no idea what's wrong and how to fix it. Please help.
Here's a simplified example:
.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
class PlaySprite : public cocos2d::Sprite
{
public:
virtual bool init();
CREATE_FUNC(PlaySprite);
void addEvent();
bool touchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
};
class InPlaySprite : public cocos2d::Sprite
{
public:
virtual bool init();
CREATE_FUNC(InPlaySprite);
void addEvent();
bool touchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
};
#endif // __HELLOWORLD_SCENE_H__
.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
auto sprite1 = PlaySprite::create();
this->addChild(sprite1);
auto sprite2 = InPlaySprite::create();
// !!!
sprite1->addChild(sprite2);
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
}
bool PlaySprite::init()
{
if(!Sprite::init())
return false;
// to do
Size visibleSize = Director::getInstance()->getVisibleSize();
this->setTexture("1.png");
this->setPosition(visibleSize.width / 2, visibleSize.height / 2);
this->addEvent();
return true;
}
void PlaySprite::addEvent()
{
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [=](Touch* touch, Event* event)
{
return this->touchBegan(touch, event);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool PlaySprite::touchBegan(Touch* touch, Event* event)
{
auto target = (Sprite* ) event->getCurrentTarget();
auto rect = this->getBoundingBox();
if(rect.containsPoint(touch->getLocation()))
{
this->setTexture("1d.png");
return true;
}
return false;
}
bool InPlaySprite::init()
{
if(!Sprite::init())
return false;
// to do
Size visibleSize = Director::getInstance()->getVisibleSize();
this->setTexture("2.png");
this->setPosition(150.0f, 150.0f);
this->addEvent();
return true;
}
void InPlaySprite::addEvent()
{
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [=](Touch* touch, Event* event)
{
return this->touchBegan(touch, event);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool InPlaySprite::touchBegan(Touch* touch, Event* event)
{
// auto target = (Sprite* ) event->getCurrentTarget();
auto rect = this->getBoundingBox();
if(rect.containsPoint(touch->getLocation()))
{
this->setTexture("2d.png");
return true;
}
return false;
}
Classes PlaySprite and InPlaySprite are are very similar because this is a very simplified example.
I think your problem is that you are swallowing touch events in the parent sprite when you call
listener->setSwallowTouches(true);
If you make that call inside of PlaySprite::addEvent() with false instead, I suspect things will work out for you.
can't change the color of a background i have this simple class :
here is the c++ file :
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{
;
}
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor(Color4B(20,0,0,255)) )
{
return false;
}
winSize = Director::getInstance()->getWinSize();
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
schedule( schedule_selector(HelloWorld::tick) );
return true;
}
void HelloWorld::onExit()
{
LayerColor::onExit();
}
void HelloWorld::onEnter()
{
LayerColor::onEnter();
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("interface/sprites.plist", "interface/sprites.png");
SpriteBatchNode* batch = SpriteBatchNode::create("interface/sprites.png");
this->addChild(batch,BATCH_Z);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
startAnim = true;
};
}
void HelloWorld::tick(float dt)
{
if(startAnim)
{
}
}
void HelloWorld::draw()
{
LayerColor::draw();
}
HelloWorld::~HelloWorld()
{
// Removes Touch Event Listener
_eventDispatcher->removeEventListener(_touchListener);
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
and the h file :
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class GameObj;
class ReelGameObj;
class HelloWorld : public cocos2d::LayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void tick(float dt);
virtual void draw();
virtual void onEnter();
virtual void onExit();
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
protected:
cocos2d::CustomCommand _customCommand;
void onDraw();
private:
cocos2d::EventListenerTouchOneByOne* _touchListener;
cocos2d::Size winSize;
cocos2d::Size visibleSize;
cocos2d::Point origin;
GameObj* pMainWindowObjCenter;
ReelGameObj* pReelGameObj;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
and nothing no color in the background , why ?
im working on windows with VC 2012
I think your color is too dark. Try changing the values to (255,25,255,255) and check the result.
I created a sample project (in Beta 2), and only changed these lines:
.h:
class HelloWorld : public cocos2d::LayerColor
.cpp:
if( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
The result is a white background.
I cleaned up and updated your code (I use cocos2dx 2.2.1) and I could change the layer's color to red.
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
virtual void onEnter();
void draw();
private:
cocos2d::CCSize winSize;
cocos2d::CCSize visibleSize;
cocos2d::CCPoint origin;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
The cpp file
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{}
HelloWorld::~HelloWorld()
{}
CCScene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = CCScene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
ccColor4B c = ccc4(255,0,0,255);
if ( !CCLayerColor::initWithColor(c) )
{
return false;
}
return true;
}
void HelloWorld::onEnter()
{
CCLayerColor::onEnter();
}
void HelloWorld::draw()
{
CCLayerColor::draw();
}
So I'm trying to create a simple app using cocos2d-x newest build and for some reason can't get my touch wired up. Here are my classes:
class GameLayer : public cocos2d::Layer
{
public:
static cocos2d::Layer* createLayer();
void update(float dt);
virtual bool init();
CREATE_FUNC(GameLayer);
private:
bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
};
cocos2d::Layer* GameLayer::createLayer()
{
GameLayer *layer = GameLayer::create();
return layer;
}
bool GameLayer::init()
{
if (!cocos2d::Layer::init())
{
return false;
}
this->schedule(schedule_selector(GameLayer::update));
this->setTouchEnabled(true);
return true;
}
void GameLayer::update(float dt)
{
}
bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);
return true;
}
void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event)
{
}
void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event)
{
}
I noticed when I call into the setTouchEnabled call that an internal flag called _running is set to false so it doesn't actually register my touch events. However I can't seem to figure out why that is the case. Am I calling things incorrectly or in the wrong order?
Currently, cocos2dx is going through a major overhauling of the library and many things have changed including Touch registration and propagation. Here is how it works now:
GameLayer.h
class GameLayer : public cocos2d::Layer
{
public:
static cocos2d::Layer* createLayer();
void update(float dt);
virtual bool init();
CREATE_FUNC(GameLayer);
private:
virtual void onEnter();
virtual void onExit();
bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
};
GameLayer.cpp
cocos2d::Layer* GameLayer::createLayer()
{
GameLayer *layer = GameLayer::create();
return layer;
}
bool GameLayer::init()
{
if (!cocos2d::Layer::init())
{
return false;
}
this->schedule(schedule_selector(GameLayer::update));
return true;
}
void GameLayer::onEnter()
{
Layer::onEnter();
// Register Touch Event
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void GameLayer::onExit()
{
// You don't need to unregister listeners here as new API
// removes all linked listeners automatically in CCNode's destructor
// which is the base class for all cocos2d DRAWING classes
Layer::onExit();
}
void GameLayer::update(float dt)
{
}
bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);
return true;
}
void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event)
{
}
void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event)
{
}
Hope it helps!