ios - mouseJoint 不会在 xcode 中抓取对象

标签 ios objective-c xcode box2d

我环顾四周,发现了类似的问题,但是在 Java 环境中(我完全没有准备好)。 我在我的世界中创建了一些物体,现在我正在制作一个鼠标关节来让它们移动,但即使似乎选择了物体并创建了关节,我也根本无法捕获它;

这是我的 .mm 文件中的代码

class myQueryCallback: public b2QueryCallback
{


public:
    myQueryCallback(const b2Vec2& point)
    {
    mouse_p = point;
    body = nil;
}

bool ReportFixture(b2Fixture* fixture)
{


    body = fixture->GetBody();
    body = nil;

    if (fixture->GetBody()->GetType() != b2_staticBody || nil)

    {
        bool inside = fixture->TestPoint(mouse_p);
        if (inside)
        {

            body = fixture->GetBody();
            trackerBody = body;
            if (trackerBody != nil ){touchedCheck = 1;
                //myfunctionhere();
            }
            m_fixture = fixture;
            return false;

        }
    }
    m_fixture = fixture;
    trackerBody = body;
    return true;
}
b2Body* body;
b2Vec2  mouse_p;
b2Fixture* m_fixture;

};

...

/*-(void) registerWithTouchDispatcher
{

    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
}*/ 

我在教程中找到了这部分,但由于它处于事件状态,程序崩溃并退出

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event // grab objects
{

for( UITouch *touch in touches ) {
    CGPoint location = [touch locationInView: [touch view]];

    location = [[CCDirector sharedDirector] convertToGL: location];

    b2Vec2 myTouchPos;
    myTouchPos.Set(location.x/PTM_RATIO, location.y/PTM_RATIO);

    b2AABB aabb;
    b2Vec2 correction;
    correction.Set(0.001f, 0.001f);
    aabb.lowerBound = myTouchPos - correction;
    aabb.upperBound = myTouchPos + correction;

    // Query the world for overlapping shapes.
    myQueryCallback ReportFixture(myTouchPos);
    world->QueryAABB(&ReportFixture, aabb);


    if (ReportFixture.m_fixture)
    {
        b2Body* myBody = ReportFixture.m_fixture->GetBody();
        b2MouseJointDef md;

        md.bodyA = ground1;//currentGround;
        md.bodyB = myBody;
        md.target = myTouchPos;
        md.maxForce = 2000.0f * myBody->GetMass();

        mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
        myBody->SetAwake(true);
        NSValue *mouseJointVal = [NSValue valueWithPointer:mouseJoint];
        NSLog(@"mouseJoint %@",mouseJointVal);

直到这里 NSLog 显示我的指针,但我无法获取我选择的 b2Body

    }

}
}


   - (void)ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
   {
        if(mouseJoint)
    {

    world->DestroyJoint(mouseJoint);
    mouseJoint = nil;
    NSValue *mouseJointVal = [NSValue valueWithPointer:mouseJoint];
    NSLog(@"mouseJoint ended %@",mouseJointVal); 

    }
}

这部分似乎不起作用,NSLog 在控制台中没有显示任何内容

我做错了什么?

在此先感谢您的帮助!

最佳答案

您没有显示 ccTouchesMoved 的代码,但我假设您知道鼠标关节必须在触摸移动时移动其目标位置,否则什么也不会发生。

我不确定问题的确切原因是什么,但我确实看到了一些问题。也许指出它们会有所帮助。

首先是您正在循环访问 ccTouchesBegan 中可能存在的多个接触点。如果多个触摸同时开始,您将创建多个鼠标关节,并且只有最后创建的关节会存储在“mousejoint”变量中。

第二个是,在 ccTouchesEnded 中,当任何 触摸结束时,您正在破坏关节 - 您应该检查触摸是否与创建鼠标关节的触摸相同。

第三个是回调中的 ReportFixture 函数正在为所有报告的灯具设置“m_fixture”成员,无论它们是否是动态的。鼠标关节应该有一个动态主体,因此与其检查主体是否不是静态的(它也可能是运动的),不如检查它 是动态的。

最后,查询回调中“m_fixture”的值未初始化为任何值,因此如果没有报告任何固定装置,您最终将在该指针中使用垃圾数据,并可能在尝试取消引用时崩溃。

我能想到 ccTouchesBegan 中的 NSLog 显示但 ccTouchesEnded 中的 NSLog 不显示的唯一原因是变量“mousejoint”不相同(例如变量阴影)。

我还假设您正在以某种方式渲染触摸位置,以检查您触摸的位置是否确实正确地转换为物理坐标。否则,您的触摸可能会检测到与您认为的不同的固定装置(例如,另一个屏幕外固定装置),这可以解释为什么您看不到任何事情发生。

关于ios - mouseJoint 不会在 xcode 中抓取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24563951/

相关文章:

ios - UiTextfield 中的底部边框

ios - 显示模态视图 Controller 时出现双状态栏

ios - 如何从 objective-c 中的 anchor 使用滑动手势旋转图像?

xcode - 如何更改 Xcode 4 中的构建目录?

ios - 如何在 Swift 中为图像使用 URL?

ruby-on-rails - apn_for_rails gem 实现问题

ios - 使用 plistbuddy 从 plist 中的字典中读取所有键

ios - 从 Assets 列表下载到 ios 7 应用程序的本地存储

objective-c - IBDesignable 在 Objective-C 中不起作用

ios - 在 iOS 6 MKMapView 上显示公交站、餐馆和其他地方