我想检测我触摸的卡的哪个对象。卡是自定义类,它扩展了cocos Sprite。
我想在卡片上调用成员方法。这样的事情:if(target is Card)target.openCard();
非常感谢你。
主类体
bool HelloWorld::init()
{
... some init code, generating card arrays, shuffling
// draw memory cards
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
auto card = Card::createCard();
card->customInit(cardsPictures[count]);
this->addChild(card);
card->setPosition(100 + i*100, 600 - j*100);
count++;
}
}
// register event listener
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
touchListener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
return true;
}
bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
auto target = event->getCurrentTarget();
if (target is Card) target.openCard(); // not working
return true;
}
答案 0 :(得分:0)
(target is Card)
对我来说,这看起来不像C ++。它是什么 ? :d
第一: 目标指针?如果是这样的话:
target->openCard(); // instead of target.openCard();
无论如何,如果你想在一个对象上调用你是某种类型卡的方法,也许你应该这样做:
Card* myCard = static_cast<Card*>(target);
myCard->openCard();
说实话,除非您实际发布相关代码,否则任何人都很难帮助您。 Card甚至看起来像什么? (我不在乎!XD)