@iQD @stevetranby @catch_up @Maxxx
Well Im doing something wrong. I made my tile objects using this code:
Vector< Sprite* > vecTable;
Vector< Sprite* > qBox;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//row.push_back(i * j); // Add an element (column) to the row
CCLOG("2d Vector %d", i * j);
_qBox = (CCqBox*)CCqBox::create("questionbutton.png");
//_qBox->onEnter();
_qBox->setPosition(Point( (visibleSize.width/2)-(( 3 * _qBox->boundingBox().size.width/2 )- (_qBox->boundingBox().size.width/2) ) + ( i * (_qBox->boundingBox().size.width * 1.1) - (_qBox->boundingBox().size.width * .1) ) , (visibleSize.height * .6) + (_qBox->boundingBox().size.height + 10 ) * j ));
addChild(_qBox,101);
qBox.pushBack(_qBox);
}
vecTable.pushBack(qBox); // Add the row to the main vector
}
the result:
so far so good. Now I want to attach touch Events in the âclass itselfâ not in âHelloWorld.cppâ
Hereâs What my class.cpp looks like:
CCqBox.cpp
void CCqBox::onEnter()
{
CCqBox::onEnter();
CCLOG(âCCqBox onEnter()â);
// Register Touch Event
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(CCqBox::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(CCqBox::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(CCqBox::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void CCqBox::onExit()
{
// auto director = Director::getInstance();
// director->getTouchDispatcher()->removeDelegate(this);
Sprite::onExit();
}
bool CCqBox::containsTouchLocation(Touch* touch)
{
Size s = this->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
return rect.containsPoint(convertTouchToNodeSpaceAR(touch));
}
bool CCqBox::onTouchBegan(Touch* touch, Event* event)
{
CCLOG(âPaddle::onTouchBegan id = %d, x = %f, y = %fâ, touch->getID(), touch->getLocation().x, touch->getLocation().y);
//if (_state != kPaddleStateUngrabbed) return false;
// if ( !containsTouchLocation(touch) ) return false;
// _state = kPaddleStateGrabbed;
CCLOG(âreturn trueâ);
return true;
}
void CCqBox::onTouchMoved(Touch* touch, Event* event)
{
// If it werenât for the TouchDispatcher, you would need to keep a reference
// to the touch from touchBegan and check that the current touch is the same
// as that one.
// Actually, it would be even more complicated since in the Cocos dispatcher
// you get Sets instead of 1 UITouch, so youâd need to loop through the set
// in each touchXXX method.
CCLOG("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
// CCASSERT(_state == kPaddleStateGrabbed, âPaddle - Unexpected state!â);
auto touchPoint = touch->getLocation();
setPosition( Vec2(touchPoint.x, getPosition().y) );
}
void CCqBox::onTouchEnded(Touch* touch, Event* event)
{
//CCASSERT(_state == kPaddleStateGrabbed, âPaddle - Unexpected state!â);
// _state = kPaddleStateUngrabbed;
}
When I pressed those tiles (displayed above) , no TOUCH EVENT! How do I solve this ? HelppP