检测我触摸的延长弹簧

Detecting which extended sprited i touched

本文关键字:触摸 检测      更新时间:2023-10-16

我想检测我触摸的卡的对象。卡是自定义类,它扩展了可可精灵。

我想在卡上调用成员方法。类似的东西:如果(目标是卡)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;
}
(target is Card)

对我来说看起来不像C 。它是什么 ?:D

首先:目标是指针吗?如果是这样:

target->openCard(); // instead of target.openCard();

无论如何,如果要在确定的对象上调用类型卡的方法,也许应该这样做:

Card* myCard = static_cast<Card*>(target);
myCard->openCard();

说实话,除非您实际发布相关代码,否则任何人都很难为您提供帮助。卡甚至看起来像什么?(我不在乎!XD)