2017-02-18 5 views
0

내가 만진 카드의 객체를 감지하고 싶습니다. 카드는 코코스 스프라이트를 확장하는 사용자 정의 클래스입니다.어떤 확장 스프레드가 i를 터치했는지 감지

카드의 멤버 메소드를 호출하고 싶습니다. 뭔가 이런 : if (target is Card) target.openCard();

대단히 감사합니다. 나 C++처럼 보이지 않는

메인 클래스 바디

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) 

. 이게 뭐야? : D

첫째로 : 는 표적으로하고 있습니까? 만약 그렇게 :

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

을 어쨌든, 형 카드의 당신이 확신하는 객체의 메소드를 호출 할 경우, 아마 당신은 수행해야합니다

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

을 솔직히 당신이하지 않는 한 실제로 포스트 관련 코드는 누구나 당신을 도울 수 없습니다. 카드는 어떻게 생겼습니까? (상관 없어! XD)