2017-02-18 50 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)