0

このコードがタッチ入力に対して断続的な応答を引き起こす理由を突き止めたいと思います...最初の命令としてNSLogを使用しても...

この段階では、いくつかの簡単なことだけが必要なゲームのために、Box2d を使用して cocos2d で新しいプロジェクトを作成しました...

画面中央に現れるバスケット。b2Fixtureで、サーフェスに落ちる必要があります。次に、ユーザーが画面に触れた場合、バスケットをタッチポイントにズームし、そこからユーザーが画面上でドラッグできるようにします。

ユーザーが手放すと、バスケットが落ちます...私は今これを機能させています...

ただし、バグは、画面に触れても常に機能するとは限らないことです... 断続的にタッチに応答するため、断続的にメソッドが呼び出されます。

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 を破棄するにはどうすればよいですか?

唯一のボタン項目を押すと、タイトルとラベル (CCMenuItem) が画面から垂直に CCmoves されます... しかし、オブジェクトはまだ存在します...

4

1 に答える 1

1

ここでの問題は、私CCMenuがまだタッチを受け取っていたことです(画面のどこCCMenuItemsで形成されたのかと思います)。

解決策は、シーン設定ではなく、CCMenu自分で宣言することでした。@implementationinit

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

次に、init で、そこで宣言するのではなく、単純に@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 に答える