0

我真的很想弄清楚为什么这段代码会导致对触摸输入的间歇性响应......即使将 NSLog 作为第一条指令......

我刚刚在 cocos2d 中使用 Box2d 制作了一个新项目,该项目在这个阶段只需要一些简单的东西......

出现在屏幕中央的篮子。它必须是一个b2Fixture并且落在一个表面上。然后,如果用户触摸屏幕,我希望篮子缩放到触摸点,然后用户可以从那里在屏幕上拖动它。

当用户放手时,篮子会掉下来......我现在有这个工作......

然而BUG是触摸屏幕并不总是有效......它间歇性地响应触摸,因此间歇性地调用方法。

正如您将在下面看到的,我曾经NSLog检查过每个方法何时被调用。结果是有时您必须将手指从屏幕上移开然后再打开几次,然后“看似随意”,它将决定运行代码....

这是我得到的...

我的触摸方法......

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Multi Touch Moved...");
    if (_mouseJoint == NULL) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);

    _mouseJoint->SetTarget(locationWorld);
}

-(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"\nThe touch was CANCELED...");
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"Single Touch Moved...");
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);

    NSLog(@"\n\nTouch did begin...");

    if (_mouseJoint != NULL)
    {
        _mouseJoint->SetTarget(locationWorld);
        NSLog(@"The IF statment was met...");
        return;
    }

    NSLog(@"The IF statment was NOT met...Running _mouseJoint setup...");

    b2MouseJointDef md;
    md.bodyA = _groundBody;
    md.bodyB = _body;
    md.target = _body->GetPosition();
    md.collideConnected = true;
    md.maxForce = 100000000.0f * _body->GetMass();
    _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
    _body->SetAwake(true);

    _mouseJoint->SetTarget(locationWorld);
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_mouseJoint != nil) {
        _world->DestroyJoint(_mouseJoint);
        _mouseJoint = nil;
    }
}

这是一些代码引用的接口......

@interface HelloWorldLayer : CCLayerColor
{
b2World *_world;
    b2Body *_body;
    b2Fixture *_bodyFix;
    b2MouseJoint *_mouseJoint;
    b2Body *_groundBody;
}

另一个成员帮助我确定的唯一想法是,因为我在一个场景中工作,并且我在屏幕上有/有一个 CCMenu 和一个 CCLabelTTF,CCMenu 是否可能仍在拦截触摸,如果是这样,动画完成后如何销毁 CCMenu?

按下唯一的按钮项只需 CC 将标题和标签(CCMenuItem)垂直移出屏幕......但该对象仍然存在......

4

1 回答 1

1

这里的问题是我CCMenu仍然在接收触摸(我假设在CCMenuItems哪里形成的屏幕部分)。

CCMenu解决方案是在我的@implementation而不是在我的init场景设置中声明我。

@implementation HelloWorldLayer
{
    CCLabelTTF *label;
    CCLabelTTF *startTheGameLabel;
    CCSprite *theCattery;
    CCMenu *_menu;
}

然后在我的初始化中,而不是在那里声明它,我只是分配给@implementation.

-(id) init { if( (self=[super initWithColor:ccc4(50, 180, 220, 255)]) )
{
    //.. Other "irrelevant to question" scene setup stuff here...//

    // Start the game button
    startTheGameLabel = [CCLabelTTF labelWithString:@"Save Some Kitties!" fontName:@"Zapfino" fontSize:20];
    CCMenuItemLabel *startTheGameLabelItem = [CCMenuItemLabel itemWithLabel:startTheGameLabel target:self selector:@selector(startGameStub:)];

    // Push the menu
    _menu = [CCMenu menuWithItems: startTheGameLabelItem, nil];
    [self addChild:_menu];

    //.. Other "irrelevant to question" scene setup stuff here...//
}

这使我可以访问CCMenu整个班级,因此我可以稍后在用户做出选择后禁用触摸输入。

-(void) startGameStub:(id)sender
{
    CGSize size = [[CCDirector sharedDirector] winSize];

     // Clear the labels off the screen
    CMoveTo *moveTheTitleLabelAction = [CCMoveTo actionWithDuration:1.0 position:ccp(label.position.x, size.height + label.boundingBox.size.height)];
    CCMoveTo *moveTheStartLabelAction = [CCMoveTo actionWithDuration:1.0 position:ccp(startTheGameLabel.position.x, size.height + startTheGameLabel.boundingBox.size.height)];

    // Commit actions
    [label runAction:moveTheTitleLabelAction];
    [startTheGameLabel runAction:moveTheStartLabelAction];

    // LIFE SAVING MESSAGE!!!
    [_menu setTouchEnabled:NO]; // This is what fixes the problem
}
于 2014-05-27T18:41:38.767 回答