在Cocos2d中处理双击

时间:2011-09-13 17:22:20

标签: objective-c ios cocos2d-iphone

这个问题太原始了,但过去8个小时我一直在努力,它正在吞噬我的能量(以及信心水平:))

在我的课堂上,我已经注册了有针对性的接触。

  [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];

我想检查用户是执行单击还是双击。在ccTouchBegan方法中,首先触摸singleTap,然后触摸doubleTap。我在其中一个Cocos2d论坛中找到了一个非常有趣的解决方案。 (我现在无法找到它。)

解决方案是这样的。

switch (touch.tapCount) {
            case 2:
                [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTouch:)  object:singleTouchLocation];
             // Handle Double Touch here
                break;
            case 1:


                self.singleTapLocation = ccp(location.x,location.y);
                self.singleTouchLocation = touch;

                [self performSelector:@selector(handleSingleTouch:) withObject:touch afterDelay:0.3
             ];



                break;
        }

一切看起来都很好。基本上,您安排一个延迟的单个触摸处理程序,看看它是否实际上是双击。如果是双击,则取消单击处理程序并执行双击逻辑。

但我的问题是,在单点触控处理程序(handleSingleTouch)中,我正在向UI中添加一个CCSprite。此操作无效。没有添加精灵。 (虽然这种方法被称为。)。实际上这是有效的,如果我没有延迟地调用选择器,但是有延迟,则不添加精灵。

我不是客观C专家,所以,如果问题太原始,我道歉。

编辑1:原始thread

编辑2:发布handleSingleTouch ..仅相关代码。

-(void) handleSingleTouch:(UITouch * ) touch  {

    CGPoint location = [touch locationInView: [touch view]];
    CGPoint glLocation = [MainSceneLayer locationFromTouch:touch];
    //Single tap
    CCLOG(@"Single tap: Adding marker image");


    if(zoomedOut) {

        CGPoint globalCoordinates = [quadConvertor convertLocalToGlobal:location 
                                                       inActiveQuadrant:activeQuadrant];



        if( [self isTouchValidForSelectedItem:globalCoordinates] == Matched) {
            [self addMarkerImages:glLocation];
        } else if([self isTouchValidForSelectedItem:globalCoordinates] == NotMatched) {
            [missedLabel stopAllActions];

            for (ImageQuadrants* quad in quadrants) { 
                if(quad.quadrantNumber == activeQuadrant) {

                    missedLabel.position  =  ccp((quad.center.x * -1)+glLocation.x  , (quad.center.y * -1)+glLocation.y);

                    //markers.position =  ccp((quad.center.x * -1)+touchPosition.x  , 320);


                }

            }


            id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
            id makeInvible = [CCCallFunc actionWithTarget:self selector:@selector(makeInvisible:)];



            id seq = [CCSequence actions:blinkAction, makeInvible,  nil];

            [missedLabel runAction:seq];

        } else {

            [alreadyAccountedLabel stopAllActions];

            for (ImageQuadrants* quad in quadrants) { 
                if(quad.quadrantNumber == activeQuadrant) {

                    alreadyAccountedLabel.position  =  ccp((quad.center.x * -1)+glLocation.x  , (quad.center.y * -1)+glLocation.y);

                    //markers.position =  ccp((quad.center.x * -1)+touchPosition.x  , 320);


                }

            }


            id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
            id makeInvible = [CCCallFunc actionWithTarget:self selector:@selector(makeInvisible1:)];



            id seq = [CCSequence actions:blinkAction, makeInvible,  nil];

            [alreadyAccountedLabel runAction:seq];


        }

    }


    swipeStartPoint = [touch locationInView:touch.view];


}

1 个答案:

答案 0 :(得分:1)

我不知道这只是你问题中的错字,但是你的延迟方法都是错误的。没有withDelay:参数。它应该是这样的:

[self performSelector:@selector(handleSingleTouch:) withObject:touch afterDelay:0.1];

修改

因为你的问题很可能是触摸迷失了。在调用延迟方法之前尝试[touch retain]。另一种方法是更改​​handleSingleTouch:方法,将两个浮点数x和y或一个CCPoint作为参数而不是UITouch。然后在延迟方法之前创建浮点数并在延迟方法中传递它们。通过这种方式,您可以避免因保留触摸而导致内存泄漏,因为在调用延迟方法后很可能无法释放它。

希望这会有所帮助