【问题标题】:Handling Double Tap in Cocos2d在 Cocos2d 中处理双击
【发布时间】:2011-10-14 23:26:55
【问题描述】:

这个问题太原始了,但我在过去的 8 个小时里一直在尝试,它正在消耗我的精力(以及信心水平:))

在我的课堂上,我已经注册了 Targeted touches。

  [[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。此操作无效。没有添加任何精灵。 (虽然该方法被调用。)。实际上这是可行的,如果我没有延迟地调用选择器,但有延迟,则不会添加精灵。

我不是 Objective 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];


}

【问题讨论】:

  • 可以发一下handleSingleTouch: 方法

标签: objective-c ios cocos2d-iphone


【解决方案1】:

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

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

编辑

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

希望对你有帮助

【讨论】:

  • 在调用延迟方法之前,您是否尝试过保留触摸?我还没有看到你的代码在哪里添加了精灵,但如果它取决于触摸的值,那么可能在调用延迟方法之前释放了触摸。
  • 我想,你可能正在做点什么。我从来没想过那个。会尝试让您知道。
  • 您能否将其添加为答案。我会接受的。问题在于触摸对象,迷路了。我认为它被保留了,但它被覆盖或类似的东西。我会进一步调查。但既然你给我指出了正确的方向:),你就得到了荣誉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多