2017-02-18 49 views
0

我想要检测我触摸的哪个对象。卡是扩展cocos Sprite的自定义类。检测我触摸了哪个扩展的spill

我想调用卡上的成员方法。就像这样: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
(target is Card) 

这并不像C++给我。它是什么 ? :D

第一张: 目标指针是?如果是这样做的:

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

无论如何,如果你要调用一个对象,你一定是类型的卡上的方法,也许你应该做的:

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

说实话,除非你真正交相关的代码很难为任何人提供帮助。卡甚至是什么样子? (我不在乎!XD)