cocos2d - 不会调用碰撞回调

时间:2016-12-11 06:58:40

标签: cocos2d-iphone cocos2d-x cocos2d-x-3.0

我正在尝试用球和目标建立一个简单的游戏, 当球接触目标时,我想增加得分, 但是回调“onContactBegin”没有调用。

屏幕按钮中有一个目标(“目标”),当用户触摸屏幕时,球会创建并开始移动。

#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();
  }

1 个答案:

答案 0 :(得分: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);