0
votes

My game in cocos2d is handling touches from the user in a way that just get touches, but not slide/drag movements. How can I hlandle slide/drag movements in cocos2d/objective-c?

E.g. if the user slides the finger in any point of the screen above the sprite _player, I want to move the sprite _player up.

This is my function that is handling touches now:

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

    CGPoint touchLocation = [touch locationInNode:self];

    positionY1 = self.contentSize.height*.2;
    positionY2 = self.contentSize.height*.40;
    positionY3 =  self.contentSize.height*.60;


    float distY1Y2 = (positionY1 + positionY2)/2;
    float distY2Y3 = (positionY2 + positionY3)/2;
    float playerMove;


    if (touchLocation.y  <= distY1Y2) {

        if(_player.position.y <= distY1Y2) {
            playerMove = positionY1;
        } else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
            playerMove = positionY1;
        } else if (_player.position.y >= distY2Y3) {
            playerMove = positionY2;
        }

    } else  if (touchLocation.y  > distY1Y2 && touchLocation.y < distY2Y3) {

        if(_player.position.y <= distY1Y2) {
            playerMove = positionY2;
        } else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
            playerMove = positionY2;
        } else if (_player.position.y >= distY2Y3) {
            playerMove = positionY2;
        }


    } else if (touchLocation.y  >= distY2Y3) {
        if(_player.position.y <= distY1Y2) {
            playerMove = positionY2;
        } else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
            playerMove = positionY3;
        } else if (_player.position.y >= distY2Y3) {
            playerMove = positionY3;
        }
    }




    //play audio
    [[OALSimpleAudio sharedInstance] playEffect:@"pew-pew-lei.caf"];

    //Disable another touch movement
    //[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

    // Move our sprite to touch location
    CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:.2f position:CGPointMake(_player.position.x, playerMove)];
    [_player runAction:actionMove];

    //Enable another touch movement

    //[[UIApplication sharedApplication] endIgnoringInteractionEvents];

    /*
    NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(enableTouch:)];
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn];
    [inv setTarget: self];
    [inv setSelector:@selector(enableTouch:)];
    [NSTimer scheduledTimerWithTimeInterval:.7f
                                 invocation:inv
                                    repeats:NO];

     */



}
1

1 Answers

1
votes

I will share some code. Please go through this code.

Your Ques : if the user slides the finger in any point of the screen above the sprite _player, I want to move the sprite _player up.

Ans :

This is for, if you touch the particular sprite and move that sprite......

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
    CGPoint point=[myTouch locationInView:[myTouch view]];
    point=[[CCDirector sharedDirector] convertToGL:point];
    NSLog(@"point is %@",NSStringFromCGPoint(point));
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch=[touches anyObject];
    CGPoint point=[myTouch locationInView:[myTouch view]];
    point=[[CCDirector sharedDirector] convertToGL:point];
    CCNode *Sprite=[self getChildByTag:spriteTag]; // This is your sprite, which one you want to move.
    [Sprite setPosition:point];

   // This is the boundary for sprite, for what sprite is nor cross the screen.
    if(Sprite.position.y>245)
        [Sprite setPosition:ccp(Sprite.position.x,245)];
    if(Sprite.position.y<25)
        [Sprite setPosition:ccp(Sprite.position.x,25)];
    if(Sprite.position.x<15)
        [Sprite setPosition:ccp(15,Sprite.position.y)];
    if(Sprite.position.x>465)
        [Sprite setPosition:ccp(465,Sprite.position.y)];   
}

This is for, if you touch anywhere on screen and move that sprite ......

// CGPoint initialPos; // in .h file

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPt = [touch locationInView:touch.view];
    initialPos = [[CCDirector sharedDirector] convertToGL:touchPt];
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch=[touches anyObject];
    CGPoint currentPos=[myTouch locationInView:[myTouch view]];
    currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];

    float diffX = currentPos.x - initialPos.x;
    float diffY = currentPos.y - initialPos.y;

    CGPoint velocity = ccp(diffX, diffY);
    initialPos = currentPos;
    [Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch=[touches anyObject];
    CGPoint currentPos=[myTouch locationInView:[myTouch view]];
    currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
    float diffX = currentPos.x - initialPos.x;
    float diffY = currentPos.y - initialPos.y;
    CGPoint velocity = ccp(diffX, diffY);
    initialPos = currentPos;
    [Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}

I think this will help you :)