Ian Freeman
Ian Freeman

Reputation: 67

Cocos2d (IOS) - How do I constantly get the location of the user's touch?

How do I constantly get the location of the user's touch? I'm working on controllers for a game, and as it stands I must lift and individually place my finger on the new control in order to make it work. It seems that the touchLocation variable only updates upon each tap, and I want it to update constantly, so that even if I slide my finger around it will know the location and update it accordingly. Here is the relevant code. Thank you!

-(void)update:(CCTime)delta{

    if([playerDirection isEqual:@"left"]){

        _player.position = ccp(_player.position.x - 2, _player.position.y);


    }

    if([playerDirection isEqual:@"right"]){

    _player.position = ccp(_player.position.x + 2, _player.position.y);

    }


    if([direction isEqual:@"up"]){


        _enemy.position = ccp(_enemy.position.x, _enemy.position.y + 2);


    }


    if([direction isEqual:@"down"]){

    _enemy.position = ccp(_enemy.position.x, _enemy.position.y - 2);


    }



    if(_enemy.position.y >= self.contentSize.height){

    direction = @"down";

    }


    if(_enemy.position.y <= 2){

    direction = @"up";

    }

    if(isTouching){

        _player.position = ccp(_player.position.x + 2, _player.position.y);

    }






}

// -----------------------------------------------------------------------
#pragma mark - Touch Handler
// -----------------------------------------------------------------------




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

    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];

    if(touchLocation.x > 400){

        playerDirection = @"right";

    }

    if(touchLocation.x < 200){

    playerDirection = @"left";

    }







    CGPoint touchLoc = [touch locationInNode:self];

    //isTouching = true;



    // Log touch location
    CCLOG(@"Move sprite to @ %@",NSStringFromCGPoint(touchLoc));

    // Move our sprite to touch location
    //CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc];
 //[_sprite runAction:actionMove];


}

- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    playerDirection = @"none";
}

Upvotes: 0

Views: 123

Answers (2)

ashish chaklasiya
ashish chaklasiya

Reputation: 170

To get Touch you have to enable touch in -(id) init() method. you can enable touch by

self.IsTouchEnable = YES

and after this you can get touch in following touch handling methods

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

hope This will help you

Upvotes: 0

Dan Reed
Dan Reed

Reputation: 54

i see you've got a touchesBegan and touchesEnded... but you're missing touchesMoved.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  for(UITouch *touch in touches) {
    //do whatever you want
  }
}

Hope that helps, good luck!

Upvotes: 1

Related Questions