【问题标题】:cocos2d - collision callback doesn't invokecocos2d - 碰撞回调不调用
【发布时间】:2016-12-11 06:58:39
【问题描述】:

我正在尝试用球和目标构建一个简单的游戏, 我想在球碰到目标时增加分数, 但回调“onContactBegin”没有调用。

屏幕按钮中有一个target("goal"),当用户触摸屏幕时,球创建并开始移动。

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

【问题讨论】:

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


    【解决方案1】:

    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);
    

    【讨论】:

    • 感谢您的回复,很抱歉我的问题是函数“onContactBegin”而不是“onTouchBegin”。我编辑了问题。
    猜你喜欢
    • 2012-06-10
    • 2018-10-25
    • 2012-03-04
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多