2

大家好,我是 COCOS2DX 框架的新手,正在尝试开发破砖游戏。我正在关注这个演示Brick breker 游戏本教程在 cocos2d (iPhone) 中。我在 cocos2dx 中编码并正常工作。但是当我正在实施ccTouchesMoved方法时。我的代码可以正确编译和运行,但是当我触摸(单击)屏幕时,它会崩溃并出现错误违规行为。我的代码是

void HelloWorld::ccTouchesMoved(CCSet* touches, CCEvent* event)
{
    if (_mouseJoint == NULL) return;

    CCTouch *myTouch = (CCTouch*)touches->anyObject();
    CCPoint location = myTouch->locationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
    _mouseJoint->SetTarget(locationWorld);
}

我该如何解决这个问题。请指导我。

4

2 回答 2

3
void ClassName::ccTouchesBegan(NSSet *touches, UIEvent* event )
{

      if (_mouseJoint != NULL) return;
      ...
}

可能是您没有将 _mouseJoint 初始化为 NULL。

在你的类的构造函数中,初始化 _mouseJoint = NULL;

于 2012-11-12T15:11:28.117 回答
2

在您提供的演示中,以下代码初始化了 mouseJoint

if (_paddleFixture->TestPoint(locationWorld)) {
    b2MouseJointDef md;
    md.bodyA = _groundBody;
    md.bodyB = _paddleBody;
    md.target = locationWorld;
    md.collideConnected = true;
    md.maxForce = 1000.0f * _paddleBody->GetMass();

    _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
    _paddleBody->SetAwake(true);
}

它在 ccTouchesBegan 中

于 2012-09-06T06:57:44.153 回答