0
votes

This question is too primitive, but I have been trying for the last 8 hours, and it is eating away my energy ( and confidence levels too :))

In my class, I have registered for Targeted touches.

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

I would like to check if the user is performing single tap or double tap. In ccTouchBegan method, first gets a touch with singleTap, then gets a touch with doubleTap. I found a very interesting solution for this one, in one of the Cocos2d Forums. ( I could not locate it now.. )

The solution goes like this.

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;
        }

Everything looks to be working fine. Essentially, you schedule a single touch handler with a delay and see if it is actually a double tap. If it is a double tap, then cancel singletap handler and perform logic for double tap.

But my problem is, In the single touch handler (handleSingleTouch), I am adding a CCSprite to the UI. This action is not working. No sprite gets added. (Although the method is called.). In fact this works, If I call the selector with no delay, but with delay, the sprite is not added.

I am not a Objective C expert, So, I apologize if the question is too primitive.

Edit 1: The original thread.

Edit 2: Posting handleSingleTouch.. only relevant code.

-(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
Can you post the handleSingleTouch: methodKDaker

1 Answers

1
votes

i dont know if thats a typo in your question only but your delay method is all wrong. There is no withDelay: argument. it should look like this:

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

EDIT:

Since your problem is most probably with the touch getting lost. Try [touch retain] before calling the delayed method. Another way is changing the handleSingleTouch: method to take two floats x and y or a CCPoint as arguments instead of a UITouch. Then you create the floats before the delayed method and pass them in the delayed method. This way you will avoid a memory leak from retaining a touch as well since most probably you cant release it after calling the delayed method.

hope this helps