2016-12-11 7 views
0

Я пытаюсь построить простую игру с мячом и целью, , и я хочу, чтобы вывести счет, когда мяч касался цели, , но обратный вызов «onContactBegin» не вызывает ,cocos2d - callision callback не вызывает

В кнопке экрана есть цель («цель»), и когда пользователь прикасается к экрану, шар создается и начинает двигаться.

#include <string> 
#include "HelloWorldScene.h" 

#define COCOS2D_DEBUG 1 

USING_NS_CC; 


enum class PhysicsCategory { 
    None = 0, 
    Goal = (1 << 0),  
    Ball = (1 << 1), 
    All = PhysicsCategory::Goal | PhysicsCategory::Ball 
}; 


Scene* HelloWorld::createScene() 
{ 
    auto scene = Scene::create(); 

    auto layer = HelloWorld::create(); 
    scene->addChild(layer); 
    return scene; 
} 

bool HelloWorld::init() 
{ 
    CCLOG("Init"); 
    if (!Layer::init()) 
    { 
     return false; 
    } 

    Size visibleSize = Director::getInstance()->getVisibleSize(); 
    Point origin = Director::getInstance()->getVisibleOrigin(); 

    height = visibleSize.height; 
    width = visibleSize.width; 

    score = 0; 
    m_score_label = Label::createWithTTF(Itos(score), "fonts/Marker Felt.ttf", 24); 
    auto vSize = Director::getInstance()->getVisibleSize(); 

    m_score_label->setPosition(Point(origin.x + vSize.width/2, 
          origin.y + vSize.height - m_score_label->getContentSize().height)); 
    this->addChild(m_score_label, 1); 

    goal = Sprite::create("images.jpg"); 
    goal->setPosition(Point((visibleSize.width/2) + origin.x , (visibleSize.height/6) + origin.y)); 
    auto goalSize = goal->getContentSize(); 
    auto physicsBody = PhysicsBody::createBox(Size(goalSize.width , goalSize.height), 
               PhysicsMaterial(0.1f, 1.0f, 0.0f)); 

    physicsBody->setCategoryBitmask((int)PhysicsCategory::Goal); 
    physicsBody->setCollisionBitmask((int)PhysicsCategory::None); 
    physicsBody->setContactTestBitmask((int)PhysicsCategory::Ball); 
    goal->setPhysicsBody(physicsBody); 

    mySprite = Sprite::create("CloseNormal.png"); 
    mySprite->setPosition(Point((visibleSize.width/2) + origin.x , (visibleSize.height/2) + origin.y)); 

    this->addChild(mySprite); 
    this->addChild(goal); 
    CCScaleBy* action = CCScaleBy::create(3,5); 
    mySprite->runAction(action); 

    auto eventListener = EventListenerTouchOneByOne::create(); 
    eventListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); 
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(eventListener, this); 

    auto contactListener = EventListenerPhysicsContact::create(); 
    contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegan, this); 
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); 

    return true; 
} 

bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event) { 

    cocos2d::Sprite * sp = Sprite::create("CloseNormal.png"); 
    sp->setPosition(Point(touch->getLocation().x , touch->getLocation().y)); 

    MoveTo *action = MoveTo::create(2, Point(width ,0)); 
    auto ballSize = sp->getContentSize(); 
    auto physicsBody = PhysicsBody::createBox(Size(ballSize.width , ballSize.height), 
               PhysicsMaterial(0.1f, 1.0f, 0.0f)); 

physicsBody->setDynamic(true); 
    physicsBody->setCategoryBitmask((int)PhysicsCategory::Ball); 
    physicsBody->setCollisionBitmask((int)PhysicsCategory::None); 
    physicsBody->setContactTestBitmask((int)PhysicsCategory::Goal); 


    sp->setPhysicsBody(physicsBody); 
    sp->runAction(action); 

    this->addChild(sp); 
    return true; 
} 


void HelloWorld::menuCloseCallback(Ref* pSender) 
{ 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) 
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); 
    return; 
#endif 

    Director::getInstance()->end(); 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 
    exit(0); 
#endif 
} 

bool HelloWorld::onContactBegan(PhysicsContact &contact) 
{ 
    CCLOG("onContactBegan"); 
    auto nodeA = contact.getShapeA()->getBody()->getNode(); 
    auto nodeB = contact.getShapeB()->getBody()->getNode(); 

    nodeA->removeFromParent(); 
    nodeB->removeFromParent(); 

    ++score ; 
    m_score_label->setString(Itos(score)); 
    return true; 
} 

std::string HelloWorld::Itos (int num) 
    { 
    std::ostringstream ss; 
    ss << num; 
    return ss.str(); 
    } 

ответ

0

helloworld.cpp

Scene* HelloWorld::createScene() { 

    auto scene = Scene::createWithPhysics(); 
    auto layer = HelloWorld::create(); 

    layer->setPhysicsWorld(scene->getPhysicsWorld()); 

    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); 
    scene->addChild(layer); 

    return scene; 
} 


bool GameScene::didBeginContact(cocos2d::PhysicsContact &contact) { 

    PhysicsBody *bodyA = contact.getShapeA()->getBody(); 
    PhysicsBody *bodyB = contact.getShapeB()->getBody(); 
} 

HelloWorld.h

cocos2d::PhysicsWorld *physicsWorld; 
void setPhysicsWorld(cocos2d::PhysicsWorld *withWorld) {physicsWorld = withWorld;}; 
bool didBeginContact(cocos2d::PhysicsContact &contact); 
+0

Спасибо за ваш ответ, я сожалею, моя проблема заключается в функции " onContactBegin "не" onTouchBegin ". Я редактировал вопрос. –